berkshelf-api-client 1.0.0 → 1.1.0

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: 9ddfee1850c3e9011e1495158ad9be8755194118
4
- data.tar.gz: 8b719703fae7941835d42a606b2e67f2d43ca9aa
3
+ metadata.gz: b24984684bc4e74fd8c9e4c74a96e7387075c235
4
+ data.tar.gz: 37fa9b61254d333c6b10abb5aff3638c807f09fa
5
5
  SHA512:
6
- metadata.gz: a7e0ddf593d0e6c6835da51d51dcf1253eca1417b867ab7fd089c6998e929b61bcf623dde549de9e2e81fa74746d201d39738c044ccda1a68dca18b7672bf5c4
7
- data.tar.gz: ada8bc1b336c3cb192ec51dd383054a2355c6044633a426e947ba87dccbbaed90e4008a2e165de86068f24887c086242644a2f2220139aa927cedabfbc27678b
6
+ metadata.gz: a2687d666f257c2ec25c9786f1f25e12fc8c5cbcec50717b09c4b2570975013ca5a5d77502e6d6c1ee196d992d98669cb2334b03915f9397e6738b6933e203f1
7
+ data.tar.gz: 3c210c34674982c6ec7411d2cdfb6520eae51349e711e8a81205bf64990cdef7af55860f1a31a8a01e1d343277152cf5253611e03fbebc32e87f087ca66e8522
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p353
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ notifications:
6
+ irc:
7
+ channels:
8
+ - "irc.freenode.org#berkshelf"
9
+ skip_join: true
10
+ use_notice: true
11
+ script: bundle exec rake spec
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Berkshelf::APIClient
2
+ [![Build Status](https://travis-ci.org/berkshelf/berkshelf-api-client.png?branch=master)](https://travis-ci.org/berkshelf/berkshelf-api-client)
2
3
 
3
4
  A Ruby library for communicating with the [Berkshelf API server](https://github.com/berkshelf/berkshelf-api)
4
5
 
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'berkshelf/api_client'
4
+ require 'berkshelf/api_client/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "berkshelf-api-client"
@@ -0,0 +1,75 @@
1
+ require 'faraday'
2
+
3
+ module Berkshelf::APIClient
4
+ class Connection < Faraday::Connection
5
+ # @return [String]
6
+ attr_reader :url
7
+
8
+ # @return [Integer]
9
+ # how many retries to attempt on HTTP requests
10
+ attr_reader :retries
11
+
12
+ # @return [Float]
13
+ # time to wait between retries
14
+ attr_reader :retry_interval
15
+
16
+ # @param [String, Addressable::URI] url
17
+ #
18
+ # @option options [Integer] :open_timeout (3)
19
+ # how long to wait (in seconds) for connection open to the API server
20
+ # @option options [Integer] :timeout (30)
21
+ # how long to wait (in seconds) before getting a response from the API server
22
+ # @option options [Integer] :retries (3)
23
+ # how many retries to perform before giving up
24
+ # @option options [Float] :retry_interval (0.5)
25
+ # how long to wait (in seconds) between each retry
26
+ def initialize(url, options = {})
27
+ options = options.reverse_merge(retries: 3, retry_interval: 0.5,
28
+ open_timeout: 3, timeout: 30)
29
+ @url = url
30
+ @retries = options[:retries]
31
+ @retry_interval = options[:retry_interval]
32
+
33
+ options[:builder] ||= Faraday::Builder.new do |b|
34
+ b.response :parse_json
35
+ b.response :gzip
36
+ b.request :retry,
37
+ max: self.retries,
38
+ interval: self.retry_interval,
39
+ exceptions: [ Faraday::Error::TimeoutError, Errno::ETIMEDOUT ]
40
+
41
+ b.adapter :net_http
42
+ end
43
+
44
+ super(self.url, options)
45
+ @options[:open_timeout] = options[:open_timeout]
46
+ @options[:timeout] = options[:timeout]
47
+ end
48
+
49
+ # Retrieves the entire universe of known cookbooks from the API source
50
+ #
51
+ # @raise [APIClient::TimeoutError]
52
+ #
53
+ # @return [Array<APIClient::RemoteCookbook>]
54
+ def universe
55
+ response = get("universe")
56
+
57
+ case response.status
58
+ when 200
59
+ [].tap do |cookbooks|
60
+ response.body.each do |name, versions|
61
+ versions.each { |version, attributes| cookbooks << RemoteCookbook.new(name, version, attributes) }
62
+ end
63
+ end
64
+ when 404
65
+ raise APIClient::ServiceNotFound, "service not found at: #{url}"
66
+ when 500..504
67
+ raise APIClient::ServiceUnavaiable, "service unavailable at: #{url}"
68
+ else
69
+ raise APIClient::BadResponse, "bad response #{response.inspect}"
70
+ end
71
+ rescue Faraday::Error::TimeoutError, Errno::ETIMEDOUT
72
+ raise APIClient::TimeoutError, "Unable to connect to: #{url}"
73
+ end
74
+ end
75
+ end
@@ -1,7 +1,7 @@
1
1
  module Berkshelf
2
2
  class APIClientError < StandardError; end
3
3
 
4
- class APIClient
4
+ module APIClient
5
5
  class TimeoutError < APIClientError; end
6
6
  class BadResponse < APIClientError; end
7
7
  class ServiceUnavaiable < APIClientError; end
@@ -1,55 +1,53 @@
1
1
  require 'json'
2
2
 
3
- module Berkshelf
4
- class APIClient
5
- # A representation of cookbook metadata indexed by a Berkshelf API Server. Returned
6
- # by sending messages to a {Berkshelf::APIClient} and used to download cookbooks
7
- # indexed by the Berkshelf API Server.
8
- class RemoteCookbook
9
- # @return [String]
10
- attr_reader :name
11
- # @return [String]
12
- attr_reader :version
13
-
14
- # @param [String] name
15
- # @param [String] version
16
- # @param [Hash] attributes
17
- def initialize(name, version, attributes = {})
18
- @name = name
19
- @version = version
20
- @attributes = attributes
21
- end
22
-
23
- # @return [Hash]
24
- def dependencies
25
- @attributes[:dependencies]
26
- end
27
-
28
- # @return [Hash]
29
- def platforms
30
- @attributes[:platforms]
31
- end
32
-
33
- # @return [Symbol]
34
- def location_type
35
- @attributes[:location_type].to_sym
36
- end
37
-
38
- # @return [String]
39
- def location_path
40
- @attributes[:location_path]
41
- end
42
-
43
- def to_hash
44
- {
45
- name: name,
46
- version: version
47
- }
48
- end
49
-
50
- def to_json(options = {})
51
- ::JSON.pretty_generate(to_hash, options)
52
- end
3
+ module Berkshelf::APIClient
4
+ # A representation of cookbook metadata indexed by a Berkshelf API Server. Returned
5
+ # by sending messages to a {Berkshelf::APIClient} and used to download cookbooks
6
+ # indexed by the Berkshelf API Server.
7
+ class RemoteCookbook
8
+ # @return [String]
9
+ attr_reader :name
10
+ # @return [String]
11
+ attr_reader :version
12
+
13
+ # @param [String] name
14
+ # @param [String] version
15
+ # @param [Hash] attributes
16
+ def initialize(name, version, attributes = {})
17
+ @name = name
18
+ @version = version
19
+ @attributes = attributes
20
+ end
21
+
22
+ # @return [Hash]
23
+ def dependencies
24
+ @attributes[:dependencies]
25
+ end
26
+
27
+ # @return [Hash]
28
+ def platforms
29
+ @attributes[:platforms]
30
+ end
31
+
32
+ # @return [Symbol]
33
+ def location_type
34
+ @attributes[:location_type].to_sym
35
+ end
36
+
37
+ # @return [String]
38
+ def location_path
39
+ @attributes[:location_path]
40
+ end
41
+
42
+ def to_hash
43
+ {
44
+ name: name,
45
+ version: version
46
+ }
47
+ end
48
+
49
+ def to_json(options = {})
50
+ ::JSON.pretty_generate(to_hash, options)
53
51
  end
54
52
  end
55
53
  end
@@ -1,5 +1,5 @@
1
1
  module Berkshelf
2
- class APIClient
3
- VERSION = "1.0.0.dev"
2
+ module APIClient
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -1,85 +1,19 @@
1
- require 'faraday'
2
-
3
1
  module Berkshelf
4
2
  # Used to communicate with a remotely hosted [Berkshelf API Server](https://github.com/berkshelf/berkshelf-api).
5
3
  #
6
4
  # @example
7
5
  # client = Berkshelf::APIClient.new("https://api.berkshelf.com")
8
6
  # client.universe #=> [...]
9
- class APIClient < Faraday::Connection
10
- VERSION = "1.0.0"
11
-
7
+ module APIClient
8
+ require_relative 'api_client/version'
12
9
  require_relative 'api_client/errors'
13
10
  require_relative 'api_client/remote_cookbook'
11
+ require_relative 'api_client/connection'
14
12
 
15
- # @return [String]
16
- attr_reader :url
17
-
18
- # @return [Integer]
19
- # how many retries to attempt on HTTP requests
20
- attr_reader :retries
21
-
22
- # @return [Float]
23
- # time to wait between retries
24
- attr_reader :retry_interval
25
-
26
- # @param [String, Addressable::URI] url
27
- #
28
- # @option options [Integer] :open_timeout (3)
29
- # how long to wait (in seconds) for connection open to the API server
30
- # @option options [Integer] :timeout (30)
31
- # how long to wait (in seconds) before getting a response from the API server
32
- # @option options [Integer] :retries (3)
33
- # how many retries to perform before giving up
34
- # @option options [Float] :retry_interval (0.5)
35
- # how long to wait (in seconds) between each retry
36
- def initialize(url, options = {})
37
- options = options.reverse_merge(retries: 3, retry_interval: 0.5,
38
- open_timeout: 3, timeout: 30)
39
- @url = url
40
- @retries = options[:retries]
41
- @retry_interval = options[:retry_interval]
42
-
43
- options[:builder] ||= Faraday::Builder.new do |b|
44
- b.response :parse_json
45
- b.response :gzip
46
- b.request :retry,
47
- max: self.retries,
48
- interval: self.retry_interval,
49
- exceptions: [ Faraday::Error::TimeoutError, Errno::ETIMEDOUT ]
50
-
51
- b.adapter :net_http
52
- end
53
-
54
- super(self.url, options)
55
- @options[:open_timeout] = options[:open_timeout]
56
- @options[:timeout] = options[:timeout]
57
- end
58
-
59
- # Retrieves the entire universe of known cookbooks from the API source
60
- #
61
- # @raise [APIClient::TimeoutError]
62
- #
63
- # @return [Array<APIClient::RemoteCookbook>]
64
- def universe
65
- response = get("universe")
66
-
67
- case response.status
68
- when 200
69
- [].tap do |cookbooks|
70
- response.body.each do |name, versions|
71
- versions.each { |version, attributes| cookbooks << RemoteCookbook.new(name, version, attributes) }
72
- end
73
- end
74
- when 404
75
- raise APIClient::ServiceNotFound, "service not found at: #{url}"
76
- when 500..504
77
- raise APIClient::ServiceUnavaiable, "service unavailable at: #{url}"
78
- else
79
- raise APIClient::BadResponse, "bad response #{response.inspect}"
13
+ class << self
14
+ def new(*args)
15
+ Connection.new(*args)
80
16
  end
81
- rescue Faraday::Error::TimeoutError, Errno::ETIMEDOUT
82
- raise APIClient::TimeoutError, "Unable to connect to: #{url}"
83
17
  end
84
18
  end
85
19
  end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Berkshelf::APIClient::Connection do
4
+ let(:instance) { described_class.new("http://localhost:26210") }
5
+
6
+ describe "#universe" do
7
+ before do
8
+ berks_dependency("ruby", "1.2.3", dependencies: { "build-essential" => ">= 1.2.2" })
9
+ berks_dependency("ruby", "2.0.0", dependencies: { "build-essential" => ">= 1.2.2" })
10
+ berks_dependency("elixir", "1.0.0", platforms: { "CentOS" => "6.0" })
11
+ end
12
+
13
+ subject { instance.universe }
14
+
15
+ it "returns an array of APIClient::RemoteCookbook" do
16
+ expect(subject).to be_a(Array)
17
+
18
+ subject.each do |remote|
19
+ expect(remote).to be_a(Berkshelf::APIClient::RemoteCookbook)
20
+ end
21
+ end
22
+
23
+ it "contains a item for each dependency" do
24
+ expect(subject).to have(3).items
25
+ expect(subject[0].name).to eql("ruby")
26
+ expect(subject[0].version).to eql("1.2.3")
27
+ expect(subject[1].name).to eql("ruby")
28
+ expect(subject[1].version).to eql("2.0.0")
29
+ expect(subject[2].name).to eql("elixir")
30
+ expect(subject[2].version).to eql("1.0.0")
31
+ end
32
+
33
+ it "has the dependencies for each" do
34
+ expect(subject[0].dependencies).to include("build-essential" => ">= 1.2.2")
35
+ expect(subject[1].dependencies).to include("build-essential" => ">= 1.2.2")
36
+ expect(subject[2].dependencies).to be_empty
37
+ end
38
+
39
+ it "has the platforms for each" do
40
+ expect(subject[0].platforms).to be_empty
41
+ expect(subject[1].platforms).to be_empty
42
+ expect(subject[2].platforms).to include("CentOS" => "= 6.0.0")
43
+ end
44
+
45
+ it "has a location_path for each" do
46
+ subject.each do |remote|
47
+ expect(remote.location_path).to_not be_nil
48
+ end
49
+ end
50
+
51
+ it "has a location_type for each" do
52
+ subject.each do |remote|
53
+ expect(remote.location_type).to_not be_nil
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,57 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Berkshelf::APIClient do
4
- let(:instance) { described_class.new("http://localhost:26210") }
5
-
6
- describe "#universe" do
7
- before do
8
- berks_dependency("ruby", "1.2.3", dependencies: { "build-essential" => ">= 1.2.2" })
9
- berks_dependency("ruby", "2.0.0", dependencies: { "build-essential" => ">= 1.2.2" })
10
- berks_dependency("elixir", "1.0.0", platforms: { "CentOS" => "6.0" })
11
- end
12
-
13
- subject { instance.universe }
14
-
15
- it "returns an array of APIClient::RemoteCookbook" do
16
- expect(subject).to be_a(Array)
17
-
18
- subject.each do |remote|
19
- expect(remote).to be_a(Berkshelf::APIClient::RemoteCookbook)
20
- end
21
- end
22
-
23
- it "contains a item for each dependency" do
24
- expect(subject).to have(3).items
25
- expect(subject[0].name).to eql("ruby")
26
- expect(subject[0].version).to eql("1.2.3")
27
- expect(subject[1].name).to eql("ruby")
28
- expect(subject[1].version).to eql("2.0.0")
29
- expect(subject[2].name).to eql("elixir")
30
- expect(subject[2].version).to eql("1.0.0")
31
- end
32
-
33
- it "has the dependencies for each" do
34
- expect(subject[0].dependencies).to include("build-essential" => ">= 1.2.2")
35
- expect(subject[1].dependencies).to include("build-essential" => ">= 1.2.2")
36
- expect(subject[2].dependencies).to be_empty
37
- end
38
-
39
- it "has the platforms for each" do
40
- expect(subject[0].platforms).to be_empty
41
- expect(subject[1].platforms).to be_empty
42
- expect(subject[2].platforms).to include("CentOS" => "= 6.0.0")
43
- end
44
-
45
- it "has a location_path for each" do
46
- subject.each do |remote|
47
- expect(remote.location_path).to_not be_nil
48
- end
49
- end
50
-
51
- it "has a location_type for each" do
52
- subject.each do |remote|
53
- expect(remote.location_type).to_not be_nil
54
- end
4
+ describe "::new" do
5
+ it "returns an instance of Berkshelf::APIClient::Connection" do
6
+ expect(described_class.new("http://localhost:26210")).to be_a(Berkshelf::APIClient::Connection)
55
7
  end
56
8
  end
57
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berkshelf-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Winsor
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-12-26 00:00:00.000000000 Z
13
+ date: 2013-12-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
@@ -120,6 +120,8 @@ extensions: []
120
120
  extra_rdoc_files: []
121
121
  files:
122
122
  - .gitignore
123
+ - .ruby-version
124
+ - .travis.yml
123
125
  - Gemfile
124
126
  - Guardfile
125
127
  - LICENSE
@@ -128,10 +130,12 @@ files:
128
130
  - berkshelf-api-client.gemspec
129
131
  - lib/berkshelf/api-client.rb
130
132
  - lib/berkshelf/api_client.rb
133
+ - lib/berkshelf/api_client/connection.rb
131
134
  - lib/berkshelf/api_client/errors.rb
132
135
  - lib/berkshelf/api_client/remote_cookbook.rb
133
136
  - lib/berkshelf/api_client/version.rb
134
137
  - spec/spec_helper.rb
138
+ - spec/unit/berkshelf/api_client/connection_spec.rb
135
139
  - spec/unit/berkshelf/api_client/remote_cookbook_spec.rb
136
140
  - spec/unit/berkshelf/api_client_spec.rb
137
141
  homepage: http://berkshelf.com
@@ -160,6 +164,7 @@ specification_version: 4
160
164
  summary: API Client for communicating with a Berkshelf API server
161
165
  test_files:
162
166
  - spec/spec_helper.rb
167
+ - spec/unit/berkshelf/api_client/connection_spec.rb
163
168
  - spec/unit/berkshelf/api_client/remote_cookbook_spec.rb
164
169
  - spec/unit/berkshelf/api_client_spec.rb
165
170
  has_rdoc: