Class: Berkshelf::API::SiteConnector::Supermarket

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

init, #logger

Constructor Details

#initialize(options = {}) ⇒ Supermarket

Returns a new instance of Supermarket.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :url (String) — default: {V1_API}

    url of community site



40
41
42
# File 'lib/berkshelf/api/site_connector/supermarket.rb', line 40

def initialize(options = {})
  @api_url = options[:url] || V1_API
end

Instance Attribute Details

#api_urlString (readonly)

Returns:

  • (String)


36
37
38
# File 'lib/berkshelf/api/site_connector/supermarket.rb', line 36

def api_url
  @api_url
end

Instance Method Details

#universeHash

Returns:

  • (Hash)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/berkshelf/api/site_connector/supermarket.rb', line 45

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}"
rescue Timeout::Error
  log.error "Failed to get `#{universe_url}' in #{TIMEOUT} seconds!"
rescue SocketError,
       Errno::ECONNREFUSED,
       Errno::ECONNRESET,
       Errno::ENETUNREACH,
       OpenURI::HTTPError => e
  log.error "Failed to get `#{universe_url}': #{e}"
end