berkshelf-api-client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9ddfee1850c3e9011e1495158ad9be8755194118
4
+ data.tar.gz: 8b719703fae7941835d42a606b2e67f2d43ca9aa
5
+ SHA512:
6
+ metadata.gz: a7e0ddf593d0e6c6835da51d51dcf1253eca1417b867ab7fd089c6998e929b61bcf623dde549de9e2e81fa74746d201d39738c044ccda1a68dca18b7672bf5c4
7
+ data.tar.gz: ada8bc1b336c3cb192ec51dd383054a2355c6044633a426e947ba87dccbbaed90e4008a2e165de86068f24887c086242644a2f2220139aa927cedabfbc27678b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,36 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :guard do
6
+ gem 'coolline', '~> 0.4.2'
7
+ gem 'guard', '~> 1.8'
8
+ gem 'guard-cucumber'
9
+ gem 'guard-rspec'
10
+ gem 'guard-spork'
11
+ gem 'guard-yard'
12
+
13
+ require 'rbconfig'
14
+
15
+ if RbConfig::CONFIG['target_os'] =~ /darwin/i
16
+ gem 'growl', require: false
17
+ gem 'rb-fsevent', require: false
18
+
19
+ if `uname`.strip == 'Darwin' && `sw_vers -productVersion`.strip >= '10.8'
20
+ gem 'terminal-notifier-guard', '~> 1.5.3', require: false
21
+ end rescue Errno::ENOENT
22
+
23
+ elsif RbConfig::CONFIG['target_os'] =~ /linux/i
24
+ gem 'libnotify', '~> 0.8.0', require: false
25
+ gem 'rb-inotify', require: false
26
+
27
+ elsif RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
28
+ gem 'rb-notifu', '>= 0.0.4', require: false
29
+ gem 'wdm', require: false
30
+ gem 'win32console', require: false
31
+ end
32
+ end
33
+
34
+ group :test do
35
+ gem 'berkshelf-api', github: "berkshelf/berkshelf-api"
36
+ end
data/Guardfile ADDED
@@ -0,0 +1,14 @@
1
+ guard 'spork' do
2
+ watch('Gemfile')
3
+ watch('spec/spec_helper.rb') { :rspec }
4
+ end
5
+
6
+ guard 'yard', stdout: '/dev/null', stderr: '/dev/null' do
7
+ watch(%r{lib/.+\.rb})
8
+ end
9
+
10
+ guard 'rspec', cli: '--color --drb --format Fuubar', all_on_start: false, all_after_pass: false do
11
+ watch(%r{^spec/unit/.+_spec\.rb$})
12
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" }
13
+ watch('spec/spec_helper.rb') { 'spec' }
14
+ end
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2013 Jamie Winsor
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Berkshelf::APIClient
2
+
3
+ A Ruby library for communicating with the [Berkshelf API server](https://github.com/berkshelf/berkshelf-api)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'berkshelf-api-client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install berkshelf-api-client
18
+
19
+ ## Usage
20
+
21
+ require 'berkshelf/api-client'
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'berkshelf/api_client'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "berkshelf-api-client"
8
+ spec.version = Berkshelf::APIClient::VERSION
9
+ spec.authors = [
10
+ "Jamie Winsor",
11
+ "Michael Ivey",
12
+ "Seth Vargo"
13
+ ]
14
+ spec.email = [
15
+ "jamie@vialstudios.com",
16
+ "michael.ivey@riotgames.com",
17
+ "sethvargo@gmail.com"
18
+ ]
19
+ spec.description = %q{API Client for communicating with a Berkshelf API server}
20
+ spec.summary = spec.description
21
+ spec.homepage = "http://berkshelf.com"
22
+ spec.license = "Apache 2.0"
23
+ spec.required_ruby_version = " >= 1.9.2"
24
+
25
+ spec.files = `git ls-files`.split($/)
26
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
27
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "faraday", "~> 0.8.5"
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.3"
33
+ spec.add_development_dependency "rake"
34
+ spec.add_development_dependency 'fuubar', "~> 1.1"
35
+ spec.add_development_dependency 'rspec', "~> 2.13"
36
+ spec.add_development_dependency 'spork', "~> 0.9"
37
+ spec.add_development_dependency 'yard', "~> 0.8"
38
+ end
@@ -0,0 +1 @@
1
+ require_relative 'api_client'
@@ -0,0 +1,85 @@
1
+ require 'faraday'
2
+
3
+ module Berkshelf
4
+ # Used to communicate with a remotely hosted [Berkshelf API Server](https://github.com/berkshelf/berkshelf-api).
5
+ #
6
+ # @example
7
+ # client = Berkshelf::APIClient.new("https://api.berkshelf.com")
8
+ # client.universe #=> [...]
9
+ class APIClient < Faraday::Connection
10
+ VERSION = "1.0.0"
11
+
12
+ require_relative 'api_client/errors'
13
+ require_relative 'api_client/remote_cookbook'
14
+
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}"
80
+ end
81
+ rescue Faraday::Error::TimeoutError, Errno::ETIMEDOUT
82
+ raise APIClient::TimeoutError, "Unable to connect to: #{url}"
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,10 @@
1
+ module Berkshelf
2
+ class APIClientError < StandardError; end
3
+
4
+ class APIClient
5
+ class TimeoutError < APIClientError; end
6
+ class BadResponse < APIClientError; end
7
+ class ServiceUnavaiable < APIClientError; end
8
+ class ServiceNotFound < APIClientError; end
9
+ end
10
+ end
@@ -0,0 +1,55 @@
1
+ require 'json'
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
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ module Berkshelf
2
+ class APIClient
3
+ VERSION = "1.0.0.dev"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'rspec'
4
+ require 'spork'
5
+ require 'berkshelf/api/rspec'
6
+
7
+ Spork.prefork do
8
+ Dir[File.join(File.expand_path("../../spec/support/**/*.rb", __FILE__))].each { |f| require f }
9
+
10
+ RSpec.configure do |config|
11
+ config.include Berkshelf::API::RSpec
12
+
13
+ config.expect_with :rspec do |c|
14
+ c.syntax = :expect
15
+ end
16
+
17
+ config.mock_with :rspec
18
+ config.treat_symbols_as_metadata_keys_with_true_values = true
19
+ config.filter_run focus: true
20
+ config.run_all_when_everything_filtered = true
21
+
22
+ config.before(:suite) { Berkshelf::API::RSpec::Server.start }
23
+ config.before(:all) { Berkshelf::API::Logging.init(location: '/dev/null') }
24
+ end
25
+ end
26
+
27
+ Spork.each_run do
28
+ require 'berkshelf/api-client'
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Berkshelf::APIClient::RemoteCookbook do
4
+ let(:name) { "ruby" }
5
+ let(:version) { "1.2.3" }
6
+ let(:dependencies) { double('dependencies') }
7
+ let(:platforms) { double('platforms') }
8
+ let(:location_type) { "chef_server" }
9
+ let(:location_path) { "http://localhost:8080" }
10
+
11
+ let(:attributes) do
12
+ { dependencies: dependencies, platforms: platforms, location_path: location_path, location_type: location_type }
13
+ end
14
+
15
+ subject { described_class.new(name, version, attributes) }
16
+
17
+ its(:name) { should eql(name) }
18
+ its(:version) { should eql(version) }
19
+ its(:dependencies) { should eql(dependencies) }
20
+ its(:platforms) { should eql(platforms) }
21
+ its(:location_type) { should eql(:chef_server) }
22
+ its(:location_path) { should eql(location_path) }
23
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
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
55
+ end
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: berkshelf-api-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jamie Winsor
8
+ - Michael Ivey
9
+ - Seth Vargo
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-12-26 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: faraday
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: 0.8.5
29
+ - !ruby/object:Gem::Dependency
30
+ name: bundler
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: '1.3'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: '1.3'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rake
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: fuubar
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ version: '1.1'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: '1.1'
71
+ - !ruby/object:Gem::Dependency
72
+ name: rspec
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.13'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ~>
83
+ - !ruby/object:Gem::Version
84
+ version: '2.13'
85
+ - !ruby/object:Gem::Dependency
86
+ name: spork
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: '0.9'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: '0.9'
99
+ - !ruby/object:Gem::Dependency
100
+ name: yard
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ~>
104
+ - !ruby/object:Gem::Version
105
+ version: '0.8'
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ~>
111
+ - !ruby/object:Gem::Version
112
+ version: '0.8'
113
+ description: API Client for communicating with a Berkshelf API server
114
+ email:
115
+ - jamie@vialstudios.com
116
+ - michael.ivey@riotgames.com
117
+ - sethvargo@gmail.com
118
+ executables: []
119
+ extensions: []
120
+ extra_rdoc_files: []
121
+ files:
122
+ - .gitignore
123
+ - Gemfile
124
+ - Guardfile
125
+ - LICENSE
126
+ - README.md
127
+ - Rakefile
128
+ - berkshelf-api-client.gemspec
129
+ - lib/berkshelf/api-client.rb
130
+ - lib/berkshelf/api_client.rb
131
+ - lib/berkshelf/api_client/errors.rb
132
+ - lib/berkshelf/api_client/remote_cookbook.rb
133
+ - lib/berkshelf/api_client/version.rb
134
+ - spec/spec_helper.rb
135
+ - spec/unit/berkshelf/api_client/remote_cookbook_spec.rb
136
+ - spec/unit/berkshelf/api_client_spec.rb
137
+ homepage: http://berkshelf.com
138
+ licenses:
139
+ - Apache 2.0
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - '>='
148
+ - !ruby/object:Gem::Version
149
+ version: 1.9.2
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.0.14
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: API Client for communicating with a Berkshelf API server
161
+ test_files:
162
+ - spec/spec_helper.rb
163
+ - spec/unit/berkshelf/api_client/remote_cookbook_spec.rb
164
+ - spec/unit/berkshelf/api_client_spec.rb
165
+ has_rdoc: