content_gateway 0.4.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +3 -0
- data/Changelog +20 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +75 -43
- data/README.md +10 -6
- data/content-gateway.gemspec +4 -3
- data/lib/content_gateway/cache.rb +9 -0
- data/lib/content_gateway/exceptions.rb +2 -0
- data/lib/content_gateway/gateway.rb +19 -5
- data/lib/content_gateway/request.rb +3 -3
- data/lib/content_gateway/version.rb +1 -1
- data/lib/content_gateway.rb +1 -0
- data/spec/integration/content_gateway/gateway_spec.rb +37 -8
- data/spec/spec_helper.rb +3 -0
- data/spec/unit/content_gateway/gateway_spec.rb +96 -90
- data/spec/unit/content_gateway/request_spec.rb +14 -2
- metadata +46 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fbe5c94a8e75a4bd2d1b593393bf7e95fb00eb2
|
4
|
+
data.tar.gz: 4dea345804cdab15b2aa85df240e0b3f8d4178ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8133b6e29883e98d16a3d23cc70dd44f8020a4406456821f160639ae1722c11d80ab0087c9fa17b9d1f7fdf2e05bfd93e32b639c06651862efee7841ddca86cc
|
7
|
+
data.tar.gz: 0643f896c0087a921fc159ecac8d92d95f76759243abe5182873e9e5bcff1fdfacddd79b7960b55e2ef594dfdc5692f50ee1199894384ff07df5a69129684fe7
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.3.8
|
data/.travis.yml
ADDED
data/Changelog
CHANGED
@@ -1,10 +1,30 @@
|
|
1
|
+
2022-02-11 [0.6.0]
|
2
|
+
|
3
|
+
* Bugfix: Updated the rest-client gem from 1.0 to 2.1 to fix memory leak issues caused by the old version
|
4
|
+
|
5
|
+
2016-12-19 [0.5.2]
|
6
|
+
|
7
|
+
* Bugfix: Now stale_on_error returns true if theres no config
|
8
|
+
* Add default url generator
|
9
|
+
|
10
|
+
2015-11-19 [0.5.1]
|
11
|
+
|
12
|
+
* Bugfix proxy implementation wrapper on rest-client
|
13
|
+
|
14
|
+
2015-07-24 [0.5.0]
|
15
|
+
|
16
|
+
* Mapping JSON parse errors to ContentGateway::ParserError
|
17
|
+
|
1
18
|
2015-06-16 [0.4.0]
|
19
|
+
|
2
20
|
* Adding ssl_version support to ssl_certificate hash
|
3
21
|
|
4
22
|
2015-01-05 [0.3.0]
|
23
|
+
|
5
24
|
* Optional url generator. (Without the url generator on boot the content gateway will use the get/post/delete/put resource argument as full url for request). Closes #4
|
6
25
|
|
7
26
|
2014-11-06 [0.2.1]
|
27
|
+
|
8
28
|
* Fix: Do not send connection parameters to url generator. Closes #2
|
9
29
|
* Fix: Send http headers to request object. Closes #3
|
10
30
|
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,55 +1,79 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
content_gateway (0.
|
5
|
-
activesupport
|
6
|
-
json
|
7
|
-
rest-client
|
4
|
+
content_gateway (0.6.0)
|
5
|
+
activesupport (>= 3)
|
6
|
+
json (~> 1.0)
|
7
|
+
rest-client (~> 2.1)
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
activesupport (
|
13
|
-
|
14
|
-
|
12
|
+
activesupport (5.2.6)
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
+
i18n (>= 0.7, < 2)
|
15
|
+
minitest (~> 5.1)
|
16
|
+
tzinfo (~> 1.1)
|
17
|
+
byebug (11.0.1)
|
18
|
+
concurrent-ruby (1.1.9)
|
19
|
+
coveralls (0.7.1)
|
15
20
|
multi_json (~> 1.3)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
21
|
+
rest-client
|
22
|
+
simplecov (>= 0.7)
|
23
|
+
term-ansicolor
|
24
|
+
thor
|
25
|
+
diff-lcs (1.5.0)
|
26
|
+
docile (1.3.5)
|
27
|
+
domain_name (0.5.20190701)
|
28
|
+
unf (>= 0.0.5, < 1.0.0)
|
29
|
+
http-accept (1.7.0)
|
30
|
+
http-cookie (1.0.4)
|
31
|
+
domain_name (~> 0.5)
|
32
|
+
i18n (1.9.1)
|
33
|
+
concurrent-ruby (~> 1.0)
|
34
|
+
json (1.8.6)
|
35
|
+
mime-types (3.4.1)
|
36
|
+
mime-types-data (~> 3.2015)
|
37
|
+
mime-types-data (3.2022.0105)
|
38
|
+
minitest (5.15.0)
|
39
|
+
multi_json (1.15.0)
|
40
|
+
netrc (0.11.0)
|
41
|
+
rake (13.0.6)
|
42
|
+
rest-client (2.1.0)
|
43
|
+
http-accept (>= 1.7.0, < 2.0)
|
44
|
+
http-cookie (>= 1.0.2, < 2.0)
|
45
|
+
mime-types (>= 1.16, < 4.0)
|
46
|
+
netrc (~> 0.8)
|
47
|
+
rspec (3.10.0)
|
48
|
+
rspec-core (~> 3.10.0)
|
49
|
+
rspec-expectations (~> 3.10.0)
|
50
|
+
rspec-mocks (~> 3.10.0)
|
51
|
+
rspec-core (3.10.2)
|
52
|
+
rspec-support (~> 3.10.0)
|
53
|
+
rspec-expectations (3.10.2)
|
54
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
55
|
+
rspec-support (~> 3.10.0)
|
56
|
+
rspec-mocks (3.10.3)
|
40
57
|
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
-
rspec-support (~> 3.
|
42
|
-
rspec-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
58
|
+
rspec-support (~> 3.10.0)
|
59
|
+
rspec-support (3.10.3)
|
60
|
+
simplecov (0.17.1)
|
61
|
+
docile (~> 1.1)
|
62
|
+
json (>= 1.8, < 3)
|
63
|
+
simplecov-html (~> 0.10.0)
|
64
|
+
simplecov-html (0.10.2)
|
65
|
+
sync (0.5.0)
|
66
|
+
term-ansicolor (1.7.1)
|
67
|
+
tins (~> 1.0)
|
68
|
+
thor (1.2.1)
|
69
|
+
thread_safe (0.3.6)
|
70
|
+
tins (1.31.0)
|
71
|
+
sync
|
72
|
+
tzinfo (1.2.9)
|
73
|
+
thread_safe (~> 0.1)
|
74
|
+
unf (0.1.4)
|
75
|
+
unf_ext
|
76
|
+
unf_ext (0.0.8)
|
53
77
|
|
54
78
|
PLATFORMS
|
55
79
|
ruby
|
@@ -57,5 +81,13 @@ PLATFORMS
|
|
57
81
|
DEPENDENCIES
|
58
82
|
byebug
|
59
83
|
content_gateway!
|
84
|
+
coveralls
|
85
|
+
rake
|
60
86
|
rspec (>= 2.3.0)
|
61
87
|
simplecov (>= 0.7.1)
|
88
|
+
|
89
|
+
RUBY VERSION
|
90
|
+
ruby 2.3.8p459
|
91
|
+
|
92
|
+
BUNDLED WITH
|
93
|
+
2.3.6
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# Content Gateway
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/globocom/content-gateway-ruby.svg)](https://travis-ci.org/globocom/content-gateway-ruby)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/github/globocom/content-gateway-ruby/badge.svg?branch=master)](https://coveralls.io/github/globocom/content-gateway-ruby?branch=master)
|
5
|
+
|
3
6
|
An easy way to get external content with two cache levels. The first is a performance cache and second is the stale.
|
4
7
|
|
5
8
|
Content Gateway lets you set a timeout for any request.
|
@@ -33,6 +36,7 @@ Or install it yourself as:
|
|
33
36
|
- `timeout`: request timeout in seconds
|
34
37
|
- `cache_expires_in`: cache data expiration time, in seconds
|
35
38
|
- `cache_stale_expires_in`: stale cache data expiration time, in seconds
|
39
|
+
- `stale_on_error`: if `true`, returns value from cache stale (if available) after a server error. Default value: `true`
|
36
40
|
- `cache`: cache store instance. This may be an instance of `ActiveSupport::Cache`
|
37
41
|
- `proxy`: proxy address, if needed
|
38
42
|
|
@@ -43,8 +47,9 @@ config = OpenStruct.new(
|
|
43
47
|
timeout: 2,
|
44
48
|
cache_expires_in: 1800,
|
45
49
|
cache_stale_expires_in: 86400,
|
50
|
+
stale_on_error: false,
|
46
51
|
cache: ActiveSupport::Cache.lookup_store(:memory_store),
|
47
|
-
proxy: "http://proxy.example.com
|
52
|
+
proxy: "http://proxy.example.com:3128"
|
48
53
|
)
|
49
54
|
```
|
50
55
|
|
@@ -54,13 +59,12 @@ config = OpenStruct.new(
|
|
54
59
|
|
55
60
|
- a label, which is used in the log messages
|
56
61
|
- a config object, just as described above
|
57
|
-
- an URL Generator object. This may be any object that responds to a `generate` method, like this:
|
58
|
-
-
|
62
|
+
- (optional) an URL Generator object. This may be any object that responds to a `generate` method, like this:
|
63
|
+
- (optional) a hash with default params. Currently, it only supports default headers
|
59
64
|
|
60
65
|
```ruby
|
61
66
|
class UrlGenerator
|
62
67
|
def generate(resource_path, params = {})
|
63
|
-
args = ""
|
64
68
|
args = "?#{params.map {|k, v| "#{k}=#{v}"}.join("&")}" if params.any?
|
65
69
|
"http://example.com/#{resource_path}#{args}"
|
66
70
|
end
|
@@ -71,7 +75,7 @@ default_params = { headers: { Accept: "application/json" } }
|
|
71
75
|
gateway = ContentGateway::Gateway.new("My API", config, UrlGenerator.new, default_params)
|
72
76
|
```
|
73
77
|
|
74
|
-
Every param may be overrided on each request.
|
78
|
+
If ommited, the default URL Generator adds the method call params as query string parameters. Every param may be overrided on each request.
|
75
79
|
|
76
80
|
This Gateway object supports the following methods:
|
77
81
|
|
@@ -167,4 +171,4 @@ gateway.post("/api/post_example", payload: { param1: "value" }, ssl_certificate:
|
|
167
171
|
|
168
172
|
## License
|
169
173
|
|
170
|
-
Copyright (c)
|
174
|
+
Copyright (c) 2016 Globo.com - Webmedia. See [LICENSE.txt](https://github.com/globocom/content-gateway-ruby/blob/master/LICENSE.txt) for more details.
|
data/content-gateway.gemspec
CHANGED
@@ -16,11 +16,12 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
17
|
gem.require_paths = ["lib"]
|
18
18
|
|
19
|
-
gem.add_dependency "activesupport"
|
20
|
-
gem.add_dependency "rest-client"
|
21
|
-
gem.add_dependency "json"
|
19
|
+
gem.add_dependency "activesupport", ">= 3"
|
20
|
+
gem.add_dependency "rest-client", "~> 2.1"
|
21
|
+
gem.add_dependency "json", "~> 1.0"
|
22
22
|
|
23
23
|
gem.add_development_dependency "rspec", ">= 2.3.0"
|
24
24
|
gem.add_development_dependency "simplecov", ">= 0.7.1"
|
25
25
|
gem.add_development_dependency "byebug"
|
26
|
+
gem.add_development_dependency "rake"
|
26
27
|
end
|
@@ -17,6 +17,7 @@ module ContentGateway
|
|
17
17
|
timeout = params[:timeout] || @config.timeout
|
18
18
|
expires_in = params[:expires_in] || @config.cache_expires_in
|
19
19
|
stale_expires_in = params[:stale_expires_in] || @config.cache_stale_expires_in
|
20
|
+
stale_on_error = config_stale_on_error params, @config
|
20
21
|
|
21
22
|
begin
|
22
23
|
Timeout.timeout(timeout) do
|
@@ -39,6 +40,7 @@ module ContentGateway
|
|
39
40
|
|
40
41
|
rescue ContentGateway::ServerError => e
|
41
42
|
begin
|
43
|
+
raise e unless stale_on_error
|
42
44
|
serve_stale
|
43
45
|
rescue ContentGateway::StaleCacheNotAvailableError
|
44
46
|
raise e
|
@@ -56,5 +58,12 @@ module ContentGateway
|
|
56
58
|
def stale_key
|
57
59
|
@stale_key ||= "stale:#{@url}"
|
58
60
|
end
|
61
|
+
|
62
|
+
private
|
63
|
+
def config_stale_on_error params, config
|
64
|
+
return params[:stale_on_error] unless params[:stale_on_error].nil?
|
65
|
+
return config.stale_on_error unless config.try(:stale_on_error).nil?
|
66
|
+
true
|
67
|
+
end
|
59
68
|
end
|
60
69
|
end
|
@@ -3,7 +3,7 @@ module ContentGateway
|
|
3
3
|
def initialize(label, config, url_generator = nil, default_params = {})
|
4
4
|
@label = label
|
5
5
|
@config = config
|
6
|
-
@url_generator = url_generator
|
6
|
+
@url_generator = url_generator || DefaultUrlGenerator
|
7
7
|
@default_params = default_params
|
8
8
|
end
|
9
9
|
|
@@ -88,19 +88,19 @@ module ContentGateway
|
|
88
88
|
end
|
89
89
|
|
90
90
|
def get_json(resource_path, params = {})
|
91
|
-
|
91
|
+
json_request(:get, resource_path, params)
|
92
92
|
end
|
93
93
|
|
94
94
|
def post_json(resource_path, params = {})
|
95
|
-
|
95
|
+
json_request(:post, resource_path, params)
|
96
96
|
end
|
97
97
|
|
98
98
|
def put_json(resource_path, params = {})
|
99
|
-
|
99
|
+
json_request(:put, resource_path, params)
|
100
100
|
end
|
101
101
|
|
102
102
|
def delete_json(resource_path, params = {})
|
103
|
-
|
103
|
+
json_request(:delete, resource_path, params)
|
104
104
|
end
|
105
105
|
|
106
106
|
def generate_url(resource_path, params = {})
|
@@ -158,6 +158,13 @@ module ContentGateway
|
|
158
158
|
end
|
159
159
|
end
|
160
160
|
|
161
|
+
def json_request(verb, resource_path, params = {})
|
162
|
+
JSON.parse(self.send(verb, resource_path, params))
|
163
|
+
rescue JSON::ParserError => e
|
164
|
+
url = generate_url(resource_path, params) rescue resource_path
|
165
|
+
raise ContentGateway::ParserError.new(url, e)
|
166
|
+
end
|
167
|
+
|
161
168
|
def measure(message)
|
162
169
|
result = nil
|
163
170
|
time_elapsed = Benchmark.measure { result = yield }
|
@@ -205,5 +212,12 @@ module ContentGateway
|
|
205
212
|
end
|
206
213
|
end.yield
|
207
214
|
end
|
215
|
+
|
216
|
+
class DefaultUrlGenerator
|
217
|
+
def generate(resource_path, params = {})
|
218
|
+
args = "?#{params.map {|k, v| "#{k}=#{v}"}.join("&")}" if params.any?
|
219
|
+
"#{resource_path}#{args}"
|
220
|
+
end
|
221
|
+
end
|
208
222
|
end
|
209
223
|
end
|
@@ -1,17 +1,17 @@
|
|
1
1
|
module ContentGateway
|
2
2
|
class Request
|
3
3
|
def initialize(method, url, headers = {}, payload = {}, proxy = nil, params = {})
|
4
|
-
data = { method: method, url: url, proxy: proxy
|
4
|
+
data = { method: method, url: url, proxy: proxy }.tap do |h|
|
5
5
|
h[:payload] = payload if payload.present?
|
6
6
|
h[:headers] = headers if headers.present?
|
7
7
|
h = load_ssl_params(h, params) if params.has_key?(:ssl_certificate)
|
8
8
|
end
|
9
|
+
RestClient.proxy = proxy
|
9
10
|
@client = RestClient::Request.new(data)
|
10
11
|
end
|
11
12
|
|
12
13
|
def execute
|
13
|
-
@client.execute
|
14
|
-
|
14
|
+
@client.execute.to_s
|
15
15
|
rescue RestClient::ResourceNotFound => e1
|
16
16
|
raise ContentGateway::ResourceNotFound.new url, e1
|
17
17
|
|
data/lib/content_gateway.rb
CHANGED
@@ -5,6 +5,7 @@ require "benchmark"
|
|
5
5
|
require "json"
|
6
6
|
require "rest-client"
|
7
7
|
require "active_support/cache"
|
8
|
+
require "active_support/notifications"
|
8
9
|
require "active_support/core_ext/object/blank"
|
9
10
|
require "active_support/core_ext/date_time/calculations"
|
10
11
|
require "active_support/core_ext/hash/indifferent_access"
|
@@ -8,11 +8,24 @@ describe ContentGateway::Gateway do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
let! :config do
|
11
|
-
|
11
|
+
class FakeConfig
|
12
|
+
attr_accessor :cache, :cache_expires_in, :cache_stale_expires_in, :proxy, :timeout
|
13
|
+
|
14
|
+
def initialize(params)
|
15
|
+
@cache = params[:cache]
|
16
|
+
@cache_expires_in = params[:cache_expires_in]
|
17
|
+
@cache_stale_expires_in = params[:cache_stale_expires_in]
|
18
|
+
@proxy = params[:proxy]
|
19
|
+
@timeout = params[:timeout]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
FakeConfig.new(
|
12
24
|
cache: ActiveSupport::Cache::NullStore.new,
|
13
25
|
cache_expires_in: 15.minutes,
|
14
26
|
cache_stale_expires_in: 1.hour,
|
15
|
-
proxy: "proxy"
|
27
|
+
proxy: "proxy",
|
28
|
+
timeout: 2
|
16
29
|
)
|
17
30
|
end
|
18
31
|
|
@@ -123,9 +136,10 @@ describe ContentGateway::Gateway do
|
|
123
136
|
end
|
124
137
|
|
125
138
|
context "with stale cache" do
|
139
|
+
let(:cache_store) {double("cache_store")}
|
140
|
+
|
126
141
|
context "on timeout" do
|
127
142
|
before do
|
128
|
-
cache_store = double("cache_store")
|
129
143
|
allow(cache_store).to receive(:fetch).with(resource_url, expires_in: default_expires_in).and_raise(Timeout::Error)
|
130
144
|
allow(cache_store).to receive(:read).with(stale_cache_key).and_return(cached_response)
|
131
145
|
config.cache = cache_store
|
@@ -140,14 +154,29 @@ describe ContentGateway::Gateway do
|
|
140
154
|
before do
|
141
155
|
stub_request_with_error({method: :get, url: resource_url, proxy: config.proxy, headers: headers}, RestClient::InternalServerError.new(nil, 500))
|
142
156
|
|
143
|
-
cache_store = double("cache_store")
|
144
157
|
allow(cache_store).to receive(:fetch).with(resource_url, expires_in: default_expires_in).and_yield
|
145
158
|
allow(cache_store).to receive(:read).with(stale_cache_key).and_return(cached_response)
|
146
159
|
config.cache = cache_store
|
147
160
|
end
|
148
161
|
|
149
|
-
|
150
|
-
|
162
|
+
context "give a config without stale_on_error" do
|
163
|
+
context "uses the default value" do
|
164
|
+
it "returns cached stale" do
|
165
|
+
expect(gateway.get(resource_path)).to eql "cached response"
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context "when stale_on_error configuration is false" do
|
171
|
+
let :gateway do
|
172
|
+
config_with_stale_on_error = config.dup
|
173
|
+
allow(config_with_stale_on_error).to receive(:stale_on_error).and_return(false)
|
174
|
+
ContentGateway::Gateway.new "API XPTO", config_with_stale_on_error, url_generator, headers: headers
|
175
|
+
end
|
176
|
+
|
177
|
+
it "raises error" do
|
178
|
+
expect { gateway.get resource_path }.to raise_error ContentGateway::ServerError
|
179
|
+
end
|
151
180
|
end
|
152
181
|
end
|
153
182
|
end
|
@@ -388,7 +417,7 @@ describe ContentGateway::Gateway do
|
|
388
417
|
private
|
389
418
|
|
390
419
|
def stub_request(opts, payload = {}, &block)
|
391
|
-
opts = { method: :get, proxy:
|
420
|
+
opts = { method: :get, proxy: nil }.merge(opts)
|
392
421
|
request = RestClient::Request.new(opts)
|
393
422
|
allow(RestClient::Request).to receive(:new).with(opts).and_return(request)
|
394
423
|
|
@@ -400,7 +429,7 @@ describe ContentGateway::Gateway do
|
|
400
429
|
end
|
401
430
|
|
402
431
|
def stub_request_with_error(opts, exc)
|
403
|
-
opts = { method: :get, proxy:
|
432
|
+
opts = { method: :get, proxy: nil }.merge(opts)
|
404
433
|
|
405
434
|
request = RestClient::Request.new(opts)
|
406
435
|
allow(RestClient::Request).to receive(:new).with(opts).and_return(request)
|
data/spec/spec_helper.rb
CHANGED
@@ -18,6 +18,7 @@ describe ContentGateway::Gateway do
|
|
18
18
|
let(:cache) { double("cache", use?: false, status: "HIT") }
|
19
19
|
let(:request) { double("request", execute: data) }
|
20
20
|
let(:data) { '{"param": "value"}' }
|
21
|
+
let(:invalid_data) { "" }
|
21
22
|
let(:cache_params) { { timeout: 2, expires_in: 30, stale_expires_in: 180, skip_cache: false, ssl_certificate: {ssl_client_cert: "test", ssl_client_key: "test"} } }
|
22
23
|
let(:connection_params) {{ timeout: 2, ssl_certificate: {ssl_client_cert: "test", ssl_client_key: "test"} }}
|
23
24
|
|
@@ -27,22 +28,48 @@ describe ContentGateway::Gateway do
|
|
27
28
|
allow(OpenSSL::PKey::RSA).to receive(:new).with("cert_content").and_return("key")
|
28
29
|
end
|
29
30
|
|
31
|
+
shared_examples "request" do
|
32
|
+
describe "doing a request" do
|
33
|
+
it "should do a # {verb} request passing the correct parameters" do
|
34
|
+
expect(subject.send(verb, path, params)).to eql data
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "doing a json request" do
|
39
|
+
it "should parse the response as JSON" do
|
40
|
+
expect(subject.send("#{verb}_json", path, params)).to eql JSON.parse(data)
|
41
|
+
end
|
42
|
+
|
43
|
+
context "when the answer is not a valid JSON" do
|
44
|
+
let(:data) { invalid_data }
|
45
|
+
|
46
|
+
it "should raise ContentGateway::ParserError" do
|
47
|
+
expect { subject.send("#{verb}_json", path, params) }.
|
48
|
+
to raise_error(ContentGateway::ParserError)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
30
54
|
describe "Without url generator" do
|
31
55
|
describe "GET method" do
|
56
|
+
let(:query_string) { { a: 1, b: 2 } }
|
57
|
+
|
32
58
|
before do
|
33
59
|
expect(ContentGateway::Request).
|
34
|
-
|
35
|
-
|
36
|
-
|
60
|
+
to receive(:new).
|
61
|
+
with(:get, fullpath, headers, nil, config.proxy, cache_params.merge(query_string)).
|
62
|
+
and_return(request)
|
63
|
+
|
37
64
|
expect(ContentGateway::Cache).
|
38
|
-
|
39
|
-
|
40
|
-
|
65
|
+
to receive(:new).
|
66
|
+
with(config, fullpath, :get, cache_params.merge(query_string)).
|
67
|
+
and_return(cache)
|
41
68
|
end
|
42
69
|
|
43
70
|
describe "#get" do
|
44
71
|
it "should do a get request passing the correct parameters" do
|
45
|
-
expect(gateway_without_url_generator.get(fullpath, cache_params.merge(headers: headers))).to eql data
|
72
|
+
expect(gateway_without_url_generator.get(fullpath, cache_params.merge(query_string).merge(headers: headers))).to eql data
|
46
73
|
end
|
47
74
|
end
|
48
75
|
end
|
@@ -50,13 +77,14 @@ describe ContentGateway::Gateway do
|
|
50
77
|
describe "POST method" do
|
51
78
|
before do
|
52
79
|
expect(ContentGateway::Request).
|
53
|
-
|
54
|
-
|
55
|
-
|
80
|
+
to receive(:new).
|
81
|
+
with(:post, fullpath, nil, payload, config.proxy, connection_params).
|
82
|
+
and_return(request)
|
83
|
+
|
56
84
|
expect(ContentGateway::Cache).
|
57
|
-
|
58
|
-
|
59
|
-
|
85
|
+
to receive(:new).
|
86
|
+
with(config, fullpath, :post, connection_params).
|
87
|
+
and_return(cache)
|
60
88
|
end
|
61
89
|
|
62
90
|
describe "#post" do
|
@@ -69,13 +97,14 @@ describe ContentGateway::Gateway do
|
|
69
97
|
describe "PUT method" do
|
70
98
|
before do
|
71
99
|
expect(ContentGateway::Request).
|
72
|
-
|
73
|
-
|
74
|
-
|
100
|
+
to receive(:new).
|
101
|
+
with(:put, fullpath, nil, payload, config.proxy, connection_params).
|
102
|
+
and_return(request)
|
103
|
+
|
75
104
|
expect(ContentGateway::Cache).
|
76
|
-
|
77
|
-
|
78
|
-
|
105
|
+
to receive(:new).
|
106
|
+
with(config, fullpath, :put, connection_params).
|
107
|
+
and_return(cache)
|
79
108
|
end
|
80
109
|
|
81
110
|
describe "#put" do
|
@@ -88,13 +117,14 @@ describe ContentGateway::Gateway do
|
|
88
117
|
describe "DELETE method" do
|
89
118
|
before do
|
90
119
|
expect(ContentGateway::Request).
|
91
|
-
|
92
|
-
|
93
|
-
|
120
|
+
to receive(:new).
|
121
|
+
with(:delete, fullpath, nil, nil, config.proxy, connection_params).
|
122
|
+
and_return(request)
|
123
|
+
|
94
124
|
expect(ContentGateway::Cache).
|
95
|
-
|
96
|
-
|
97
|
-
|
125
|
+
to receive(:new).
|
126
|
+
with(config, fullpath, :delete, connection_params).
|
127
|
+
and_return(cache)
|
98
128
|
end
|
99
129
|
|
100
130
|
describe "#delete" do
|
@@ -107,106 +137,82 @@ describe ContentGateway::Gateway do
|
|
107
137
|
|
108
138
|
describe "With url generator" do
|
109
139
|
before do
|
110
|
-
expect(url_generator).to receive(:generate).with(path, {}).and_return("url")
|
140
|
+
expect(url_generator).to receive(:generate).at_least(:once).with(path, {}).and_return("url")
|
111
141
|
end
|
112
142
|
|
113
143
|
describe "GET method" do
|
114
144
|
before do
|
115
145
|
expect(ContentGateway::Request).
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
expect(ContentGateway::Cache).
|
120
|
-
to receive(:new).
|
121
|
-
with(config, "url", :get, cache_params).
|
122
|
-
and_return(cache)
|
123
|
-
end
|
146
|
+
to receive(:new).
|
147
|
+
with(:get, "url", headers, nil, config.proxy, cache_params).
|
148
|
+
and_return(request)
|
124
149
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
150
|
+
expect(ContentGateway::Cache).
|
151
|
+
to receive(:new).
|
152
|
+
with(config, "url", :get, cache_params).
|
153
|
+
and_return(cache)
|
129
154
|
end
|
130
155
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
end
|
156
|
+
it_should_behave_like "request" do
|
157
|
+
let(:verb) { "get" }
|
158
|
+
let(:params) { cache_params.merge(headers: headers) }
|
135
159
|
end
|
136
160
|
end
|
137
161
|
|
138
162
|
describe "POST method" do
|
139
163
|
before do
|
140
164
|
expect(ContentGateway::Request).
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
expect(ContentGateway::Cache).
|
145
|
-
to receive(:new).
|
146
|
-
with(config, "url", :post, connection_params).
|
147
|
-
and_return(cache)
|
148
|
-
end
|
165
|
+
to receive(:new).
|
166
|
+
with(:post, "url", nil, payload, config.proxy, connection_params).
|
167
|
+
and_return(request)
|
149
168
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
169
|
+
expect(ContentGateway::Cache).
|
170
|
+
to receive(:new).
|
171
|
+
with(config, "url", :post, connection_params).
|
172
|
+
and_return(cache)
|
154
173
|
end
|
155
174
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
end
|
175
|
+
it_should_behave_like "request" do
|
176
|
+
let(:verb) { "post" }
|
177
|
+
let(:params) { cache_params.merge(payload: payload) }
|
160
178
|
end
|
161
179
|
end
|
162
180
|
|
163
181
|
describe "PUT method" do
|
164
182
|
before do
|
165
183
|
expect(ContentGateway::Request).
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
expect(ContentGateway::Cache).
|
170
|
-
to receive(:new).
|
171
|
-
with(config, "url", :put, connection_params).
|
172
|
-
and_return(cache)
|
173
|
-
end
|
184
|
+
to receive(:new).
|
185
|
+
with(:put, "url", nil, payload, config.proxy, connection_params).
|
186
|
+
and_return(request)
|
174
187
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
188
|
+
expect(ContentGateway::Cache).
|
189
|
+
to receive(:new).
|
190
|
+
with(config, "url", :put, connection_params).
|
191
|
+
and_return(cache)
|
179
192
|
end
|
180
193
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
end
|
194
|
+
it_should_behave_like "request" do
|
195
|
+
let(:verb) { "put" }
|
196
|
+
let(:params) { cache_params.merge(payload: payload) }
|
185
197
|
end
|
186
198
|
end
|
187
199
|
|
188
200
|
describe "DELETE method" do
|
189
201
|
before do
|
190
202
|
expect(ContentGateway::Request).
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
expect(ContentGateway::Cache).
|
195
|
-
to receive(:new).
|
196
|
-
with(config, "url", :delete, connection_params).
|
197
|
-
and_return(cache)
|
198
|
-
end
|
203
|
+
to receive(:new).
|
204
|
+
with(:delete, "url", nil, nil, config.proxy, connection_params).
|
205
|
+
and_return(request)
|
199
206
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
207
|
+
expect(ContentGateway::Cache).
|
208
|
+
to receive(:new).
|
209
|
+
with(config, "url", :delete, connection_params).
|
210
|
+
and_return(cache)
|
204
211
|
end
|
205
212
|
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
end
|
213
|
+
it_should_behave_like "request" do
|
214
|
+
let(:verb) { "delete" }
|
215
|
+
let(:params) { cache_params.merge(payload: payload) }
|
210
216
|
end
|
211
217
|
end
|
212
218
|
end
|
@@ -11,7 +11,7 @@ describe ContentGateway::Request do
|
|
11
11
|
|
12
12
|
let(:client) { double("rest client", execute: "data", url: "/url") }
|
13
13
|
|
14
|
-
let(:request_params) { { method: :get, url: "/url", proxy:
|
14
|
+
let(:request_params) { { method: :get, url: "/url", proxy: nil } }
|
15
15
|
|
16
16
|
describe "#execute" do
|
17
17
|
context "when request is successful" do
|
@@ -156,7 +156,7 @@ describe ContentGateway::Request do
|
|
156
156
|
|
157
157
|
context "when request fails" do
|
158
158
|
it "should return ssl failure error if certificate was not found" do
|
159
|
-
expect { subject_ssl.execute }.to raise_error(ContentGateway::OpenSSLFailure).with_message(
|
159
|
+
expect { subject_ssl.execute }.to raise_error(ContentGateway::OpenSSLFailure).with_message(/^\/url - No such file or directory/)
|
160
160
|
end
|
161
161
|
|
162
162
|
it "should return ssl failure error if certificate cert was not valid" do
|
@@ -171,5 +171,17 @@ describe ContentGateway::Request do
|
|
171
171
|
end
|
172
172
|
end
|
173
173
|
end
|
174
|
+
|
175
|
+
context "when proxy is used" do
|
176
|
+
let(:proxy) { 'http://proxy.test:3128' }
|
177
|
+
subject { ContentGateway::Request.new(:get, "/url", {}, {}, proxy) }
|
178
|
+
let(:request_params) { { method: :get, url: "/url", proxy: proxy } }
|
179
|
+
|
180
|
+
it "should set proxy on RestClient" do
|
181
|
+
expect(subject.execute).to eql "data"
|
182
|
+
expect(RestClient.proxy).to eql(proxy)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
174
186
|
end
|
175
187
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: content_gateway
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Túlio Ornelas
|
@@ -11,93 +11,107 @@ authors:
|
|
11
11
|
- Daniel Martins
|
12
12
|
- Rafael Biriba
|
13
13
|
- Célio Latorraca
|
14
|
-
autorequire:
|
14
|
+
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
|
-
date:
|
17
|
+
date: 2022-02-11 00:00:00.000000000 Z
|
18
18
|
dependencies:
|
19
19
|
- !ruby/object:Gem::Dependency
|
20
20
|
name: activesupport
|
21
21
|
requirement: !ruby/object:Gem::Requirement
|
22
22
|
requirements:
|
23
|
-
- -
|
23
|
+
- - ">="
|
24
24
|
- !ruby/object:Gem::Version
|
25
|
-
version: '
|
25
|
+
version: '3'
|
26
26
|
type: :runtime
|
27
27
|
prerelease: false
|
28
28
|
version_requirements: !ruby/object:Gem::Requirement
|
29
29
|
requirements:
|
30
|
-
- -
|
30
|
+
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '3'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: rest-client
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- -
|
37
|
+
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '
|
39
|
+
version: '2.1'
|
40
40
|
type: :runtime
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- -
|
44
|
+
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
46
|
+
version: '2.1'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: json
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- -
|
51
|
+
- - "~>"
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '0'
|
53
|
+
version: '1.0'
|
54
54
|
type: :runtime
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
|
-
- -
|
58
|
+
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: '0'
|
60
|
+
version: '1.0'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: rspec
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
64
64
|
requirements:
|
65
|
-
- -
|
65
|
+
- - ">="
|
66
66
|
- !ruby/object:Gem::Version
|
67
67
|
version: 2.3.0
|
68
68
|
type: :development
|
69
69
|
prerelease: false
|
70
70
|
version_requirements: !ruby/object:Gem::Requirement
|
71
71
|
requirements:
|
72
|
-
- -
|
72
|
+
- - ">="
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: 2.3.0
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
76
|
name: simplecov
|
77
77
|
requirement: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
|
-
- -
|
79
|
+
- - ">="
|
80
80
|
- !ruby/object:Gem::Version
|
81
81
|
version: 0.7.1
|
82
82
|
type: :development
|
83
83
|
prerelease: false
|
84
84
|
version_requirements: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
|
-
- -
|
86
|
+
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: 0.7.1
|
89
89
|
- !ruby/object:Gem::Dependency
|
90
90
|
name: byebug
|
91
91
|
requirement: !ruby/object:Gem::Requirement
|
92
92
|
requirements:
|
93
|
-
- -
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: rake
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
94
108
|
- !ruby/object:Gem::Version
|
95
109
|
version: '0'
|
96
110
|
type: :development
|
97
111
|
prerelease: false
|
98
112
|
version_requirements: !ruby/object:Gem::Requirement
|
99
113
|
requirements:
|
100
|
-
- -
|
114
|
+
- - ">="
|
101
115
|
- !ruby/object:Gem::Version
|
102
116
|
version: '0'
|
103
117
|
description: An easy way to get external content with two cache levels. The first
|
@@ -114,10 +128,11 @@ executables: []
|
|
114
128
|
extensions: []
|
115
129
|
extra_rdoc_files: []
|
116
130
|
files:
|
117
|
-
- .gitignore
|
118
|
-
- .rspec
|
119
|
-
- .ruby-gemset
|
120
|
-
- .ruby-version
|
131
|
+
- ".gitignore"
|
132
|
+
- ".rspec"
|
133
|
+
- ".ruby-gemset"
|
134
|
+
- ".ruby-version"
|
135
|
+
- ".travis.yml"
|
121
136
|
- Changelog
|
122
137
|
- Gemfile
|
123
138
|
- Gemfile.lock
|
@@ -139,24 +154,24 @@ files:
|
|
139
154
|
homepage: https://github.com/globocom/content-gateway-ruby
|
140
155
|
licenses: []
|
141
156
|
metadata: {}
|
142
|
-
post_install_message:
|
157
|
+
post_install_message:
|
143
158
|
rdoc_options: []
|
144
159
|
require_paths:
|
145
160
|
- lib
|
146
161
|
required_ruby_version: !ruby/object:Gem::Requirement
|
147
162
|
requirements:
|
148
|
-
- -
|
163
|
+
- - ">="
|
149
164
|
- !ruby/object:Gem::Version
|
150
165
|
version: '0'
|
151
166
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
167
|
requirements:
|
153
|
-
- -
|
168
|
+
- - ">="
|
154
169
|
- !ruby/object:Gem::Version
|
155
170
|
version: '0'
|
156
171
|
requirements: []
|
157
|
-
rubyforge_project:
|
158
|
-
rubygems_version: 2.
|
159
|
-
signing_key:
|
172
|
+
rubyforge_project:
|
173
|
+
rubygems_version: 2.5.2.3
|
174
|
+
signing_key:
|
160
175
|
specification_version: 4
|
161
176
|
summary: Content Gateway
|
162
177
|
test_files:
|