Skip to content

Commit

Permalink
Add Droplet serialization properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Ross committed Aug 3, 2014
1 parent 9fa2d5a commit 2970ac1
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 60 deletions.
1 change: 0 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
--color
--warnings
--require spec_helper
4 changes: 4 additions & 0 deletions droplet_kit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", "~> 3.0.0"

# FakeServe
spec.add_development_dependency 'sinatra', '~> 1.4'
spec.add_development_dependency 'webmock', '~> 1.18'
end
3 changes: 3 additions & 0 deletions lib/droplet_kit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module DropletKit
# Models
autoload :BaseModel, 'droplet_kit/models/base_model'
autoload :Droplet, 'droplet_kit/models/droplet'
autoload :Region, 'droplet_kit/models/region'
autoload :Image, 'droplet_kit/models/image'
autoload :Size, 'droplet_kit/models/size'

# Resources
autoload :DropletResource, 'droplet_kit/resources/droplet_resource'
Expand Down
36 changes: 36 additions & 0 deletions lib/droplet_kit/mappings/droplet_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,42 @@ class DropletMapping
property :snapshot_ids, scopes: [:read]
property :action_ids, scopes: [:read]
property :features, scopes: [:read]

property :region, scopes: [:read] do
mapping Region

property :slug, scopes: [:read]
property :name, scopes: [:read]
property :sizes, scopes: [:read]
property :available, scopes: [:read]
property :features, scopes: [:read]
end

property :image, scopes: [:read] do
mapping Image

property :id, scopes: [:read]
property :name, scopes: [:read]
property :distribution, scopes: [:read]
property :slug, scopes: [:read]
property :public, scopes: [:read]
property :regions, scopes: [:read]
end

property :size, scopes: [:read] do
mapping Size

property :slug, scopes: [:read]
property :transfer, scopes: [:read]
property :price_monthly, scopes: [:read]
property :price_hourly, scopes: [:read]
end

property :networks do
mapping NetworkMap
property :v4
property :v6
end
end
end
end
4 changes: 4 additions & 0 deletions lib/droplet_kit/models/droplet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Droplet < BaseModel
attribute :action_ids
attribute :features

attribute :region
attribute :image
attribute :size

# region object
# networks object
# size object
Expand Down
10 changes: 10 additions & 0 deletions lib/droplet_kit/models/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module DropletKit
class Image < BaseModel
attribute :id
attribute :name
attribute :distribution
attribute :slug
attribute :public
attribute :regions
end
end
9 changes: 9 additions & 0 deletions lib/droplet_kit/models/region.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module DropletKit
class Region < BaseModel
attribute :slug
attribute :name
attribute :sizes
attribute :available
attribute :features
end
end
8 changes: 8 additions & 0 deletions lib/droplet_kit/models/size.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module DropletKit
class Size < BaseModel
attribute :slug
attribute :transfer
attribute :price_monthly
attribute :price_hourly
end
end
84 changes: 84 additions & 0 deletions spec/fixtures/droplets/all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"droplets": [
{
"id": 19,
"name": "test.example.com",
"memory": 1024,
"vcpus": 2,
"disk": 20,
"region": {
"slug": "nyc1",
"name": "New York",
"sizes": [
"1024mb",
"512mb"
],
"available": true,
"features": [
"virtio",
"private_networking",
"backups",
"ipv6"
]
},
"image": {
"id": 119192817,
"name": "Ubuntu 13.04",
"distribution": "ubuntu",
"slug": "ubuntu1304",
"public": true,
"regions": [
"nyc1"
],
"created_at": "2014-07-29T14:35:36Z"
},
"size": {
"slug": "1024mb",
"transfer": 2,
"price_monthly": 10.0,
"price_hourly": 0.01488
},
"locked": false,
"status": "active",
"networks": {
"v4": [
{
"ip_address": "127.0.0.19",
"netmask": "255.255.255.0",
"gateway": "127.0.0.20",
"type": "public"
}
],
"v6": [
{
"ip_address": "2001::13",
"cidr": 124,
"gateway": "2400:6180:0000:00D0:0000:0000:0009:7000",
"type": "public"
}
]
},
"kernel": {
"id": 485432985,
"name": "DO-recovery-static-fsck",
"version": "3.8.0-25-generic"
},
"created_at": "2014-07-29T14:35:36Z",
"features": [
"ipv6"
],
"backup_ids": [
449676382
],
"snapshot_ids": [
449676383
],
"action_ids": [

]
}
],
"meta": {
"total": 1
}
}
48 changes: 48 additions & 0 deletions spec/lib/droplet_kit/resources/droplet_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'spec_helper'

RSpec.describe DropletKit::DropletResource do
subject(:resource) { described_class.new(connection) }
include_context 'resources'

# Theres a lot to check
def check_droplet(droplet)
expect(droplet.name).to eq('test.example.com')
expect(droplet.memory).to eq(1024)
expect(droplet.vcpus).to eq(2)
expect(droplet.disk).to eq(20)
expect(droplet.locked).to eq(false)
expect(droplet.status).to eq('active')
expect(droplet.created_at).to eq('2014-07-29T14:35:36Z')

expect(droplet.region).to be_kind_of(DropletKit::Region)
expect(droplet.region.slug).to eq('nyc1')
expect(droplet.region.name).to eq('New York')
expect(droplet.region.sizes).to include('1024mb', '512mb')
expect(droplet.region.available).to be(true)
expect(droplet.region.features).to include("virtio", "private_networking", "backups", "ipv6")

expect(droplet.image).to be_kind_of(DropletKit::Image)
expect(droplet.image.id).to eq(119192817)
expect(droplet.image.name).to eq("Ubuntu 13.04")
expect(droplet.image.distribution).to eq("ubuntu")
expect(droplet.image.slug).to eq("ubuntu1304")
expect(droplet.image.public).to eq(true)
expect(droplet.image.regions).to include('nyc1')

expect(droplet.size).to be_kind_of(DropletKit::Size)
expect(droplet.size.slug).to eq("1024mb")
expect(droplet.size.transfer).to eq(2)
expect(droplet.size.price_monthly).to eq(10.0)
expect(droplet.size.price_hourly).to eq(0.01488)
end

describe '#all' do
it 'returns all of the droplets' do
stub_do_api('/v2/droplets', :get).to_return(body: api_fixture('droplets/all'))
droplets = resource.all
expect(droplets).to all(be_kind_of(DropletKit::Droplet))

check_droplet(droplets[0])
end
end
end
65 changes: 6 additions & 59 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,64 +1,11 @@
require 'droplet_kit'
require 'webmock/rspec'

RSpec.configure do |config|
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
Dir['./spec/support/**/*.rb'].each do |file|
require file
end

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
RSpec.configure do |config|
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
expectations.syntax = :expect
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
mocks.syntax = :expect
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended.
mocks.verify_partial_doubles = true
end
=end
config.include RequestStubHelpers
end
4 changes: 4 additions & 0 deletions spec/support/fake_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'sinatra/base'

class FakeServer < Sinatra::Base
end
9 changes: 9 additions & 0 deletions spec/support/request_stub_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module RequestStubHelpers
def stub_do_api(path, verb = :any)
stub_request(verb, %r[#{DropletKit::Client::DIGITALOCEAN_API}#{path}])
end

def api_fixture(fixture_name)
Pathname.new('./spec/fixtures/').join("#{fixture_name}.json").read
end
end
4 changes: 4 additions & 0 deletions spec/support/resource_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shared_context 'resources' do
let(:client) { DropletKit::Client.new(access_token: 'bunk-token') }
let(:connection) { client.connection }
end

0 comments on commit 2970ac1

Please sign in to comment.