Class: Berkshelf::API::SiteConnector::Supermarket
- Inherits:
-
Object
- Object
- Berkshelf::API::SiteConnector::Supermarket
- Includes:
- Logging
- Defined in:
- lib/berkshelf/api/site_connector/supermarket.rb
Constant Summary collapse
- V1_API =
The default API server
'https://supermarket.getchef.com/'.freeze
- TIMEOUT =
The timeout for the HTTP request
15
- EMPTY_UNIVERSE =
{}.freeze
Instance Attribute Summary collapse
- #api_url ⇒ String readonly
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Supermarket
constructor
A new instance of Supermarket.
- #universe ⇒ Hash
Methods included from Logging
Constructor Details
#initialize(options = {}) ⇒ Supermarket
Returns a new instance of Supermarket.
41 42 43 |
# File 'lib/berkshelf/api/site_connector/supermarket.rb', line 41 def initialize( = {}) @api_url = [:url] || V1_API end |
Instance Attribute Details
#api_url ⇒ String (readonly)
37 38 39 |
# File 'lib/berkshelf/api/site_connector/supermarket.rb', line 37 def api_url @api_url end |
Instance Method Details
#universe ⇒ Hash
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/berkshelf/api/site_connector/supermarket.rb', line 46 def universe universe_url = URI.parse(File.join(api_url, 'universe.json')).to_s log.debug "Loading universe from `#{universe_url}'..." Timeout.timeout(TIMEOUT) do response = open(universe_url, 'User-Agent' => USER_AGENT) JSON.parse(response.read) end rescue JSON::ParserError => e log.error "Failed to parse JSON: #{e}" EMPTY_UNIVERSE rescue Timeout::Error log.error "Failed to get `#{universe_url}' in #{TIMEOUT} seconds!" EMPTY_UNIVERSE rescue SocketError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ENETUNREACH, OpenURI::HTTPError => e log.error "Failed to get `#{universe_url}': #{e}" EMPTY_UNIVERSE end |