Class: Intown::Client
Constant Summary collapse
- API_VERSION =
2.0
Class Method Summary collapse
- .artist_identifier(params) ⇒ Object
- .bad_gateway(response) ⇒ Object
- .encode_name(name) ⇒ Object
- .facebook_identifier(fbid) ⇒ Object
- .internal_server_error(response) ⇒ Object
- .invalid_response_error(response) ⇒ Object
- .musicbrainz_identifier(mbid) ⇒ Object
- .options ⇒ Object
- .process_not_acceptable(response) ⇒ Object
- .process_not_found(response) ⇒ Object
- .process_response(response) ⇒ Object
Class Method Details
.artist_identifier(params) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/intown/client.rb', line 66 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
58 59 60 |
# File 'lib/intown/client.rb', line 58 def bad_gateway(response) raise Intown::BadGatewayError, "Bandsintown returned a 502 proxy error. Please retry this request later." end |
.encode_name(name) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/intown/client.rb', line 73 def encode_name(name) # periods & slashes cause the API to blow up # must double-encode the slash # question marks break the api as well sanitized_name = name.gsub(/\?/, "").strip URI.encode(sanitized_name).gsub(/\./, "%2E").gsub(/\//, "%252F") end |
.facebook_identifier(fbid) ⇒ Object
85 86 87 |
# File 'lib/intown/client.rb', line 85 def facebook_identifier(fbid) "fbid_#{fbid}" end |
.internal_server_error(response) ⇒ Object
54 55 56 |
# File 'lib/intown/client.rb', line 54 def internal_server_error(response) raise Intown::InternalServerError, "Bandsintown returned a 500 internal server error. Please retry this request later." end |
.invalid_response_error(response) ⇒ Object
62 63 64 |
# File 'lib/intown/client.rb', line 62 def invalid_response_error(response) raise Intown::InvalidResponseError, "JSON parsing the Bandsintown response failed with: status_code: #{response.code}, body: #{response.body}" end |
.musicbrainz_identifier(mbid) ⇒ Object
81 82 83 |
# File 'lib/intown/client.rb', line 81 def musicbrainz_identifier(mbid) "mbid_#{mbid}" end |
.options ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/intown/client.rb', line 13 def { :query => { :app_id => Intown.configuration.app_id, :api_version => API_VERSION, :format => "json" } } end |
.process_not_acceptable(response) ⇒ Object
42 43 44 |
# File 'lib/intown/client.rb', line 42 def process_not_acceptable(response) raise Intown::InvalidRequestError, "Bandsintown rejected your request. Please check your input parameters." end |
.process_not_found(response) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/intown/client.rb', line 46 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 39 40 |
# 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 rescue JSON::ParserError => e invalid_response_error(response) end |