Class: Intown::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/intown/client.rb

Direct Known Subclasses

Artist, Event

Constant Summary collapse

API_VERSION =
2.0

Class Method Summary collapse

Class Method Details

.artist_identifier(params) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
# File 'lib/intown/client.rb', line 60

def artist_identifier(params)
  return musicbrainz_identifier(params[:mbid]) if params[:mbid]
  return facebook_identifier(params[:fbid]) if params[:fbid]
  return encode_name(params[:name]) if params[:name]
  raise ArgumentError, "params must contain one of mbid, fbid, or name"
end

.bad_gateway(response) ⇒ Object



56
57
58
# File 'lib/intown/client.rb', line 56

def bad_gateway(response)
  raise Intown::BadGatewayError, "Bandsintown returned a 502 proxy error. Please retry this request later."
end

.encode_name(name) ⇒ Object



67
68
69
70
71
# File 'lib/intown/client.rb', line 67

def encode_name(name)
  # periods & slashes cause the API to blow up
  # must double-encode the slash
  URI.encode(name).gsub(/\./, "%2E").gsub(/\//, "%252F")
end

.facebook_identifier(fbid) ⇒ Object



77
78
79
# File 'lib/intown/client.rb', line 77

def facebook_identifier(fbid)
  "fbid_#{fbid}"
end

.internal_server_error(response) ⇒ Object



52
53
54
# File 'lib/intown/client.rb', line 52

def internal_server_error(response)
  raise Intown::InternalServerError, "Bandsintown returned a 500 internal server error. Please retry this request later."
end

.musicbrainz_identifier(mbid) ⇒ Object



73
74
75
# File 'lib/intown/client.rb', line 73

def musicbrainz_identifier(mbid)
  "mbid_#{mbid}"
end

.optionsObject



13
14
15
16
17
18
19
# File 'lib/intown/client.rb', line 13

def options
  {
    :query => {
       :app_id => Intown.configuration.app_id, :api_version => API_VERSION, :format => "json"
    }
  }
end

.process_not_acceptable(response) ⇒ Object



40
41
42
# File 'lib/intown/client.rb', line 40

def process_not_acceptable(response)
  raise Intown::InvalidRequestError, "Bandsintown rejected your request.  Please check your input parameters."
end

.process_not_found(response) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/intown/client.rb', line 44

def process_not_found(response)
  json = JSON.parse(response.body)
  if errors = json['errors']
    raise Intown::InvalidRequestError, "app_id is required for this request" if errors.any? {|e| e =~ /app_id param is required/}
  end
  nil
end

.process_response(response) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/intown/client.rb', line 21

def process_response(response)
  case response.code
  when 500 then internal_server_error(response)
  when 502 then bad_gateway(response)
  when 404 then process_not_found(response)
  when 406 then process_not_acceptable(response)
  else
    json_response = JSON.parse(response.body)

    if json_response.is_a? Hash
      Hashie::Mash.new(json_response)
    elsif json_response.is_a? Array
      json_response.map {|e| Hashie::Mash.new(e)}
    else
      json_response
    end
  end
end