Class: Berkshelf::CommunityREST
- Inherits:
-
Faraday::Connection
- Object
- Faraday::Connection
- Berkshelf::CommunityREST
- Defined in:
- lib/berkshelf/community_rest.rb
Constant Summary collapse
- V1_API =
'https://supermarket.getchef.com'.freeze
Instance Attribute Summary collapse
- #api_uri ⇒ String 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 = Dir.mktmpdir) ⇒ 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 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/berkshelf/community_rest.rb', line 70 def initialize(uri = V1_API, = {}) = .reverse_merge(retries: 5, retry_interval: 0.5) @api_uri = uri @retries = .delete(:retries) @retry_interval = .delete(:retry_interval) [:builder] ||= Faraday::RackBuilder.new do |b| b.response :parse_json b.response :gzip b.response :follow_redirects b.request :retry, max: @retries, interval: @retry_interval, exceptions: [Faraday::Error::TimeoutError] b.adapter :net_http end super(api_uri, ) end |
Instance Attribute Details
#api_uri ⇒ String (readonly)
55 56 57 |
# File 'lib/berkshelf/community_rest.rb', line 55 def api_uri @api_uri end |
#retries ⇒ Integer (readonly)
Returns how many retries to attempt on HTTP requests.
58 59 60 |
# File 'lib/berkshelf/community_rest.rb', line 58 def retries @retries end |
#retry_interval ⇒ Float (readonly)
Returns time to wait between retries.
61 62 63 |
# File 'lib/berkshelf/community_rest.rb', line 61 def retry_interval @retry_interval end |
Class Method Details
.unpack(target, destination = Dir.mktmpdir) ⇒ String
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/berkshelf/community_rest.rb', line 13 def unpack(target, destination = Dir.mktmpdir) if is_gzip_file(target) Archive::Tar::Minitar.unpack(Zlib::GzipReader.new(File.open(target, 'rb')), destination) elsif is_tar_file(target) Archive::Tar::Minitar.unpack(target, destination) else raise Berkshelf::UnknownCompressionType.new(target) end FileUtils.rm_rf Dir.glob("#{destination}/**/PaxHeader") destination end |
.uri_escape_version(version) ⇒ String
28 29 30 |
# File 'lib/berkshelf/community_rest.rb', line 28 def uri_escape_version(version) version.to_s.gsub('.', '_') end |
Instance Method Details
#download(name, version) ⇒ String?
Download and extract target cookbook archive to the local file system, returning its filepath.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/berkshelf/community_rest.rb', line 101 def download(name, version) archive = stream(find(name, version)[:file]) extracted = self.class.unpack(archive.path) if File.cookbook?(extracted) extracted else Dir.glob(File.join(extracted, '*')).find do |dir| File.cookbook?(dir) end end ensure archive.unlink unless archive.nil? end |
#find(name, version) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/berkshelf/community_rest.rb', line 116 def find(name, version) response = get("cookbooks/#{name}/versions/#{self.class.uri_escape_version(version)}") case response.status when (200..299) response.body when 404 raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") else raise CommunitySiteError.new(api_uri, "'#{name}' (#{version})") end end |
#latest_version(name) ⇒ String
Returns the latest version of the cookbook and its download link.
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/berkshelf/community_rest.rb', line 132 def latest_version(name) response = get("cookbooks/#{name}") case response.status when (200..299) self.class.version_from_uri response.body['latest_version'] when 404 raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") else raise CommunitySiteError.new(api_uri, "the latest version of '#{name}'") end end |
#satisfy(name, constraint) ⇒ String
167 168 169 170 171 |
# File 'lib/berkshelf/community_rest.rb', line 167 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
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/berkshelf/community_rest.rb', line 179 def stream(target) local = Tempfile.new('community-rest-stream') local.binmode retryable(tries: retries, on: OpenURI::HTTPError, sleep: retry_interval) do open(target, 'rb', ) do |remote| local.write(remote.read) end end local ensure local.close(false) unless local.nil? end |
#versions(name) ⇒ Array
148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/berkshelf/community_rest.rb', line 148 def versions(name) response = get("cookbooks/#{name}") case response.status when (200..299) response.body['versions'].collect do |version_uri| self.class.version_from_uri(version_uri) end when 404 raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") else raise CommunitySiteError.new(api_uri, "versions of '#{name}'") end end |