aptible-resource 0.3.3 → 0.3.4

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: 82f938de116c53f45b2033e06ae7a293aca0c0f7
4
- data.tar.gz: dc977779622c227070d3cd09f58c91d9883baa8f
3
+ metadata.gz: 3f853b404f5592c14f4604e6338d0c6f6e563540
4
+ data.tar.gz: 749de9d4d56f4c106595206d1fe287ec0151331d
5
5
  SHA512:
6
- metadata.gz: 8d37e852b758d7e3300bca70bbc8c80338fdb0ef489199f3fb965ed4c8a5ec6b0a063f5091b349d556d9fdc2797e15529ada77caa121736c8874e58a3d91c73e
7
- data.tar.gz: 7191b315d2d20abf449f3215e7b2907e77b0f67a2d65631b2c0cce686b5a2bebeb72a44ee1d4b91d0674137b04778d7da4d8d3d7146f54657b37ae1e0a31bc00
6
+ metadata.gz: eaedd3c259edab267e029a7b5a908b6dad516561cd0a884775f4a993cb249414b2cf34535fbfdc8c3ab85ca656bfe7a0ae5d71f0f5fa8cd8892336b1a439ab9f
7
+ data.tar.gz: a5358768fbac108d5863074a0b13d0fb42e4febc13efc9cb1402118ed12b7e7dd8f3cb560c45cc7bfb31415c91ad5e0a87064ee37edc5b3350a73122f4fea026
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  # HyperResource dependencies
23
23
  spec.add_dependency 'uri_template', '>= 0.5.2'
24
- spec.add_dependency 'faraday', '>= 0.8.6'
24
+ spec.add_dependency 'faraday', '~> 0.9.2'
25
25
  spec.add_dependency 'json'
26
26
 
27
27
  spec.add_dependency 'fridge'
@@ -32,4 +32,5 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency 'rake'
33
33
  spec.add_development_dependency 'rspec', '~> 2.0'
34
34
  spec.add_development_dependency 'pry'
35
+ spec.add_development_dependency 'webmock'
35
36
  end
@@ -12,6 +12,9 @@ require 'aptible/resource/adapter'
12
12
  require 'aptible/resource/errors'
13
13
  require 'aptible/resource/boolean'
14
14
 
15
+ # Open errors that make sense
16
+ require 'aptible/resource/ext/faraday'
17
+
15
18
  module Aptible
16
19
  module Resource
17
20
  # rubocop:disable ClassLength
@@ -41,9 +44,9 @@ module Aptible
41
44
  return [] unless resource
42
45
  if resource.links.key?('next')
43
46
  options[:href] = resource.links['next'].href
44
- resource.send(basename).entries + all(options)
47
+ resource.entries + all(options)
45
48
  else
46
- resource.send(basename).entries
49
+ resource.entries
47
50
  end
48
51
  end
49
52
  # rubocop: enable AbcSize
@@ -126,7 +129,9 @@ module Aptible
126
129
  if (memoized = instance_variable_get("@#{relation}"))
127
130
  memoized
128
131
  elsif links[relation]
129
- instance_variable_set("@#{relation}", links[relation].entries)
132
+ depaginated = self.class.all(href: links[relation].base_href,
133
+ headers: headers)
134
+ instance_variable_set("@#{relation}", depaginated)
130
135
  end
131
136
  end
132
137
  end
@@ -0,0 +1,5 @@
1
+ Faraday::Adapter::NetHttp.class_eval do |cls|
2
+ # Work around https://github.com/lostisland/faraday/issues/561 by treating
3
+ # connection timeouts as... timeouts.
4
+ cls::NET_HTTP_EXCEPTIONS.delete Net::OpenTimeout if defined?(Net::OpenTimeout)
5
+ end
@@ -1,5 +1,5 @@
1
1
  module Aptible
2
2
  module Resource
3
- VERSION = '0.3.3'
3
+ VERSION = '0.3.4'
4
4
  end
5
5
  end
@@ -78,12 +78,18 @@ class HyperResource
78
78
  }.to_json)
79
79
  return Thread.current[key] if Thread.current[key]
80
80
 
81
- fc = Faraday.new(self.faraday_options.merge(:url => url))
82
- fc.headers.merge!('User-Agent' => "HyperResource #{HyperResource::VERSION}")
83
- fc.headers.merge!(self.headers || {})
84
- if ba=self.auth[:basic]
85
- fc.basic_auth(*ba)
81
+ fc = Faraday.new(self.faraday_options.merge(:url => url)) do |builder|
82
+ builder.headers.merge!('User-Agent' => "HyperResource #{HyperResource::VERSION}")
83
+ builder.headers.merge!(self.headers || {})
84
+ if (ba = self.auth[:basic])
85
+ builder.basic_auth(*ba)
86
+ end
87
+
88
+ builder.request :url_encoded
89
+ builder.request :retry
90
+ builder.adapter Faraday.default_adapter
86
91
  end
92
+
87
93
  Thread.current[key] = fc
88
94
  end
89
95
 
@@ -31,7 +31,7 @@ describe Aptible::Resource::Base do
31
31
  let(:collection) { double 'Api' }
32
32
 
33
33
  before do
34
- collection.stub(:mainframes) { [mainframe] }
34
+ collection.stub(:entries) { [mainframe] }
35
35
  collection.stub(:links) { Hash.new }
36
36
  Api::Mainframe.any_instance.stub(:find_by_url) { collection }
37
37
  end
@@ -51,7 +51,7 @@ describe Aptible::Resource::Base do
51
51
  Api::Mainframe.all(options)
52
52
  end
53
53
 
54
- describe 'when paginated' do
54
+ context 'when paginated' do
55
55
  before do
56
56
  page = double('page')
57
57
  allow(page).to receive(:href).and_return(
@@ -210,6 +210,8 @@ describe Aptible::Resource::Base do
210
210
  before { Api.has_many :mainframes }
211
211
  before { subject.stub(:loaded) { true } }
212
212
  before { subject.stub(:links) { { mainframes: mainframes_link } } }
213
+ before { mainframes_link.stub(:entries) { [mainframe] } }
214
+ before { mainframes_link.stub(:base_href) { '/mainframes' } }
213
215
 
214
216
  describe '#create_#{relation}' do
215
217
  it 'should populate #errors in the event of an error' do
@@ -253,6 +255,14 @@ describe Aptible::Resource::Base do
253
255
  expect(subject.create_mainframe!({})).to eq mainframe
254
256
  end
255
257
  end
258
+
259
+ describe '#{relation}s' do
260
+ it 'should defer to self.class.all' do
261
+ expect(subject.class).to receive(:all).with(href: '/mainframes',
262
+ headers: subject.headers)
263
+ subject.mainframes
264
+ end
265
+ end
256
266
  end
257
267
 
258
268
  context '.field' do
@@ -1,17 +1,49 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Aptible::Resource::Base, slow: true do
4
- subject { Api.new(root: 'http://10.255.255.1/') }
5
-
6
- it 'should time out by default after a reasonable delay' do
7
- # Faraday throws different kinds of errors depending on whether
8
- # Net::OpenTimeout is defined, so let's check for this
9
- # https://github.com/lostisland/faraday/issues/561
10
- e = Faraday::Error::TimeoutError
11
- e = Faraday::Error::ConnectionFailed if defined? Net::OpenTimeout
12
-
13
- expect do
14
- Timeout.timeout(15) { subject.all }
15
- end.to raise_error(e, 'execution expired')
3
+ # With webmock (fake connections), to check how we handle timeouts.
4
+ describe Aptible::Resource::Base do
5
+ let(:body) { { 'hello' => '1' } }
6
+ let(:json_body) { JSON.unparse(body) }
7
+ let(:domain) { 'api.aptible.com' }
8
+
9
+ subject { Api.new(root: "http://#{domain}") }
10
+
11
+ context 'with mock connections' do
12
+ around do |example|
13
+ WebMock.disable_net_connect!
14
+ example.run
15
+ WebMock.allow_net_connect!
16
+ end
17
+
18
+ it 'should retry timeout errors' do
19
+ stub_request(:get, domain)
20
+ .to_timeout.then
21
+ .to_timeout.then
22
+ .to_return(body: json_body)
23
+
24
+ expect(subject.get.body).to eq(body)
25
+ end
26
+
27
+ it 'should not retry POSTs' do
28
+ stub_request(:post, domain)
29
+ .to_timeout.then
30
+ .to_return(body: json_body)
31
+
32
+ expect { subject.post }.to raise_error(Faraday::TimeoutError)
33
+ end
34
+ end
35
+
36
+ context 'without connections' do
37
+ it 'default to 10 seconds of timeout and retry 3 times' do
38
+ # This really relies on how exactly MRI implements Net::HTTP open timeouts
39
+ skip 'MRI implementation-specific' if RUBY_PLATFORM == 'java'
40
+
41
+ expect(Timeout).to receive(:timeout)
42
+ .with(10, Net::OpenTimeout)
43
+ .exactly(3).times
44
+ .and_raise(Net::OpenTimeout)
45
+
46
+ expect { subject.all }.to raise_error(Faraday::Error::TimeoutError)
47
+ end
16
48
  end
17
49
  end
data/spec/spec_helper.rb CHANGED
@@ -12,3 +12,7 @@ end
12
12
 
13
13
  # Require library up front
14
14
  require 'aptible/resource'
15
+
16
+ # Webmock
17
+ require 'webmock/rspec'
18
+ WebMock.allow_net_connect!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aptible-resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank Macreery
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-22 00:00:00.000000000 Z
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uri_template
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: faraday
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.8.6
33
+ version: 0.9.2
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.8.6
40
+ version: 0.9.2
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: json
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: webmock
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
153
167
  description: Foundation classes for Aptible resource server gems
154
168
  email:
155
169
  - frank@macreery.com
@@ -171,6 +185,7 @@ files:
171
185
  - lib/aptible/resource/base.rb
172
186
  - lib/aptible/resource/boolean.rb
173
187
  - lib/aptible/resource/errors.rb
188
+ - lib/aptible/resource/ext/faraday.rb
174
189
  - lib/aptible/resource/version.rb
175
190
  - lib/hyper_resource.rb
176
191
  - lib/hyper_resource/adapter.rb
@@ -210,7 +225,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
225
  version: '0'
211
226
  requirements: []
212
227
  rubyforge_project:
213
- rubygems_version: 2.2.2
228
+ rubygems_version: 2.4.5
214
229
  signing_key:
215
230
  specification_version: 4
216
231
  summary: Foundation classes for Aptible resource server gems