aptible-api 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dfdcc30829f257c5b6d98ad8ede2ea34b7a5f328
4
- data.tar.gz: 79ce495635673d2b54c9e60d142fb7b2d27f13fd
3
+ metadata.gz: 0013944c43af6ae364441bcf5802e68defadc874
4
+ data.tar.gz: 26b2de9b909d738085ab83b7b397e3e13fb625a5
5
5
  SHA512:
6
- metadata.gz: d435190c8b3dff583848b19285a2e50ef7d06bb7a1c9f10e8cd276eaa00e444e851e34a8da4c7a39f32d4674a771efd89c8d5b1675bad2534abcf639b85a9b26
7
- data.tar.gz: 36a56dadd99b38fa76a6558fd598fee051f798fa41b0e3fcd018b2a2d8ec1a8564ea73ca56642daa65ff86d7f4b6af37408651bdfa0d38e79ff5732fc7e2f49f
6
+ metadata.gz: 12d71e142af40537c0a9c20974fb995604ebf7ac51340f994ab9915908bac2da3f37f74967c637939ed285b131938c36a019126b4f2f184415da46923dd534d0
7
+ data.tar.gz: ff6a9b5150ab4dd95332e389c96934699c8ff25e6c5bd90040137f2b1fccb432154034a26248de21b1634e0c840d427fc218f86d43eb2ee5fdb0957db00af112
data/Gemfile CHANGED
@@ -1,7 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'oauth2', github: 'fancyremarker/oauth2', branch: 'aptible'
4
- gem 'hyperresource', github: 'fancyremarker/hyperresource', branch: 'aptible'
5
-
6
3
  # Specify your gem's dependencies in aptible-api.gemspec
7
4
  gemspec
data/Procfile ADDED
@@ -0,0 +1 @@
1
+ console: bundle exec pry -r aptible/api
data/README.md CHANGED
@@ -11,12 +11,11 @@ Ruby client for [api.aptible.com](https://api.aptible.com/). Aptible's API serve
11
11
  Add the following lines to your application's Gemfile.
12
12
 
13
13
  gem 'aptible-api'
14
- gem 'hyperresource', github: 'fancyremarker/hyperresource', branch: 'aptible'
15
-
16
- The forked version of the HyperResource gem is necessary until [gamache/hyperresource#19](https://github.com/gamache/hyperresource/pull/19) is merged.
17
14
 
18
15
  And then run `bundle install`.
19
16
 
17
+ *Note:* A forked version of the HyperResource gem (`hyperresource-aptible`) is necessary until [gamache/hyperresource#22](https://github.com/gamache/hyperresource/pull/22) and [gamache/hyperresource#23](https://github.com/gamache/hyperresource/pull/23) are merged.
18
+
20
19
  ## Usage
21
20
 
22
21
  First, get a token:
data/aptible-api.gemspec CHANGED
@@ -3,11 +3,10 @@ lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  require 'English'
6
- require 'aptible/api/version'
7
6
 
8
7
  Gem::Specification.new do |spec|
9
8
  spec.name = 'aptible-api'
10
- spec.version = Aptible::Api::VERSION
9
+ spec.version = '0.2.0'
11
10
  spec.authors = ['Frank Macreery']
12
11
  spec.email = ['frank@macreery.com']
13
12
  spec.description = %q{Ruby client for api.aptible.com}
@@ -20,13 +19,15 @@ Gem::Specification.new do |spec|
20
19
  spec.require_paths = ['lib']
21
20
 
22
21
  spec.add_dependency 'gem_config'
23
- spec.add_dependency 'hyperresource'
22
+ spec.add_dependency 'hyperresource-aptible'
24
23
  spec.add_dependency 'aptible-auth'
25
24
  spec.add_dependency 'fridge'
26
25
 
27
26
  spec.add_development_dependency 'bundler', '~> 1.3'
28
27
  spec.add_development_dependency 'aptible-tasks', '>= 0.2.0'
28
+ spec.add_development_dependency 'activesupport'
29
29
  spec.add_development_dependency 'rake'
30
30
  spec.add_development_dependency 'rspec', '~> 2.0'
31
+ spec.add_development_dependency 'foreman'
31
32
  spec.add_development_dependency 'pry'
32
33
  end
@@ -0,0 +1,16 @@
1
+ module Aptible
2
+ class Api::Account < Api::Resource
3
+ has_many :apps
4
+ has_many :disks
5
+ has_many :services
6
+
7
+ def production?
8
+ type == 'production'
9
+ end
10
+
11
+ def operations
12
+ # TODO: Implement /accounts/:id/operations
13
+ []
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ module Aptible
2
+ class Api::Adapter < HyperResource::Adapter::HAL_JSON
3
+ class << self
4
+ # rubocop:disable MethodLength
5
+ def get_data_type_from_object(object)
6
+ return nil unless object
7
+
8
+ if (type = object['_type'])
9
+ if type.respond_to?(:camelize)
10
+ type.camelize
11
+ else
12
+ type[0].upcase + type[1..-1]
13
+ end
14
+ else
15
+ 'Resource'
16
+ end
17
+ end
18
+ # rubocop:enable MethodLength
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ module Aptible
2
+ class Api::App < Api::Resource
3
+ has_many :configurations
4
+ has_many :images
5
+ has_many :operations
6
+ has_many :permissions
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module Aptible
2
+ class Api::Attachment < Api::Resource
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Aptible
2
+ class Api::Configuration < Api::Resource
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Aptible
2
+ class Api::Container < Api::Resource
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module Aptible
2
+ class Api::Disk < Api::Resource
3
+ has_many :operations
4
+ has_many :permissions
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ module Aptible
2
+ class Api::Image < Api::Resource
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Aptible
2
+ class Api::Operation < Api::Resource
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Aptible
2
+ class Api::Permission < Api::Resource
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Aptible
2
+ class Api::Proxy < Api::Resource
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module Aptible
2
+ class Api::Release < Api::Resource
3
+ has_many :containers
4
+ end
5
+ end
@@ -0,0 +1,49 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Aptible
4
+ class Api::Resource < Api
5
+ def self.collection_url
6
+ basename = name.split('::').last
7
+ config = Aptible::Api.configuration
8
+ config.root_url.chomp('/') + "/#{basename.downcase.pluralize}"
9
+ end
10
+
11
+ def self.find(id)
12
+ find_by_url("#{collection_url}/#{id}")
13
+ end
14
+
15
+ def self.find_by_url(url)
16
+ # REVIEW: Should exception be raised if return type mismatch?
17
+ new.find_by_url(url)
18
+ rescue
19
+ nil
20
+ end
21
+
22
+ # rubocop:disable PredicateName
23
+ def self.has_many(relation)
24
+ define_method relation do
25
+ get unless loaded
26
+ if (memoized = instance_variable_get("@#{relation}"))
27
+ memoized
28
+ elsif links[relation]
29
+ instance_variable_set("@#{relation}", links[relation].entries)
30
+ end
31
+ end
32
+ end
33
+ # rubocop:enable PredicateName
34
+ end
35
+ end
36
+
37
+ require 'aptible/api/account'
38
+ require 'aptible/api/app'
39
+ require 'aptible/api/attachment'
40
+ require 'aptible/api/configuration'
41
+ require 'aptible/api/container'
42
+ require 'aptible/api/disk'
43
+ require 'aptible/api/image'
44
+ require 'aptible/api/operation'
45
+ require 'aptible/api/permission'
46
+ require 'aptible/api/proxy'
47
+ require 'aptible/api/release'
48
+ require 'aptible/api/service'
49
+ require 'aptible/api/vhost'
@@ -0,0 +1,9 @@
1
+ module Aptible
2
+ class Api::Service < Api::Resource
3
+ has_many :vhosts
4
+ has_many :operations
5
+ has_many :permissions
6
+ has_many :proxies
7
+ has_many :releases
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module Aptible
2
+ class Api::Vhost < Api::Resource
3
+ end
4
+ end
data/lib/aptible/api.rb CHANGED
@@ -1,16 +1,69 @@
1
- require 'aptible/api/version'
2
- require 'aptible/api/client'
3
-
1
+ require 'aptible/auth'
4
2
  require 'gem_config'
3
+ require 'hyperresource'
4
+ require 'fridge'
5
5
 
6
6
  module Aptible
7
- module Api
7
+ class Api < HyperResource
8
8
  include GemConfig::Base
9
9
 
10
+ attr_accessor :token, :config
11
+
10
12
  with_configuration do
11
13
  has :root_url,
12
14
  classes: [String],
13
15
  default: ENV['APTIBLE_API_ROOT_URL'] || 'https://api.aptible.com'
14
16
  end
17
+
18
+ def self.get_data_type_from_response(response)
19
+ return nil unless response && response.body
20
+ adapter.get_data_type_from_object(adapter.deserialize(response.body))
21
+ end
22
+
23
+ def self.adapter
24
+ Aptible::Api::Adapter
25
+ end
26
+
27
+ def adapter
28
+ self.class.adapter
29
+ end
30
+
31
+ def initialize(options = {})
32
+ if options.is_a?(Hash)
33
+ self.token = options[:token]
34
+
35
+ options[:root] ||= config.root_url
36
+ options[:namespace] ||= 'Aptible::Api'
37
+ options[:headers] ||= { 'Content-Type' => 'application/json' }
38
+ options[:headers].merge!(
39
+ 'Authorization' => "Bearer #{bearer_token}"
40
+ ) if options[:token]
41
+ end
42
+
43
+ super(options)
44
+ end
45
+
46
+ def find_by_url(url)
47
+ fail "URL must be rooted at #{root}" unless /^#{root}/.match url
48
+
49
+ resource = dup
50
+ resource.href = url.gsub(/^#{root}/, '')
51
+ resource.get
52
+ end
53
+
54
+ def bearer_token
55
+ case token
56
+ when Aptible::Auth::Token then token.access_token
57
+ when Fridge::AccessToken then token.to_s
58
+ when String then token
59
+ end
60
+ end
61
+
62
+ def config
63
+ @config ||= Aptible::Api.configuration
64
+ end
15
65
  end
16
66
  end
67
+
68
+ require 'aptible/api/adapter'
69
+ require 'aptible/api/resource'
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Aptible::Api::Resource do
4
+ describe '.collection_url' do
5
+ it 'should use the pluralized resource name' do
6
+ url = Aptible::Api::Disk.collection_url
7
+ expect(url).to eq 'https://api.aptible.com/disks'
8
+ end
9
+ end
10
+
11
+ describe '.find' do
12
+ it 'should call find_by_url' do
13
+ url = 'https://api.aptible.com/disks/42'
14
+ expect(Aptible::Api::Disk).to receive(:find_by_url).with url
15
+ Aptible::Api::Disk.find(42)
16
+ end
17
+ end
18
+ end
@@ -1,6 +1,33 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Aptible::Api do
4
+ describe '#initialize' do
5
+ it 'should be a HyperResource instance' do
6
+ expect(subject).to be_a HyperResource
7
+ end
8
+ end
9
+
10
+ describe '#bearer_token' do
11
+ it 'should accept an Aptible::Auth::Token' do
12
+ token = Aptible::Auth::Token.new
13
+ token.stub(:access_token) { 'abtible_auth_token' }
14
+ subject.stub(:token) { token }
15
+ expect(subject.bearer_token).to eq token.access_token
16
+ end
17
+
18
+ it 'should accept an Fridge::AccessToken' do
19
+ token = Fridge::AccessToken.new
20
+ token.stub(:to_s) { 'fridge_access_token' }
21
+ subject.stub(:token) { token }
22
+ expect(subject.bearer_token).to eq token.to_s
23
+ end
24
+
25
+ it 'should accept a String' do
26
+ subject.stub(:token) { 'token' }
27
+ expect(subject.bearer_token).to eq 'token'
28
+ end
29
+ end
30
+
4
31
  it 'should have a configurable root_url' do
5
32
  config = described_class.configuration
6
33
  expect(config).to be_a GemConfig::Configuration
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aptible-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank Macreery
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-19 00:00:00.000000000 Z
11
+ date: 2014-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gem_config
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: hyperresource
28
+ name: hyperresource-aptible
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '>='
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - '>='
95
95
  - !ruby/object:Gem::Version
96
96
  version: 0.2.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rake
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +136,20 @@ dependencies:
122
136
  - - ~>
123
137
  - !ruby/object:Gem::Version
124
138
  version: '2.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: foreman
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
125
153
  - !ruby/object:Gem::Dependency
126
154
  name: pry
127
155
  requirement: !ruby/object:Gem::Requirement
@@ -148,13 +176,27 @@ files:
148
176
  - .travis.yml
149
177
  - Gemfile
150
178
  - LICENSE.md
179
+ - Procfile
151
180
  - README.md
152
181
  - Rakefile
153
182
  - aptible-api.gemspec
154
183
  - lib/aptible/api.rb
155
- - lib/aptible/api/client.rb
156
- - lib/aptible/api/version.rb
157
- - spec/aptible/api/client_spec.rb
184
+ - lib/aptible/api/account.rb
185
+ - lib/aptible/api/adapter.rb
186
+ - lib/aptible/api/app.rb
187
+ - lib/aptible/api/attachment.rb
188
+ - lib/aptible/api/configuration.rb
189
+ - lib/aptible/api/container.rb
190
+ - lib/aptible/api/disk.rb
191
+ - lib/aptible/api/image.rb
192
+ - lib/aptible/api/operation.rb
193
+ - lib/aptible/api/permission.rb
194
+ - lib/aptible/api/proxy.rb
195
+ - lib/aptible/api/release.rb
196
+ - lib/aptible/api/resource.rb
197
+ - lib/aptible/api/service.rb
198
+ - lib/aptible/api/vhost.rb
199
+ - spec/aptible/api/resource_spec.rb
158
200
  - spec/aptible/api_spec.rb
159
201
  - spec/shared/set_env.rb
160
202
  - spec/spec_helper.rb
@@ -183,7 +225,7 @@ signing_key:
183
225
  specification_version: 4
184
226
  summary: Ruby client for api.aptible.com
185
227
  test_files:
186
- - spec/aptible/api/client_spec.rb
228
+ - spec/aptible/api/resource_spec.rb
187
229
  - spec/aptible/api_spec.rb
188
230
  - spec/shared/set_env.rb
189
231
  - spec/spec_helper.rb
@@ -1,39 +0,0 @@
1
- require 'hyperresource'
2
- require 'aptible/auth'
3
- require 'fridge'
4
-
5
- module Aptible
6
- module Api
7
- class Client < HyperResource
8
- attr_accessor :token, :config
9
-
10
- def initialize(options = {})
11
- unless options.is_a?(Hash)
12
- fail ArgumentError, 'Call Aptible::Api::Client.new with a Hash'
13
- end
14
- self.token = options[:token]
15
-
16
- options[:root] ||= config.root_url
17
- options[:headers] ||= { 'Content-Type' => 'application/json' }
18
- options[:headers].merge!(
19
- 'Authorization' => "Bearer #{bearer_token}"
20
- ) if options[:token]
21
-
22
- super(options)
23
- end
24
-
25
- def bearer_token
26
- # REVIEW: Should we really allow any token type here?
27
- case token
28
- when Aptible::Auth::Token then token.access_token
29
- when Fridge::AccessToken then token.to_s
30
- when String then token
31
- end
32
- end
33
-
34
- def config
35
- @config ||= Aptible::Api.configuration
36
- end
37
- end
38
- end
39
- end
@@ -1,5 +0,0 @@
1
- module Aptible
2
- module Api
3
- VERSION = '0.1.2'
4
- end
5
- end
@@ -1,30 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Aptible::Api::Client do
4
- describe '#initialize' do
5
- it 'should be a HyperResource instance' do
6
- expect(subject).to be_a HyperResource
7
- end
8
- end
9
-
10
- describe '#bearer_token' do
11
- it 'should accept an Aptible::Auth::Token' do
12
- token = Aptible::Auth::Token.new
13
- token.stub(:access_token) { 'abtible_auth_token' }
14
- subject.stub(:token) { token }
15
- expect(subject.bearer_token).to eq token.access_token
16
- end
17
-
18
- it 'should accept an Fridge::AccessToken' do
19
- token = Fridge::AccessToken.new
20
- token.stub(:to_s) { 'fridge_access_token' }
21
- subject.stub(:token) { token }
22
- expect(subject.bearer_token).to eq token.to_s
23
- end
24
-
25
- it 'should accept a String' do
26
- subject.stub(:token) { 'token' }
27
- expect(subject.bearer_token).to eq 'token'
28
- end
29
- end
30
- end