Class: Bandsintown::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bandsintown/base.rb

Direct Known Subclasses

Artist, Event, Venue

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bandsintown_urlObject

Returns the value of attribute bandsintown_url.



4
5
6
# File 'lib/bandsintown/base.rb', line 4

def bandsintown_url
  @bandsintown_url
end

Class Method Details

.check_for_errors(json) ⇒ Object



24
25
26
27
28
# File 'lib/bandsintown/base.rb', line 24

def self.check_for_errors(json)
  if json.is_a?(Hash) && json.has_key?("errors")
    raise Bandsintown::APIError.new(json["errors"].join(", "))
  end
end

.connectionObject



14
15
16
# File 'lib/bandsintown/base.rb', line 14

def self.connection
  @connection ||= Bandsintown::Connection.new("http://api.bandsintown.com")
end

.parse(response) ⇒ Object



18
19
20
21
22
# File 'lib/bandsintown/base.rb', line 18

def self.parse(response)
  json = JSON.parse(response)
  check_for_errors(json)
  json
end

.request(http_method, api_method, args = {}) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/bandsintown/base.rb', line 6

def self.request(http_method, api_method, args={})
  case http_method
  when :get  then self.connection.get(self.resource_path, api_method, args)
  when :post then self.connection.post(self.resource_path, api_method, args)
  else raise ArgumentError, "only :get and :post requests are supported"
  end
end

.request_and_parse(http_method, api_method, args = {}) ⇒ Object



30
31
32
# File 'lib/bandsintown/base.rb', line 30

def self.request_and_parse(http_method, api_method, args={})
  parse(request(http_method, api_method, args))
end

Instance Method Details

#to_hashObject



34
35
36
37
38
39
40
41
42
# File 'lib/bandsintown/base.rb', line 34

def to_hash
  hash = {}
  self.instance_variables.each do |ivar|
    value = self.instance_variable_get(ivar)
    next if value.blank?
    hash[:"#{ivar.gsub('@', '')}"] = value
  end
  hash
end