content_gateway 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fa85efc4f1c83ec7bdd1a6759fb1fda12afcbea4
4
- data.tar.gz: aab412e14cccaac8e3110a82b9067e95dc5c034a
3
+ metadata.gz: ee3e2f6da92f839d6d66363fb6860fe51ca61ff1
4
+ data.tar.gz: 25fcaa57e51bf4f365133c4d5672ce93cb1076e9
5
5
  SHA512:
6
- metadata.gz: 89ce05e82342e19a45d4cdf54bcbd065d6ee2f897230d5af0aaecad301b636c455fa4f531fc2dc5abe08fffa0bdb522d7d8c26383b5acfd32942459fd1e58241
7
- data.tar.gz: bd179384af08df8c163fd8e2e32509bff4dcdbcf8aed44192dd27b5c039114edceec71f3018d9b759860b728086e61b0adcb547d7f20aab0ebf2e9e5d63f5641
6
+ metadata.gz: 4e95aa8acf647aaa56102fcc5e28ca6fab2f46d0885d3353d9f785003f6a4848d6ee367cbbc2d79a7ef78679e20f0f296d51f2e889ef29dc4515a6d8e21a2db5
7
+ data.tar.gz: 79995262957697314650059639726c15d474a199c59c9f96ee377ceadaa9b2c38a0ebbc930c5eededd0ded68f8908112419a9b0ef985919581b2c48389270881
data/Changelog CHANGED
@@ -1,10 +1,17 @@
1
+ 2015-07-24 [0.5.0]
2
+
3
+ * Mapping JSON parse errors to ContentGateway::ParserError
4
+
1
5
  2015-06-16 [0.4.0]
6
+
2
7
  * Adding ssl_version support to ssl_certificate hash
3
8
 
4
9
  2015-01-05 [0.3.0]
10
+
5
11
  * 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
12
 
7
13
  2014-11-06 [0.2.1]
14
+
8
15
  * Fix: Do not send connection parameters to url generator. Closes #2
9
16
  * Fix: Send http headers to request object. Closes #3
10
17
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- content_gateway (0.3.0)
4
+ content_gateway (0.5.0)
5
5
  activesupport
6
6
  json
7
7
  rest-client
@@ -9,13 +9,12 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activesupport (4.0.2)
13
- i18n (~> 0.6, >= 0.6.4)
14
- minitest (~> 4.2)
15
- multi_json (~> 1.3)
12
+ activesupport (4.1.8)
13
+ i18n (~> 0.6, >= 0.6.9)
14
+ json (~> 1.7, >= 1.7.7)
15
+ minitest (~> 5.1)
16
16
  thread_safe (~> 0.1)
17
- tzinfo (~> 0.3.37)
18
- atomic (1.1.14)
17
+ tzinfo (~> 1.1)
19
18
  byebug (3.5.1)
20
19
  columnize (~> 0.8)
21
20
  debugger-linecache (~> 1.2)
@@ -23,13 +22,15 @@ GEM
23
22
  columnize (0.8.9)
24
23
  debugger-linecache (1.2.0)
25
24
  diff-lcs (1.2.5)
26
- i18n (0.6.9)
25
+ i18n (0.6.11)
27
26
  json (1.8.1)
28
- mime-types (2.0)
29
- minitest (4.7.5)
27
+ mime-types (2.4.3)
28
+ minitest (5.5.0)
30
29
  multi_json (1.7.9)
31
- rest-client (1.6.7)
32
- mime-types (>= 1.16)
30
+ netrc (0.10.2)
31
+ rest-client (1.7.2)
32
+ mime-types (>= 1.16, < 3.0)
33
+ netrc (~> 0.7)
33
34
  rspec (3.1.0)
34
35
  rspec-core (~> 3.1.0)
35
36
  rspec-expectations (~> 3.1.0)
@@ -47,9 +48,9 @@ GEM
47
48
  simplecov-html (~> 0.7.1)
48
49
  simplecov-html (0.7.1)
49
50
  slop (3.6.0)
50
- thread_safe (0.1.3)
51
- atomic
52
- tzinfo (0.3.38)
51
+ thread_safe (0.3.4)
52
+ tzinfo (1.2.2)
53
+ thread_safe (~> 0.1)
53
54
 
54
55
  PLATFORMS
55
56
  ruby
@@ -81,5 +81,7 @@ module ContentGateway
81
81
  end
82
82
  end
83
83
 
84
+ class ParserError < BaseError; end
85
+
84
86
  class StaleCacheNotAvailableError < StandardError; end
85
87
  end
@@ -88,19 +88,19 @@ module ContentGateway
88
88
  end
89
89
 
90
90
  def get_json(resource_path, params = {})
91
- JSON.parse get(resource_path, params)
91
+ json_request(:get, resource_path, params)
92
92
  end
93
93
 
94
94
  def post_json(resource_path, params = {})
95
- JSON.parse post(resource_path, params)
95
+ json_request(:post, resource_path, params)
96
96
  end
97
97
 
98
98
  def put_json(resource_path, params = {})
99
- JSON.parse put(resource_path, params)
99
+ json_request(:put, resource_path, params)
100
100
  end
101
101
 
102
102
  def delete_json(resource_path, params = {})
103
- JSON.parse delete(resource_path, params)
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 }
@@ -1,3 +1,3 @@
1
1
  module ContentGateway
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -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,17 +28,41 @@ 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
32
56
  before do
33
57
  expect(ContentGateway::Request).
34
- to receive(:new).
35
- with(:get, fullpath, headers, nil, config.proxy, cache_params).
36
- and_return(request)
58
+ to receive(:new).
59
+ with(:get, fullpath, headers, nil, config.proxy, cache_params).
60
+ and_return(request)
61
+
37
62
  expect(ContentGateway::Cache).
38
- to receive(:new).
39
- with(config, fullpath, :get, cache_params).
40
- and_return(cache)
63
+ to receive(:new).
64
+ with(config, fullpath, :get, cache_params).
65
+ and_return(cache)
41
66
  end
42
67
 
43
68
  describe "#get" do
@@ -50,13 +75,14 @@ describe ContentGateway::Gateway do
50
75
  describe "POST method" do
51
76
  before do
52
77
  expect(ContentGateway::Request).
53
- to receive(:new).
54
- with(:post, fullpath, nil, payload, config.proxy, connection_params).
55
- and_return(request)
78
+ to receive(:new).
79
+ with(:post, fullpath, nil, payload, config.proxy, connection_params).
80
+ and_return(request)
81
+
56
82
  expect(ContentGateway::Cache).
57
- to receive(:new).
58
- with(config, fullpath, :post, connection_params).
59
- and_return(cache)
83
+ to receive(:new).
84
+ with(config, fullpath, :post, connection_params).
85
+ and_return(cache)
60
86
  end
61
87
 
62
88
  describe "#post" do
@@ -69,13 +95,14 @@ describe ContentGateway::Gateway do
69
95
  describe "PUT method" do
70
96
  before do
71
97
  expect(ContentGateway::Request).
72
- to receive(:new).
73
- with(:put, fullpath, nil, payload, config.proxy, connection_params).
74
- and_return(request)
98
+ to receive(:new).
99
+ with(:put, fullpath, nil, payload, config.proxy, connection_params).
100
+ and_return(request)
101
+
75
102
  expect(ContentGateway::Cache).
76
- to receive(:new).
77
- with(config, fullpath, :put, connection_params).
78
- and_return(cache)
103
+ to receive(:new).
104
+ with(config, fullpath, :put, connection_params).
105
+ and_return(cache)
79
106
  end
80
107
 
81
108
  describe "#put" do
@@ -88,13 +115,14 @@ describe ContentGateway::Gateway do
88
115
  describe "DELETE method" do
89
116
  before do
90
117
  expect(ContentGateway::Request).
91
- to receive(:new).
92
- with(:delete, fullpath, nil, nil, config.proxy, connection_params).
93
- and_return(request)
118
+ to receive(:new).
119
+ with(:delete, fullpath, nil, nil, config.proxy, connection_params).
120
+ and_return(request)
121
+
94
122
  expect(ContentGateway::Cache).
95
- to receive(:new).
96
- with(config, fullpath, :delete, connection_params).
97
- and_return(cache)
123
+ to receive(:new).
124
+ with(config, fullpath, :delete, connection_params).
125
+ and_return(cache)
98
126
  end
99
127
 
100
128
  describe "#delete" do
@@ -107,106 +135,82 @@ describe ContentGateway::Gateway do
107
135
 
108
136
  describe "With url generator" do
109
137
  before do
110
- expect(url_generator).to receive(:generate).with(path, {}).and_return("url")
138
+ expect(url_generator).to receive(:generate).at_least(:once).with(path, {}).and_return("url")
111
139
  end
112
140
 
113
141
  describe "GET method" do
114
142
  before do
115
143
  expect(ContentGateway::Request).
116
- to receive(:new).
117
- with(:get, "url", headers, nil, config.proxy, cache_params).
118
- and_return(request)
119
- expect(ContentGateway::Cache).
120
- to receive(:new).
121
- with(config, "url", :get, cache_params).
122
- and_return(cache)
123
- end
144
+ to receive(:new).
145
+ with(:get, "url", headers, nil, config.proxy, cache_params).
146
+ and_return(request)
124
147
 
125
- describe "#get" do
126
- it "should do a get request passing the correct parameters" do
127
- expect(subject.get(path, cache_params.merge(headers: headers))).to eql data
128
- end
148
+ expect(ContentGateway::Cache).
149
+ to receive(:new).
150
+ with(config, "url", :get, cache_params).
151
+ and_return(cache)
129
152
  end
130
153
 
131
- describe "#get_json" do
132
- it "should parse the response as JSON" do
133
- expect(subject.get_json(path, cache_params.merge(headers: headers))).to eql JSON.parse(data)
134
- end
154
+ it_should_behave_like "request" do
155
+ let(:verb) { "get" }
156
+ let(:params) { cache_params.merge(headers: headers) }
135
157
  end
136
158
  end
137
159
 
138
160
  describe "POST method" do
139
161
  before do
140
162
  expect(ContentGateway::Request).
141
- to receive(:new).
142
- with(:post, "url", nil, payload, config.proxy, connection_params).
143
- and_return(request)
163
+ to receive(:new).
164
+ with(:post, "url", nil, payload, config.proxy, connection_params).
165
+ and_return(request)
166
+
144
167
  expect(ContentGateway::Cache).
145
- to receive(:new).
146
- with(config, "url", :post, connection_params).
147
- and_return(cache)
168
+ to receive(:new).
169
+ with(config, "url", :post, connection_params).
170
+ and_return(cache)
148
171
  end
149
172
 
150
- describe "#post" do
151
- it "should do a post request passing the correct parameters" do
152
- expect(subject.post(path, cache_params.merge(payload: payload))).to eql data
153
- end
154
- end
155
-
156
- describe "#post_json" do
157
- it "should parse the response as JSON" do
158
- expect(subject.post_json(path, cache_params.merge(payload: payload))).to eql JSON.parse(data)
159
- end
173
+ it_should_behave_like "request" do
174
+ let(:verb) { "post" }
175
+ let(:params) { cache_params.merge(payload: payload) }
160
176
  end
161
177
  end
162
178
 
163
179
  describe "PUT method" do
164
180
  before do
165
181
  expect(ContentGateway::Request).
166
- to receive(:new).
167
- with(:put, "url", nil, payload, config.proxy, connection_params).
168
- and_return(request)
169
- expect(ContentGateway::Cache).
170
- to receive(:new).
171
- with(config, "url", :put, connection_params).
172
- and_return(cache)
173
- end
182
+ to receive(:new).
183
+ with(:put, "url", nil, payload, config.proxy, connection_params).
184
+ and_return(request)
174
185
 
175
- describe "#put" do
176
- it "should do a put request passing the correct parameters" do
177
- expect(subject.put(path, cache_params.merge(payload: payload))).to eql data
178
- end
186
+ expect(ContentGateway::Cache).
187
+ to receive(:new).
188
+ with(config, "url", :put, connection_params).
189
+ and_return(cache)
179
190
  end
180
191
 
181
- describe "#put_json" do
182
- it "should parse the response as JSON" do
183
- expect(subject.put_json(path, cache_params.merge(payload: payload))).to eql JSON.parse(data)
184
- end
192
+ it_should_behave_like "request" do
193
+ let(:verb) { "put" }
194
+ let(:params) { cache_params.merge(payload: payload) }
185
195
  end
186
196
  end
187
197
 
188
198
  describe "DELETE method" do
189
199
  before do
190
200
  expect(ContentGateway::Request).
191
- to receive(:new).
192
- with(:delete, "url", nil, nil, config.proxy, connection_params).
193
- and_return(request)
194
- expect(ContentGateway::Cache).
195
- to receive(:new).
196
- with(config, "url", :delete, connection_params).
197
- and_return(cache)
198
- end
201
+ to receive(:new).
202
+ with(:delete, "url", nil, nil, config.proxy, connection_params).
203
+ and_return(request)
199
204
 
200
- describe "#delete" do
201
- it "should do a delete request passing the correct parameters" do
202
- expect(subject.delete(path, cache_params.merge(payload: payload))).to eql data
203
- end
205
+ expect(ContentGateway::Cache).
206
+ to receive(:new).
207
+ with(config, "url", :delete, connection_params).
208
+ and_return(cache)
204
209
  end
205
210
 
206
- describe "#delete_json" do
207
- it "should parse the response as JSON" do
208
- expect(subject.delete_json(path, cache_params.merge(payload: payload))).to eql JSON.parse(data)
209
- end
211
+ it_should_behave_like "request" do
212
+ let(:verb) { "delete" }
213
+ let(:params) { cache_params.merge(payload: payload) }
210
214
  end
211
215
  end
212
216
  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.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Túlio Ornelas
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2015-06-16 00:00:00.000000000 Z
17
+ date: 2015-07-24 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: activesupport