Class: Berkshelf::CommunityREST
- Inherits:
-
Object
- Object
- Berkshelf::CommunityREST
- Defined in:
- lib/berkshelf/community_rest.rb
Constant Summary collapse
- V1_API =
"https://supermarket.chef.io".freeze
Instance Attribute Summary collapse
- #api_uri ⇒ String readonly
- #connection ⇒ Berkshelf::RidleyCompat readonly
-
#retries ⇒ Integer
readonly
How many retries to attempt on HTTP requests.
-
#retry_interval ⇒ Float
readonly
Time to wait between retries.
Class Method Summary collapse
- .unpack(target, destination) ⇒ String
- .uri_escape_version(version) ⇒ String
- .version_from_uri(uri) ⇒ String
Instance Method Summary collapse
-
#download(name, version) ⇒ String?
Download and extract target cookbook archive to the local file system, returning its filepath.
- #find(name, version) ⇒ Object
-
#initialize(uri = V1_API, options = {}) ⇒ CommunityREST
constructor
A new instance of CommunityREST.
-
#latest_version(name) ⇒ String
Returns the latest version of the cookbook and its download link.
- #satisfy(name, constraint) ⇒ String
-
#stream(target) ⇒ Tempfile
Stream the response body of a remote URL to a file on the local file system.
- #versions(name) ⇒ Array
Constructor Details
#initialize(uri = V1_API, options = {}) ⇒ CommunityREST
Returns a new instance of CommunityREST.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/berkshelf/community_rest.rb', line 70 def initialize(uri = V1_API, = {}) = .dup = { retries: 5, retry_interval: 0.5, ssl: Berkshelf::Config.instance.ssl }.merge() @api_uri = uri [:server_url] = uri @retries = .delete(:retries) @retry_interval = .delete(:retry_interval) @connection = Berkshelf::RidleyCompatJSON.new(**) end |
Instance Attribute Details
#api_uri ⇒ String (readonly)
53 54 55 |
# File 'lib/berkshelf/community_rest.rb', line 53 def api_uri @api_uri end |
#connection ⇒ Berkshelf::RidleyCompat (readonly)
61 62 63 |
# File 'lib/berkshelf/community_rest.rb', line 61 def connection @connection end |
#retries ⇒ Integer (readonly)
Returns how many retries to attempt on HTTP requests.
56 57 58 |
# File 'lib/berkshelf/community_rest.rb', line 56 def retries @retries end |
#retry_interval ⇒ Float (readonly)
Returns time to wait between retries.
59 60 61 |
# File 'lib/berkshelf/community_rest.rb', line 59 def retry_interval @retry_interval end |
Class Method Details
.unpack(target, destination) ⇒ String
13 14 15 16 17 18 19 20 21 |
# File 'lib/berkshelf/community_rest.rb', line 13 def unpack(target, destination) if is_gzip_file(target) || is_tar_file(target) Mixlib::Archive.new(target).extract(destination) else raise Berkshelf::UnknownCompressionType.new(target, destination) end destination end |
.uri_escape_version(version) ⇒ String
26 27 28 |
# File 'lib/berkshelf/community_rest.rb', line 26 def uri_escape_version(version) version.to_s.tr(".", "_") end |
Instance Method Details
#download(name, version) ⇒ String?
Download and extract target cookbook archive to the local file system, returning its filepath.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/berkshelf/community_rest.rb', line 91 def download(name, version) archive = stream(find(name, version)["file"]) scratch = Dir.mktmpdir extracted = self.class.unpack(archive.path, scratch) if File.cookbook?(extracted) extracted else Dir.glob("#{extracted}/*").find do |dir| File.cookbook?(dir) end end ensure archive.unlink unless archive.nil? end |
#find(name, version) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/berkshelf/community_rest.rb', line 107 def find(name, version) body = connection.get("cookbooks/#{name}/versions/#{self.class.uri_escape_version(version)}") # Artifactory responds with a 200 and blank body for unknown cookbooks. raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") if body.nil? body rescue CookbookNotFound raise rescue Berkshelf::APIClient::ServiceNotFound raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") rescue raise CommunitySiteError.new(api_uri, "'#{name}' (#{version})") end |
#latest_version(name) ⇒ String
Returns the latest version of the cookbook and its download link.
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/berkshelf/community_rest.rb', line 125 def latest_version(name) body = connection.get("cookbooks/#{name}") # Artifactory responds with a 200 and blank body for unknown cookbooks. raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") if body.nil? self.class.version_from_uri body["latest_version"] rescue Berkshelf::APIClient::ServiceNotFound raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") rescue raise CommunitySiteError.new(api_uri, "the latest version of '#{name}'") end |
#satisfy(name, constraint) ⇒ String
161 162 163 164 165 |
# File 'lib/berkshelf/community_rest.rb', line 161 def satisfy(name, constraint) Semverse::Constraint.satisfy_best(constraint, versions(name)).to_s rescue Semverse::NoSolutionError nil end |
#stream(target) ⇒ Tempfile
Stream the response body of a remote URL to a file on the local file system
173 174 175 176 177 178 179 180 181 |
# File 'lib/berkshelf/community_rest.rb', line 173 def stream(target) local = Tempfile.new("community-rest-stream") local.binmode Retryable.retryable(tries: retries, on: Berkshelf::APIClientError, sleep: retry_interval) do connection.streaming_request(target, {}, local) end ensure local.close(false) unless local.nil? end |
#versions(name) ⇒ Array
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/berkshelf/community_rest.rb', line 141 def versions(name) body = connection.get("cookbooks/#{name}") # Artifactory responds with a 200 and blank body for unknown cookbooks. raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") if body.nil? body["versions"].collect do |version_uri| self.class.version_from_uri(version_uri) end rescue Berkshelf::APIClient::ServiceNotFound raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") rescue raise CommunitySiteError.new(api_uri, "versions of '#{name}'") end |