droplet_kit 1.1.2 → 1.1.3

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: 9a7043fefd42f5297c3f91fa3033c30a47ff86fd
4
- data.tar.gz: 2dfc49f1230133e864b1b9373a6caee0c7b9c5e3
3
+ metadata.gz: 6e28942bdd478cd811a6f8e8aa0ee9632b5d4f50
4
+ data.tar.gz: 96fca5ad6cb96e1e4c58f0d4bc513bcc770e05a7
5
5
  SHA512:
6
- metadata.gz: da1218af357c9a2c1ef5cd2650d583cd2899b4895760747438054e8d353de5c1479a186427ef3711c2f541333d189a19d6c5fc7aff193cd0b08393018514f004
7
- data.tar.gz: f37b970dd2c05bad03039fbaadd98736c70e4c889014f9e01a5ddcdf11d3b5150196ba7a6da908f028b104c7232b8089d4b2d3cf3e2b32f0500a8b9032300364
6
+ metadata.gz: 1efaaffe2af2dd9f717e09c61742a16fb6c8c3bf3602e67164550bd3c7842cf26b7b7d35165a8447c366ecc97528c22e65c86b399c7a9204a2284d9ebb6ed65f
7
+ data.tar.gz: 32839b6b91778d2414440ecb9de9b8aad17a1306525d55226fe462b15abcbfc3287233858eea2672568b1beccb8d8782dcc38ef4830a0a88e8a24a94c907ad6b
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'simplecov'
3
4
  # Specify your gem's dependencies in droplet_kit.gemspec
4
5
  gemspec
data/README.md CHANGED
@@ -121,6 +121,12 @@ Actions supported:
121
121
 
122
122
  client = DropletKit::Client.new(access_token: 'TOKEN')
123
123
  client.domain_records #=> DropletKit::DomainRecordResource
124
+ domain_record = DropletKit::DomainRecord.new(
125
+ type: 'CNAME',
126
+ name: 'www',
127
+ data: '@'
128
+ )
129
+
124
130
 
125
131
  Actions supported:
126
132
 
@@ -144,6 +150,18 @@ Actions supported:
144
150
  * `client.images.update(image, id: 'id')`
145
151
 
146
152
 
153
+ ## Image Action Resource
154
+
155
+ client = DropletKit::Client.new(access_token: 'TOKEN')
156
+ client.image_actions #=> DropletKit::ImageActionResource
157
+
158
+ Image Actions Supported:
159
+
160
+ * `client.image_actions.all(image_id: 123)`
161
+ * `client.image_actions.find(image_id: 123, id: 123455)`
162
+ * `client.image_actions.transfer(image_id: 123, region: 'nyc3')`
163
+
164
+
147
165
  ## Region resource
148
166
 
149
167
  client = DropletKit::Client.new(access_token: 'TOKEN')
data/droplet_kit.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = DropletKit::VERSION
9
9
  spec.authors = ["Robert Ross"]
10
10
  spec.email = ["rross@digitalocean.com"]
11
- spec.summary = %q{Droplet Kit is the official Ruby library for Digital Ocean's API}
12
- spec.description = %q{Droplet Kit is the official Ruby library for Digital Ocean's API}
11
+ spec.summary = %q{Droplet Kit is the official Ruby library for DigitalOcean's API}
12
+ spec.description = %q{Droplet Kit is the official Ruby library for DigitalOcean's API}
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
15
15
 
@@ -17,13 +17,14 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
-
20
+
21
21
  spec.required_ruby_version = '>= 2.0.0'
22
22
 
23
23
  spec.add_dependency 'virtus', '~> 1.0.3'
24
24
  spec.add_dependency "resource_kit", '~> 0.1.1'
25
25
  spec.add_dependency "kartograph", '~> 0.2.0'
26
26
  spec.add_dependency "activesupport", '~> 4.1.6'
27
+ spec.add_dependency "faraday", '~> 0.9.1'
27
28
 
28
29
  spec.add_development_dependency "bundler", "~> 1.6"
29
30
  spec.add_development_dependency "rake"
@@ -40,7 +40,13 @@ module DropletKit
40
40
  end
41
41
 
42
42
  def last?
43
- @current_page == (self.total.to_f / per_page.to_f).ceil
43
+ @current_page == total_pages || self.total.zero?
44
+ end
45
+
46
+ def total_pages
47
+ return nil if self.total.nil?
48
+
49
+ (self.total.to_f / per_page.to_f).ceil
44
50
  end
45
51
 
46
52
  def ==(other)
@@ -6,9 +6,17 @@ module DropletKit
6
6
  handler(200, 201) { |response| ImageActionMapping.extract_single(response.body, :read) }
7
7
  end
8
8
 
9
+ action :all, 'GET /v2/images/:image_id/actions' do
10
+ handler(200) { |response| ImageActionMapping.extract_collection(response.body, :read) }
11
+ end
12
+
9
13
  action :find, 'GET /v2/images/:image_id/actions/:id' do
10
14
  handler(200) { |response| ImageActionMapping.extract_single(response.body, :read) }
11
15
  end
12
16
  end
17
+
18
+ def all(*args)
19
+ PaginatedResource.new(action(:all), self, *args)
20
+ end
13
21
  end
14
22
  end
@@ -1,3 +1,3 @@
1
1
  module DropletKit
2
- VERSION = "1.1.2"
2
+ VERSION = "1.1.3"
3
3
  end
@@ -0,0 +1,7 @@
1
+ {
2
+ "droplets": [
3
+ ],
4
+ "meta": {
5
+ "total": 0
6
+ }
7
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "actions": [
3
+ {
4
+ "id": 298374,
5
+ "status": "completed",
6
+ "type": "image_destroy",
7
+ "started_at": "2014-10-28T17:11:05Z",
8
+ "completed_at": "2014-10-28T17:11:06Z",
9
+ "resource_id": 45646587,
10
+ "resource_type": "image",
11
+ "region": null
12
+ },
13
+ {
14
+ "id": 234598,
15
+ "status": "completed",
16
+ "type": "transfer",
17
+ "started_at": "2014-10-28T17:09:11Z",
18
+ "completed_at": "2014-10-28T17:12:04Z",
19
+ "resource_id": 45646587,
20
+ "resource_type": "image",
21
+ "region": "nyc2"
22
+ }
23
+ ],
24
+ "links": {},
25
+ "meta": {
26
+ "total": 2
27
+ }
28
+ }
@@ -10,6 +10,17 @@ RSpec.describe DropletKit::Client do
10
10
  end
11
11
  end
12
12
 
13
+ describe "#method_missing" do
14
+
15
+ context "called with an existing method" do
16
+ it { expect{ client.actions}.to_not raise_error }
17
+ end
18
+
19
+ context "called with a missing method" do
20
+ it { expect{client.this_is_wrong}.to raise_error(NoMethodError) }
21
+ end
22
+ end
23
+
13
24
  describe '#connection' do
14
25
  it 'populates the authorization header correctly' do
15
26
  expect(client.connection.headers['Authorization']).to eq("Bearer #{client.access_token}")
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe DropletKit::BaseModel do
4
+ subject(:resource) do
5
+ Class.new(DropletKit::BaseModel) do |base|
6
+ attribute :droplet_limit
7
+ def self.name
8
+ 'SomeModel'
9
+ end
10
+ end
11
+ end
12
+
13
+ describe '#inspect' do
14
+ it 'returns the information about the current user' do
15
+ instance = resource.new(droplet_limit: 5)
16
+ expect(instance.inspect).to include("<SomeModel")
17
+ expect(instance.inspect).to include("@droplet_limit=>5")
18
+ expect(instance.inspect).to include("<SomeModel {:@droplet_limit=>5}>")
19
+ end
20
+ end
21
+ end
@@ -8,21 +8,8 @@ RSpec.describe DropletKit::PaginatedResource do
8
8
  let(:request_count) { RequestCounter.new(0) }
9
9
 
10
10
  let(:connection) { Faraday.new {|b| b.adapter :test, stubs } }
11
- let(:stubs) do
12
- Faraday::Adapter::Test::Stubs.new do |stub|
13
- stub.get('/droplets') do |env|
14
- request_count.count += 1
15
- uri = Addressable::URI.parse(env[:url].to_s)
16
- page = (uri.query_values['page'] || 1).to_i
17
- per_page = (uri.query_values['per_page'] || 20).to_i
18
- range = (0...per_page).map do |num|
19
- num + ((page - 1) * per_page)
20
- end
21
-
22
- [200, {}, { objects: range, meta: { total: 40 } }.to_json ]
23
- end
24
- end
25
- end
11
+ let(:response_size) { 40 }
12
+ let(:stubs) { stub_pager_request(response_size) }
26
13
  let(:action) { ResourceKit::Action.new(:find, :get, '/droplets') }
27
14
 
28
15
  before do
@@ -38,6 +25,26 @@ RSpec.describe DropletKit::PaginatedResource do
38
25
  end
39
26
  end
40
27
 
28
+ describe "#total_pages" do
29
+ let(:instance) { DropletKit::PaginatedResource.new(action, resource) }
30
+ it "returns nil if no request made" do
31
+ expect(instance.total_pages).to be_nil
32
+ end
33
+
34
+ it "returns correct page count after request made" do
35
+ instance.take(20)
36
+ expect(instance.total_pages).to eq(2)
37
+ end
38
+
39
+ context "when results are empty" do
40
+ let(:stubs) { stub_pager_request(0) }
41
+ it "returns 0" do
42
+ instance.take(1)
43
+ expect(instance.total_pages).to eq(0)
44
+ end
45
+ end
46
+ end
47
+
41
48
  describe '#each' do
42
49
  subject(:paginated) { DropletKit::PaginatedResource.new(action, resource) }
43
50
 
@@ -69,6 +69,12 @@ RSpec.describe DropletKit::DropletResource do
69
69
 
70
70
  check_droplet(droplets.first)
71
71
  end
72
+
73
+ it 'returns an empty array of droplets' do
74
+ stub_do_api('/v2/droplets', :get).to_return(body: api_fixture('droplets/all_empty'))
75
+ droplets = resource.all.map(&:id)
76
+ expect(droplets).to be_empty
77
+ end
72
78
  end
73
79
 
74
80
  describe '#find' do
@@ -26,6 +26,32 @@ RSpec.describe DropletKit::ImageActionResource do
26
26
  end
27
27
  end
28
28
 
29
+ describe '#all' do
30
+ it 'returns all of the image actions via a paginated resources' do
31
+ request = stub_do_api('/v2/images/449676391/actions', :get).to_return(
32
+ body: api_fixture('image_actions/all'),
33
+ status: 200
34
+ )
35
+
36
+ actions = resource.all(image_id: 449676391).take(20)
37
+
38
+ expect(request).to have_been_made
39
+
40
+ expect(actions.size).to be(2)
41
+
42
+ action = actions.first
43
+
44
+ expect(action.id).to eq(298374)
45
+ expect(action.status).to eq("completed")
46
+ expect(action.type).to eq("image_destroy")
47
+ expect(action.started_at).to eq("2014-10-28T17:11:05Z")
48
+ expect(action.completed_at).to eq("2014-10-28T17:11:06Z")
49
+ expect(action.resource_id).to eq(45646587)
50
+ expect(action.resource_type).to eq("image")
51
+ expect(action.region).to eq(nil)
52
+ end
53
+ end
54
+
29
55
  describe '#find' do
30
56
  it 'returns a single action' do
31
57
  stub_do_api('/v2/images/449676391/actions/23').to_return(body: api_fixture('image_actions/find'))
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,14 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start
4
+
1
5
  require 'droplet_kit'
2
6
  require 'webmock/rspec'
3
7
 
8
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
9
+ SimpleCov::Formatter::HTMLFormatter
10
+ ]
11
+
4
12
  Dir['./spec/support/**/*.rb'].each do |file|
5
13
  require file
6
14
  end
@@ -1,8 +1,4 @@
1
1
  class NullHashLoad
2
- def self.load(hash)
3
- hash
4
- end
5
-
6
2
  def self.dump(hash)
7
3
  hash
8
4
  end
@@ -6,4 +6,23 @@ module RequestStubHelpers
6
6
  def api_fixture(fixture_name)
7
7
  Pathname.new('./spec/fixtures/').join("#{fixture_name}.json").read
8
8
  end
9
+
10
+ def stub_pager_request(total_results = 40)
11
+ Faraday::Adapter::Test::Stubs.new do |stub|
12
+ stub.get('/droplets') do |env|
13
+ request_count.count += 1
14
+ uri = Addressable::URI.parse(env[:url].to_s)
15
+ page = (uri.query_values['page'] || 1).to_i
16
+ per_page = (uri.query_values['per_page'] || 20).to_i
17
+
18
+ max_elems = [total_results, per_page].min
19
+
20
+ range = (0...max_elems).map do |num|
21
+ num + ((page - 1) * per_page)
22
+ end
23
+
24
+ [200, {}, { objects: range, meta: { total: total_results } }.to_json ]
25
+ end
26
+ end
27
+ end
9
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: droplet_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Ross
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-07 00:00:00.000000000 Z
11
+ date: 2015-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 4.1.6
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.1
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: bundler
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -150,7 +164,7 @@ dependencies:
150
164
  - - "~>"
151
165
  - !ruby/object:Gem::Version
152
166
  version: '1.18'
153
- description: Droplet Kit is the official Ruby library for Digital Ocean's API
167
+ description: Droplet Kit is the official Ruby library for DigitalOcean's API
154
168
  email:
155
169
  - rross@digitalocean.com
156
170
  executables: []
@@ -227,12 +241,14 @@ files:
227
241
  - spec/fixtures/domains/create.json
228
242
  - spec/fixtures/domains/find.json
229
243
  - spec/fixtures/droplets/all.json
244
+ - spec/fixtures/droplets/all_empty.json
230
245
  - spec/fixtures/droplets/create.json
231
246
  - spec/fixtures/droplets/find.json
232
247
  - spec/fixtures/droplets/list_actions.json
233
248
  - spec/fixtures/droplets/list_backups.json
234
249
  - spec/fixtures/droplets/list_kernels.json
235
250
  - spec/fixtures/droplets/list_snapshots.json
251
+ - spec/fixtures/image_actions/all.json
236
252
  - spec/fixtures/image_actions/create.json
237
253
  - spec/fixtures/image_actions/find.json
238
254
  - spec/fixtures/images/all.json
@@ -244,6 +260,7 @@ files:
244
260
  - spec/fixtures/ssh_keys/find.json
245
261
  - spec/fixtures/ssh_keys/update.json
246
262
  - spec/lib/droplet_kit/client_spec.rb
263
+ - spec/lib/droplet_kit/models/base_model_spec.rb
247
264
  - spec/lib/droplet_kit/models/droplet_spec.rb
248
265
  - spec/lib/droplet_kit/paginated_resource_spec.rb
249
266
  - spec/lib/droplet_kit/resources/account_resource_spec.rb
@@ -285,7 +302,7 @@ rubyforge_project:
285
302
  rubygems_version: 2.2.2
286
303
  signing_key:
287
304
  specification_version: 4
288
- summary: Droplet Kit is the official Ruby library for Digital Ocean's API
305
+ summary: Droplet Kit is the official Ruby library for DigitalOcean's API
289
306
  test_files:
290
307
  - spec/fixtures/account/info.json
291
308
  - spec/fixtures/actions/all.json
@@ -298,12 +315,14 @@ test_files:
298
315
  - spec/fixtures/domains/create.json
299
316
  - spec/fixtures/domains/find.json
300
317
  - spec/fixtures/droplets/all.json
318
+ - spec/fixtures/droplets/all_empty.json
301
319
  - spec/fixtures/droplets/create.json
302
320
  - spec/fixtures/droplets/find.json
303
321
  - spec/fixtures/droplets/list_actions.json
304
322
  - spec/fixtures/droplets/list_backups.json
305
323
  - spec/fixtures/droplets/list_kernels.json
306
324
  - spec/fixtures/droplets/list_snapshots.json
325
+ - spec/fixtures/image_actions/all.json
307
326
  - spec/fixtures/image_actions/create.json
308
327
  - spec/fixtures/image_actions/find.json
309
328
  - spec/fixtures/images/all.json
@@ -315,6 +334,7 @@ test_files:
315
334
  - spec/fixtures/ssh_keys/find.json
316
335
  - spec/fixtures/ssh_keys/update.json
317
336
  - spec/lib/droplet_kit/client_spec.rb
337
+ - spec/lib/droplet_kit/models/base_model_spec.rb
318
338
  - spec/lib/droplet_kit/models/droplet_spec.rb
319
339
  - spec/lib/droplet_kit/paginated_resource_spec.rb
320
340
  - spec/lib/droplet_kit/resources/account_resource_spec.rb