rack-transform 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -4
- data/lib/rack/transform/transformers/base.rb +98 -0
- data/lib/rack/transform/version.rb +1 -1
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3977529e8deed2f7bea78f754015c3e7a7a85123
|
4
|
+
data.tar.gz: 1613ce73e0c4f8a9eb65869ebc4b24eb33ee0f1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e64821e2cee834db71f45e3b0dede2b8cefbd25680ca9e18bd087f1e13d0c4a0ed5dca032b62dbbd213b7a7fca4d4db8f6b4aa7b599df60353ec83f5fb8f3e00
|
7
|
+
data.tar.gz: 6f338b5c299e876d305ca9040ce3cb52816cce6446589a3ad2d402e8866f7c128a92af67fecba1a3ae0d6afe40658865d8c84ba0b5d5c338869501da4075dc55
|
data/README.md
CHANGED
@@ -7,6 +7,9 @@ The need arouse when migrating an API from PHP to Ruby, where the URLs were
|
|
7
7
|
different and the response body used to have more metadata that was moved to
|
8
8
|
headers.
|
9
9
|
|
10
|
+
[![Build Status](https://travis-ci.org/kitop/rack-transform.svg)](https://travis-ci.org/kitop/rack-transform)
|
11
|
+
[![Code Climate](https://codeclimate.com/github/kitop/rack-transform/badges/gpa.svg)](https://codeclimate.com/github/kitop/rack-transform)
|
12
|
+
|
10
13
|
---
|
11
14
|
|
12
15
|
## Usage
|
@@ -51,10 +54,15 @@ Transformer.new do |transformer|
|
|
51
54
|
end
|
52
55
|
```
|
53
56
|
|
54
|
-
`
|
55
|
-
`call`, it can be a proc
|
56
|
-
|
57
|
-
|
57
|
+
`request` and `response` methods should receive an object that responds to
|
58
|
+
`call`, it can be a proc or and object. And they will receive an `env` object
|
59
|
+
and `status`, `header`, `body` tuple accordingly. Should return the same
|
60
|
+
modified as needed. You can use the base transformers if you prefer to use
|
61
|
+
actual classes instead of `proc`s.
|
62
|
+
|
63
|
+
## Base Transformer
|
64
|
+
|
65
|
+
To help with boilerplate code, base transformer request and response classes were added. [More info](./lib/rack/transform/transformers/base.rb).
|
58
66
|
|
59
67
|
## Contributing
|
60
68
|
|
@@ -0,0 +1,98 @@
|
|
1
|
+
module Rack
|
2
|
+
class Transform
|
3
|
+
module Transformers
|
4
|
+
module Base
|
5
|
+
class Request
|
6
|
+
CONTENT_TYPE = "CONTENT_TYPE".freeze
|
7
|
+
CONTENT_LENGTH = "CONTENT_LENGTH".freeze
|
8
|
+
POST_BODY = "rack.input".freeze
|
9
|
+
JSON_TYPE = "application/json".freeze
|
10
|
+
|
11
|
+
def self.call(env)
|
12
|
+
new(env).process
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :env
|
16
|
+
|
17
|
+
def initialize(env)
|
18
|
+
@env = env
|
19
|
+
end
|
20
|
+
|
21
|
+
def process
|
22
|
+
raise NotImplementedError
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def req
|
28
|
+
@req ||= Rack::Request.new(env)
|
29
|
+
end
|
30
|
+
|
31
|
+
def params
|
32
|
+
req.params
|
33
|
+
end
|
34
|
+
|
35
|
+
def update_env(path:, query:, method:, body:)
|
36
|
+
body = body.is_a?(String) ? body : dump_json(body)
|
37
|
+
env.update(
|
38
|
+
Rack::PATH_INFO => path,
|
39
|
+
Rack::QUERY_STRING => query,
|
40
|
+
Rack::REQUEST_METHOD => method,
|
41
|
+
CONTENT_TYPE => JSON_TYPE,
|
42
|
+
CONTENT_LENGTH => body.bytesize,
|
43
|
+
POST_BODY => StringIO.new(body)
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
def dump_json(data)
|
48
|
+
JSON.dump(data)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class Response
|
53
|
+
CONTENT_LENGTH = "Content-Length".freeze
|
54
|
+
CONTENT_TYPE = "Content-Type".freeze
|
55
|
+
JSON_TYPE = "application/json".freeze
|
56
|
+
|
57
|
+
def self.call(status, header, body)
|
58
|
+
new(status, header, body).process
|
59
|
+
end
|
60
|
+
|
61
|
+
attr_reader :status, :header, :body
|
62
|
+
|
63
|
+
def initialize(status, header, body)
|
64
|
+
@status = status
|
65
|
+
@header = header.merge CONTENT_TYPE => JSON_TYPE
|
66
|
+
@body = body
|
67
|
+
end
|
68
|
+
|
69
|
+
def process
|
70
|
+
raise NotImplementedError
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def respond_with(body, status: 200, header: @header)
|
76
|
+
formatted_body = body.is_a?(String) ? body : dump_json(body)
|
77
|
+
|
78
|
+
header[CONTENT_LENGTH] = formatted_body.bytesize.to_s
|
79
|
+
|
80
|
+
[status, header, [formatted_body]]
|
81
|
+
end
|
82
|
+
|
83
|
+
def parsed_body
|
84
|
+
@parsed_body ||= body.empty? ? [] : load_json(body.first)
|
85
|
+
end
|
86
|
+
|
87
|
+
def dump_json(data)
|
88
|
+
JSON.dump(data)
|
89
|
+
end
|
90
|
+
|
91
|
+
def load_json(data)
|
92
|
+
JSON.load(data)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-transform
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Esteban Pastorino
|
@@ -30,6 +30,20 @@ dependencies:
|
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 1.5.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
33
47
|
- !ruby/object:Gem::Dependency
|
34
48
|
name: rspec
|
35
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -62,6 +76,7 @@ files:
|
|
62
76
|
- README.md
|
63
77
|
- lib/rack/transform.rb
|
64
78
|
- lib/rack/transform/transformer.rb
|
79
|
+
- lib/rack/transform/transformers/base.rb
|
65
80
|
- lib/rack/transform/version.rb
|
66
81
|
- spec/helper.rb
|
67
82
|
- spec/rack/transform/transformer_spec.rb
|