class Falcore::Fetcher

Public Class Methods

get(url) click to toggle source

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.

@raise [RuntimeError]

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

@param [String] url

the url to get

@return [Hash]

the parsed JSON hash
# 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