rack-transform 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 159e21b98524f2d792e7d5cc802adb86687fd4bd
4
+ data.tar.gz: a98e77d6fe21712cfc4fbb3366386b51a1d72a47
5
+ SHA512:
6
+ metadata.gz: 418a12346b5624c6b3103201dfca8c33dd488a1716205f0630e9fe1762ef84fa3e0f9ec055362a1c43226f7df3811b20edb83a104b121e4420af822227c81d09
7
+ data.tar.gz: 33face941575973e25352b5d32dbf1bfed76d992769ef61aaf785f992517d7cce8ee9961db2fc1fae8e43a7653894e9f151575c733059c8872a093d4bbe1e5e9
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Esteban Pastorino
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Rack::Transform
2
+
3
+ `Rack::Transform` is a middleware that attemps to make a compatibility layer
4
+ between two different request/responses that should hit the same endpoint.
5
+
6
+ The need arouse when migrating an API from PHP to Ruby, where the URLs were
7
+ different and the response body used to have more metadata that was moved to
8
+ headers.
9
+
10
+ ---
11
+
12
+ ## Usage
13
+
14
+ ```ruby
15
+ use Rack::Transform do |map|
16
+ map.on "playlist-get-cues" do |transformer|
17
+ transformer.request = proc do |env|
18
+ ...
19
+ end
20
+ transformer.response = proc do |status, header, body|
21
+ ...
22
+ end
23
+ end
24
+
25
+ map.on "search" do |transformer|
26
+ ...
27
+ end
28
+ ...
29
+ end
30
+ ```
31
+
32
+ `map.on` will receive a string that should match the `type` parameter from the
33
+ URL and then a block to set the request and response transformers. For more
34
+ information, see `Rack::Transform::Transformer`
35
+
36
+ ## Rack::Transform::Transformer
37
+
38
+ `Rack:Transform::Transformer` intent is to transform rack requests and/or
39
+ responses into a different format.
40
+ It can manipulate the request, the response, or both.
41
+
42
+ Usage:
43
+ ```ruby
44
+ Transformer.new do |transformer|
45
+ transformer.request = proc do |env|
46
+ ...
47
+ end
48
+ transformer.response = proc do |status, header, body|
49
+ ...
50
+ end
51
+ end
52
+ ```
53
+
54
+ `resquest` and `response` methods should receinve and object that responds to
55
+ `call`, it can be a proc # or and object. And they will receive an `env`
56
+ object and `status`, `header`, # `body` tuple accordingly. Should return the
57
+ same modified as needed.
58
+
59
+ ## Contributing
60
+
61
+ See the [contributing guide](./CONTRIBUTING.md).
62
+
@@ -0,0 +1,16 @@
1
+ module Rack
2
+ class Transform
3
+ class Transformer
4
+ REQUEST_NOOP = Proc.new { |env| env }
5
+ RESPONSE_NOOP = Proc.new { |status, header, body| [status, header, body] }
6
+
7
+ attr_accessor :request, :response
8
+
9
+ def initialize
10
+ @request = REQUEST_NOOP
11
+ @response = RESPONSE_NOOP
12
+ yield self if block_given?
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Transform
3
+ VERSION = "0.1.0".freeze
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ require "rack/transform/transformer"
2
+
3
+ module Rack
4
+ class Transform
5
+ def initialize(app)
6
+ @app = app
7
+ @transformers = {}
8
+ yield self if block_given?
9
+ end
10
+
11
+ def call(env)
12
+ if transformer = request_transformer(env)
13
+ env = transformer.request.call(env)
14
+ status, header, body = @app.call(env)
15
+ transformer.response.call(status, header, body)
16
+ else
17
+ @app.call(env)
18
+ end
19
+ end
20
+
21
+ def on(type, &block)
22
+ @transformers[type] ||= Transformer.new(&block)
23
+ end
24
+
25
+ private
26
+
27
+ def request_transformer(env)
28
+ if env[Rack::REQUEST_METHOD] == Rack::GET
29
+ req = Rack::Request.new(env)
30
+ @transformers[req.params["type"]]
31
+ end
32
+ end
33
+ end
34
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "rspec"
2
+ require "rack"
3
+ require "rack/transform"
@@ -0,0 +1,35 @@
1
+ require_relative "../../helper"
2
+
3
+ RSpec.describe Rack::Transform::Transformer do
4
+ let(:formatter) { Rack::Transform::Transformer.new }
5
+
6
+ describe "#request" do
7
+ it "defaults to noop" do
8
+ result = formatter.request.call("foo")
9
+ expect(result).to eq "foo"
10
+ end
11
+
12
+ it "can assign a block" do
13
+ formatter.request = proc do |env|
14
+ "new env"
15
+ end
16
+
17
+ expect(formatter.request.call(1)).to eq "new env"
18
+ end
19
+ end
20
+
21
+ describe "#response" do
22
+ it "defaults to noop" do
23
+ result = formatter.response.call("foo", "bar", "baz")
24
+ expect(result).to eq ["foo", "bar", "baz"]
25
+ end
26
+
27
+ it "can assign a block" do
28
+ formatter.response = proc do |status, header, body|
29
+ ["new status", "new header", "new body"]
30
+ end
31
+
32
+ expect(formatter.response.call(1, 2, 3)).to eq ["new status", "new header", "new body"]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,53 @@
1
+ require_relative "../helper"
2
+
3
+ RSpec.describe Rack::Transform do
4
+ context "request" do
5
+ it "transforms a request" do
6
+ stack = Rack::Builder.new do
7
+ use Rack::Transform do |map|
8
+ map.on("foo") do |transformer|
9
+ transformer.request = proc do |env|
10
+ env[Rack::REQUEST_METHOD] = "POST"
11
+ env
12
+ end
13
+ end
14
+ end
15
+
16
+ run Proc.new { |env|
17
+ [200, {}, ["method: #{env[Rack::REQUEST_METHOD]}"]]
18
+ }
19
+ end
20
+
21
+ request = Rack::MockRequest.new(stack)
22
+
23
+ response = request.get("?type=foo")
24
+
25
+ expect(response.body).to eq "method: POST"
26
+ end
27
+ end
28
+
29
+ context "response" do
30
+ it "transforms a response" do
31
+ stack = Rack::Builder.new do
32
+ use Rack::Transform do |map|
33
+ map.on("bar") do |transformer|
34
+ transformer.response = proc do |status, header, body|
35
+ new_body = ["response: #{body.join("")}"]
36
+ [status, header, new_body]
37
+ end
38
+ end
39
+ end
40
+
41
+ run Proc.new { |env|
42
+ [200, {}, ["lorem ipsum"]]
43
+ }
44
+ end
45
+
46
+ request = Rack::MockRequest.new(stack)
47
+
48
+ response = request.get("?type=bar")
49
+
50
+ expect(response.body).to eq "response: lorem ipsum"
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-transform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Esteban Pastorino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.5'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.5.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.2'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.2.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.2'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.2.0
53
+ description: Rack::Transform is a middleware that attemps to make a compatibility
54
+ layer between two different request/responses that should hit the same endpoint.
55
+ email:
56
+ - ejpastorino@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - MIT-LICENSE
62
+ - README.md
63
+ - lib/rack/transform.rb
64
+ - lib/rack/transform/transformer.rb
65
+ - lib/rack/transform/version.rb
66
+ - spec/helper.rb
67
+ - spec/rack/transform/transformer_spec.rb
68
+ - spec/rack/transform_spec.rb
69
+ homepage: http://github.com/kitop/rack-transform
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.2.2
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Transform Rack request and responses on the fly.
93
+ test_files:
94
+ - spec/helper.rb
95
+ - spec/rack/transform/transformer_spec.rb
96
+ - spec/rack/transform_spec.rb