content_gateway 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/COPYING +24 -0
- data/Changelog +42 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +61 -0
- data/README.md +134 -0
- data/Rakefile +10 -0
- data/content-gateway.gemspec +26 -0
- data/lib/content_gateway/cache.rb +59 -0
- data/lib/content_gateway/exceptions.rb +79 -0
- data/lib/content_gateway/gateway.rb +153 -0
- data/lib/content_gateway/request.rb +48 -0
- data/lib/content_gateway/version.rb +3 -0
- data/lib/content_gateway.rb +23 -0
- data/spec/integration/content_gateway/gateway_spec.rb +410 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/unit/content_gateway/cache_spec.rb +134 -0
- data/spec/unit/content_gateway/gateway_spec.rb +121 -0
- data/spec/unit/content_gateway/request_spec.rb +105 -0
- metadata +163 -0
@@ -0,0 +1,121 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ContentGateway::Gateway do
|
4
|
+
subject do
|
5
|
+
ContentGateway::Gateway.new("label", config, url_generator)
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:config) { double("config", proxy: nil) }
|
9
|
+
let(:headers) { { "Content-Type" => "application/json" } }
|
10
|
+
let(:payload) { { "data" => 1234 } }
|
11
|
+
let(:url_generator) { double("URL generator") }
|
12
|
+
let(:path) { "/api/test.json" }
|
13
|
+
let(:cache) { double("cache", use?: false, status: "HIT") }
|
14
|
+
let(:request) { double("request", execute: data) }
|
15
|
+
let(:data) { '{"param": "value"}' }
|
16
|
+
let(:cache_params) { { timeout: 2, expires_in: 30, stale_expires_in: 180, skip_cache: false } }
|
17
|
+
|
18
|
+
before do
|
19
|
+
allow(url_generator).to receive(:generate).and_return("url")
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "GET method" do
|
23
|
+
before do
|
24
|
+
expect(ContentGateway::Request).
|
25
|
+
to receive(:new).
|
26
|
+
with(:get, "url", headers, nil, config.proxy).
|
27
|
+
and_return(request)
|
28
|
+
expect(ContentGateway::Cache).
|
29
|
+
to receive(:new).
|
30
|
+
with(config, "url", :get, cache_params).
|
31
|
+
and_return(cache)
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#get" do
|
35
|
+
it "should do a get request passing the correct parameters" do
|
36
|
+
expect(subject.get(path, cache_params.merge(headers: headers))).to eql data
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#get_json" do
|
41
|
+
it "should parse the response as JSON" do
|
42
|
+
expect(subject.get_json(path, cache_params.merge(headers: headers))).to eql JSON.parse(data)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "POST method" do
|
48
|
+
before do
|
49
|
+
expect(ContentGateway::Request).
|
50
|
+
to receive(:new).
|
51
|
+
with(:post, "url", nil, payload, config.proxy).
|
52
|
+
and_return(request)
|
53
|
+
expect(ContentGateway::Cache).
|
54
|
+
to receive(:new).
|
55
|
+
with(config, "url", :post, cache_params).
|
56
|
+
and_return(cache)
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#post" do
|
60
|
+
it "should do a post request passing the correct parameters" do
|
61
|
+
expect(subject.post(path, cache_params.merge(payload: payload))).to eql data
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#post_json" do
|
66
|
+
it "should parse the response as JSON" do
|
67
|
+
expect(subject.post_json(path, cache_params.merge(payload: payload))).to eql JSON.parse(data)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "PUT method" do
|
73
|
+
before do
|
74
|
+
expect(ContentGateway::Request).
|
75
|
+
to receive(:new).
|
76
|
+
with(:put, "url", nil, payload, config.proxy).
|
77
|
+
and_return(request)
|
78
|
+
expect(ContentGateway::Cache).
|
79
|
+
to receive(:new).
|
80
|
+
with(config, "url", :put, cache_params).
|
81
|
+
and_return(cache)
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "#put" do
|
85
|
+
it "should do a put request passing the correct parameters" do
|
86
|
+
expect(subject.put(path, cache_params.merge(payload: payload))).to eql data
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "#put_json" do
|
91
|
+
it "should parse the response as JSON" do
|
92
|
+
expect(subject.put_json(path, cache_params.merge(payload: payload))).to eql JSON.parse(data)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "DELETE method" do
|
98
|
+
before do
|
99
|
+
expect(ContentGateway::Request).
|
100
|
+
to receive(:new).
|
101
|
+
with(:delete, "url", nil, payload, config.proxy).
|
102
|
+
and_return(request)
|
103
|
+
expect(ContentGateway::Cache).
|
104
|
+
to receive(:new).
|
105
|
+
with(config, "url", :delete, cache_params).
|
106
|
+
and_return(cache)
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "#delete" do
|
110
|
+
it "should do a delete request passing the correct parameters" do
|
111
|
+
expect(subject.delete(path, cache_params.merge(payload: payload))).to eql data
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "#delete_json" do
|
116
|
+
it "should parse the response as JSON" do
|
117
|
+
expect(subject.delete_json(path, cache_params.merge(payload: payload))).to eql JSON.parse(data)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ContentGateway::Request do
|
4
|
+
subject do
|
5
|
+
ContentGateway::Request.new(:get, "/url")
|
6
|
+
end
|
7
|
+
|
8
|
+
before do
|
9
|
+
allow(RestClient::Request).to receive(:new).with(request_params).and_return(client)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:client) { double("rest client", execute: "data", url: "/url") }
|
13
|
+
|
14
|
+
let(:request_params) { { method: :get, url: "/url", proxy: :none } }
|
15
|
+
|
16
|
+
describe "#execute" do
|
17
|
+
context "when request is successful" do
|
18
|
+
it "should return request data" do
|
19
|
+
expect(subject.execute).to eql "data"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "when request fails" do
|
24
|
+
context "with RestClient::ResourceNotFound exception" do
|
25
|
+
before do
|
26
|
+
expect(client).to receive(:execute).and_raise(RestClient::ResourceNotFound)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should raise ContentGateway::ResourceNotFound" do
|
30
|
+
expect { subject.execute }.to raise_error ContentGateway::ResourceNotFound
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "with RestClient::Unauthorized exception" do
|
35
|
+
before do
|
36
|
+
expect(client).to receive(:execute).and_raise(RestClient::Unauthorized)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should raise ContentGateway::UnauthorizedError" do
|
40
|
+
expect { subject.execute }.to raise_error ContentGateway::UnauthorizedError
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "with RestClient::UnprocessableEntity exception" do
|
45
|
+
before do
|
46
|
+
expect(client).to receive(:execute).and_raise(RestClient::UnprocessableEntity)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should raise ContentGateway::ValidationError" do
|
50
|
+
expect { subject.execute }.to raise_error ContentGateway::ValidationError
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "with RestClient::Forbidden exception" do
|
55
|
+
before do
|
56
|
+
expect(client).to receive(:execute).and_raise(RestClient::Forbidden)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should raise ContentGateway::Forbidden" do
|
60
|
+
expect { subject.execute }.to raise_error ContentGateway::Forbidden
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "with RestClient::Conflict exception" do
|
65
|
+
before do
|
66
|
+
expect(client).to receive(:execute).and_raise(RestClient::Conflict)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should raise ContentGateway::ConflictError" do
|
70
|
+
expect { subject.execute }.to raise_error ContentGateway::ConflictError
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "with a 5xx error" do
|
75
|
+
before do
|
76
|
+
expect(client).to receive(:execute).and_raise(RestClient::Exception.new(nil, 502))
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should raise ContentGateway::ServerError" do
|
80
|
+
expect { subject.execute }.to raise_error ContentGateway::ServerError
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "with other error codes from RestClient" do
|
85
|
+
before do
|
86
|
+
expect(client).to receive(:execute).and_raise(RestClient::Exception.new(nil, 418))
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should raise the original exception" do
|
90
|
+
expect { subject.execute }.to raise_error RestClient::Exception
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context "with unmapped exceptions" do
|
95
|
+
before do
|
96
|
+
expect(client).to receive(:execute).and_raise(StandardError)
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should raise ContentGateway::ConnectionFailure" do
|
100
|
+
expect { subject.execute }.to raise_error ContentGateway::ConnectionFailure
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: content_gateway
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Emerson Macedo
|
8
|
+
- Guilherme Garnier
|
9
|
+
- Daniel Martins
|
10
|
+
- Rafael Biriba
|
11
|
+
- Célio Latorraca
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
date: 2014-10-21 00:00:00.000000000 Z
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: activesupport
|
19
|
+
requirement: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - '>='
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '0'
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: rest-client
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: json
|
47
|
+
requirement: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
type: :runtime
|
53
|
+
prerelease: false
|
54
|
+
version_requirements: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rspec
|
61
|
+
requirement: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 2.3.0
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 2.3.0
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: simplecov
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 0.7.1
|
80
|
+
type: :development
|
81
|
+
prerelease: false
|
82
|
+
version_requirements: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 0.7.1
|
87
|
+
- !ruby/object:Gem::Dependency
|
88
|
+
name: byebug
|
89
|
+
requirement: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
type: :development
|
95
|
+
prerelease: false
|
96
|
+
version_requirements: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
description: An easy way to get external content with two cache levels. The first
|
102
|
+
is a performance cache and second is the stale
|
103
|
+
email:
|
104
|
+
- emerleite@gmail.com
|
105
|
+
- guilherme.garnier@gmail.com
|
106
|
+
- daniel.tritone@gmail.com
|
107
|
+
- biribarj@gmail.com
|
108
|
+
- celio.la@gmail.com
|
109
|
+
executables: []
|
110
|
+
extensions: []
|
111
|
+
extra_rdoc_files: []
|
112
|
+
files:
|
113
|
+
- .gitignore
|
114
|
+
- .rspec
|
115
|
+
- .ruby-gemset
|
116
|
+
- .ruby-version
|
117
|
+
- COPYING
|
118
|
+
- Changelog
|
119
|
+
- Gemfile
|
120
|
+
- Gemfile.lock
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- content-gateway.gemspec
|
124
|
+
- lib/content_gateway.rb
|
125
|
+
- lib/content_gateway/cache.rb
|
126
|
+
- lib/content_gateway/exceptions.rb
|
127
|
+
- lib/content_gateway/gateway.rb
|
128
|
+
- lib/content_gateway/request.rb
|
129
|
+
- lib/content_gateway/version.rb
|
130
|
+
- spec/integration/content_gateway/gateway_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
- spec/unit/content_gateway/cache_spec.rb
|
133
|
+
- spec/unit/content_gateway/gateway_spec.rb
|
134
|
+
- spec/unit/content_gateway/request_spec.rb
|
135
|
+
homepage: ''
|
136
|
+
licenses: []
|
137
|
+
metadata: {}
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - '>='
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - '>='
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 2.4.2
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: Content Gateway
|
158
|
+
test_files:
|
159
|
+
- spec/integration/content_gateway/gateway_spec.rb
|
160
|
+
- spec/spec_helper.rb
|
161
|
+
- spec/unit/content_gateway/cache_spec.rb
|
162
|
+
- spec/unit/content_gateway/gateway_spec.rb
|
163
|
+
- spec/unit/content_gateway/request_spec.rb
|