raki 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 525786fbd7d2b61ce8eaf8f2f5f6ea070de9801521a353b71c62ffbcf0d3b2c6
4
+ data.tar.gz: ce2226ef9894b6fff04ac74923ce26168f6674802a2ea83cedf93d8d6c6717e5
5
+ SHA512:
6
+ metadata.gz: 6905b821786588bc1215bf498e75b7421284d2862f35e4282ec994557c5029fd124a805909bcafe465a9335b28507832d28242dc3fa33c66d170fe51d568c96b
7
+ data.tar.gz: 51b1595622b43efb6c7a220cc65032c39391ae361a9f76d454da5bd9de1ddd760180fbc804e1004366c8f74535bfb4ed8d99f505caf72848dde0608dc997b8b2
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ desc 'Run all the tests'
7
+ task default: :test
8
+
9
+ Rake::TestTask.new("test:regular") do |t|
10
+ t.libs << "test"
11
+ t.test_files = FileList["test/**/*_test.rb"]
12
+ t.warning = false
13
+ t.verbose = true
14
+ end
15
+
16
+ desc "Run all the fast + platform agnostic tests"
17
+ task test: %w[test:regular]
18
+
19
+ desc "Run all the tests we run on CI"
20
+ task ci: :test
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raki
4
+ autoload :Block, 'raki/block'
5
+ autoload :Builder, 'raki/builder'
6
+ autoload :Cache, 'raki/cache'
7
+ autoload :Idem, 'raki/idem'
8
+ autoload :Lint, 'raki/lint'
9
+ autoload :Merge, 'raki/merge'
10
+ autoload :MiddlewareBase, 'raki/middleware_base'
11
+ autoload :Nil, 'raki/nil'
12
+ autoload :Params, 'raki/params'
13
+ autoload :Require, 'raki/require'
14
+ autoload :Slice, 'raki/slice'
15
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Middleware to execute a Block.
4
+ module Raki
5
+ class Block < Raki::MiddlewareBase
6
+ def call(env)
7
+ my_env = env
8
+ @block.call(my_env)
9
+ @app.call(my_env)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ # see also
4
+ # cat ~/test/rack/lib/rack/builder.rb
5
+
6
+ # inspired by Rack::Builder
7
+ module Raki
8
+ class Builder
9
+ def initialize(default_app = nil, &block)
10
+ @added = []
11
+ @runs = [default_app].compact
12
+ instance_eval(&block) if block_given?
13
+ end
14
+
15
+ # Create a new Raki::Builder instance and return the Raki application
16
+ # generated from it.
17
+ def self.app(default_app = nil, &block)
18
+ new(default_app, &block).to_app
19
+ end
20
+
21
+ def add(middleware, *args, &block)
22
+ raise 'No add allowed after a run' if @runs.size.positive?
23
+
24
+ @added <<
25
+ if middleware.instance_of?(Class)
26
+ proc { |app| middleware.new(app, *args, &block) }
27
+ else
28
+ proc { |_app| middleware }
29
+ end
30
+ end
31
+ ruby2_keywords(:add) if respond_to?(:ruby2_keywords, true)
32
+
33
+ def run(app)
34
+ @runs << app
35
+ end
36
+
37
+ def to_app
38
+ app = fake_parallel(@runs)
39
+ @added.reverse.inject(app) { |a, e| e[a] }
40
+ end
41
+
42
+ private
43
+ def fake_parallel(runs)
44
+ return runs.first if runs.size < 2
45
+
46
+ app = lambda do |env|
47
+ status, hsh, body = [200, {}, []]
48
+ runs.shuffle.each { |run|
49
+ s, h, b = run.call(env)
50
+ status = s unless s == 200
51
+ hsh.merge! h
52
+ body << b unless b.empty?
53
+ }
54
+
55
+ [status, hsh, body.flatten]
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'crimp'
4
+
5
+ # not yet finished
6
+ # requires a Rails environment! (see Rails below)
7
+ # middleware for caching.
8
+
9
+ module Raki
10
+ class Cache < Raki::MiddlewareBase
11
+ def call(env)
12
+ @block&.call(env)
13
+ kache(Crimp.signature(env)) {
14
+ @app.call(env)
15
+ }
16
+ end
17
+
18
+ private
19
+ def kache(key)
20
+ # raise 'Missing Rails environment' unless defined?(Rails.cache)
21
+ return yield unless defined?(Rails.cache)
22
+
23
+ Rails.cache.fetch(key, expires_in: 1.hour) {
24
+ yield
25
+ }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Middleware just passing over or returning the params.
4
+ module Raki
5
+ class Idem < Raki::MiddlewareBase
6
+ def call(env)
7
+ return env unless @app
8
+
9
+ @block&.call(env)
10
+ @app.call(env)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Raki::AssertRequest validates your requests according to Raki spec.
4
+
5
+ # see also:
6
+ # cat ~/test/rack/lib/rack/lint.rb
7
+ # cat ~/test/rack/SPEC.rdoc
8
+ module Raki
9
+ class LintError < RuntimeError; end
10
+
11
+ class Lint < Raki::MiddlewareBase
12
+ def call(env)
13
+ assert('No env given') { env }
14
+ assert('Block complained') { @block.call(env) } if @block
15
+
16
+ @block&.call(env)
17
+ response = @app.call(env)
18
+
19
+ assert('Expected array') { response.class == Array }
20
+ assert('Expected array of 3 items') { response.size == 3 }
21
+ assert('First item must be an Integer') { response[0].class == Integer }
22
+ assert('Second item must be a Hash') { response[1].class == Hash }
23
+ assert('Third item must be an Array') { response[2].class == Array }
24
+
25
+ response
26
+ end
27
+
28
+ private
29
+ def assert(message)
30
+ return if yield
31
+
32
+ raise LintError, message
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Middleware Merge.
4
+ module Raki
5
+ class Merge < Raki::MiddlewareBase
6
+ def call(env)
7
+ my_env = env.merge(*@args)
8
+ @block&.call(my_env)
9
+ @app.call(my_env)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Middleware just passing over or returning the params.
4
+ module Raki
5
+ class MiddlewareBase
6
+ def initialize(app = nil, *args, &block)
7
+ @app = app
8
+ @args = args
9
+ @block = block
10
+ end
11
+
12
+ def call(_env)
13
+ raise '#call must be overwritten by a middleware raki'
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Returns a Raki zero.
4
+ module Raki
5
+ class Nil < Raki::MiddlewareBase
6
+ def call(_env)
7
+ @block&.call(env)
8
+ @app&.call(env)
9
+
10
+ [200, {}, []]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Middleware Require.
4
+ module Raki
5
+ class RequireError < RuntimeError; end
6
+
7
+ class Require < Raki::MiddlewareBase
8
+ def call(env)
9
+ expected = @args.flatten.sort
10
+ bool = env.keys.sort == expected
11
+ raise(RequireError, "Expected <#{expected.join}>") unless bool
12
+
13
+ @block&.call(env)
14
+ @app.call(env)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Middleware Slice.
4
+ module Raki
5
+ class Slice < Raki::MiddlewareBase
6
+ def call(env)
7
+ my_env = env.slice(*@args.flatten)
8
+ @block&.call(my_env)
9
+ @app.call(my_env)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Raki is freely distributable under the terms of an MIT-style license.
4
+ # See MIT-LICENSE or https://opensource.org/licenses/MIT.
5
+
6
+ # The Raki main module, serving as a namespace for all core Raki
7
+ # modules and classes.
8
+ #
9
+ # All modules meant for use in your application are <tt>autoload</tt>ed here,
10
+ # so it should be enough just to <tt>require 'rack'</tt> in your code.
11
+
12
+ module Raki
13
+ VERSION = '0.0.2'
14
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/raki/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "raki"
7
+ s.version = Raki::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.summary = "A modular hash interface."
10
+ s.license = "MIT"
11
+
12
+ s.description = <<~EOF
13
+ Raki provides a minimal, modular and adaptable interface for developing
14
+ web applications in Ruby. By wrapping HTTP requests and responses in
15
+ the simplest way possible, it unifies and distills the API for web
16
+ servers, web frameworks, and software in between (the so-called
17
+ middleware) into a single method call.
18
+ EOF
19
+
20
+ s.files = Dir['{bin/*,contrib/*,example/*,lib/**/*}'] +
21
+ %w(raki.gemspec Rakefile)
22
+
23
+ s.bindir = 'bin'
24
+ s.require_path = 'lib'
25
+
26
+ s.author = 'Dittmar Krall'
27
+ s.email = 'dittmar.krall@matique.de'
28
+
29
+ s.homepage = 'https://matique.com'
30
+
31
+ s.required_ruby_version = '>= 2.6.0'
32
+
33
+ s.add_development_dependency 'rake'
34
+ s.add_development_dependency 'bundler'
35
+
36
+ s.add_development_dependency 'minitest', "~> 5.0"
37
+ s.add_development_dependency 'minitest-sprint'
38
+ s.add_development_dependency 'minitest-global_expectations'
39
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: raki
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Dittmar Krall
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-sprint
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-global_expectations
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: |
84
+ Raki provides a minimal, modular and adaptable interface for developing
85
+ web applications in Ruby. By wrapping HTTP requests and responses in
86
+ the simplest way possible, it unifies and distills the API for web
87
+ servers, web frameworks, and software in between (the so-called
88
+ middleware) into a single method call.
89
+ email: dittmar.krall@matique.de
90
+ executables: []
91
+ extensions: []
92
+ extra_rdoc_files: []
93
+ files:
94
+ - Rakefile
95
+ - lib/raki.rb
96
+ - lib/raki/block.rb
97
+ - lib/raki/builder.rb
98
+ - lib/raki/cache.rb
99
+ - lib/raki/idem.rb
100
+ - lib/raki/lint.rb
101
+ - lib/raki/merge.rb
102
+ - lib/raki/middleware_base.rb
103
+ - lib/raki/nil.rb
104
+ - lib/raki/require.rb
105
+ - lib/raki/slice.rb
106
+ - lib/raki/version.rb
107
+ - raki.gemspec
108
+ homepage: https://matique.com
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 2.6.0
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.0.8
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: A modular hash interface.
131
+ test_files: []