Class: Idiomag::REST

Inherits:
Object
  • Object
show all
Defined in:
lib/idiomag/rest.rb

Class Method Summary collapse

Class Method Details

.fetch(resource, query = {}, parse = true) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/idiomag/rest.rb', line 5

def self.fetch(resource, query={}, parse=true)
  raise ArgumentError, 'api key missing' if Base.api_key.blank?
  
  options = {:query => {:key => Base.api_key}}
  options[:query].merge!(query)
  options[:format] = :json if parse
  begin
    data = HTTParty.get(API_URL + resource + "/json", options)
  rescue Net::HTTPServerException => e
      raise ArgumentError, 'invalid user, artist or tag' if e.message =~ /400/
  end
  raise InvalidResult if data.blank?
  data
end