Class: Falcore::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/falcore/fetcher.rb

Class Method Summary collapse

Class Method Details

.get(url) ⇒ Hash

Get the JSON at the given URL and parse it as such. This method is just a very thin wrapper around Ruby’s native OpenURI with some error- handling magic.

Parameters:

  • url (String)

    the url to get

Returns:

  • (Hash)

    the parsed JSON hash

Raises:

  • (RuntimeError)

    if the request fails (40X, 50X, bad-URL) or if the JSON is invalid



40
41
42
43
44
45
46
47
# File 'lib/falcore/fetcher.rb', line 40

def get(url)
  response = open(url)
  JSON.parse(response.read)
rescue Errno::ENOENT, OpenURI::HTTPError, SocketError => e
  raise "Failed to GET '#{url}': #{e.class} - #{e.message}"
rescue JSON::ParserError
  raise 'Invalid JSON!'
end