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
EMPTY_UNIVERSE =
{}.freeze

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



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

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

Instance Attribute Details

#api_urlString (readonly)

Returns:

  • (String)


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

def api_url
  @api_url
end

Instance Method Details

#universeHash

Returns:

  • (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