Skip to content

Commit

Permalink
added APILimitReachedException
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityblast committed Nov 13, 2010
1 parent e50864b commit ce99254
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/musix_match/api/base.rb
@@ -1,6 +1,7 @@
module MusixMatch
module API
class AuthenticationFailedException < Exception; end
class APILimiReachedException < Exception; end
class APIKeyNotSpecifiedException < Exception; end

class Base
Expand Down Expand Up @@ -31,7 +32,10 @@ def self.get(method, params={})
when String
JSON.parse(response.parsed_response)
end
raise AuthenticationFailedException.new('Authentication failed, probably because of a bad API key') if parsed_response['message']['header']['status_code'] == 401
case parsed_response['message']['header']['status_code']
when 401 then raise AuthenticationFailedException.new('Authentication failed, probably because of a bad API key')
when 402 then raise APILimiReachedException.new('A limit was reached, either you exceeded per hour requests limits or your balance is insufficient')
end
parsed_response
end

Expand Down
19 changes: 17 additions & 2 deletions spec/api/base_spec.rb
Expand Up @@ -30,7 +30,7 @@
result.should == parsed_response
end

it "should raise an error" do
it "should raise an error AuthenticationFailedException" do
lambda do
MusixMatch::API::Base.api_key = 'API_KEY'
method = 'lyrics.search'
Expand All @@ -43,5 +43,20 @@
result = MusixMatch::API::Base.get(method, params)
result.should == parsed_response
end.should raise_error(MusixMatch::API::AuthenticationFailedException)
end
end

it "should raise an error APIAccessLimitException" do
lambda do
MusixMatch::API::Base.api_key = 'API_KEY'
method = 'lyrics.search'
params = {:q_artist => 'artist name'}
url = MusixMatch::API::Base.url_for(method, params)
response = mock(HTTParty::Response)
parsed_response = {'message' => {'header' => {'status_code' => 402}}}
response.should_receive(:parsed_response).twice.and_return(parsed_response)
HTTParty.should_receive(:get).with(url).and_return(response)
result = MusixMatch::API::Base.get(method, params)
result.should == parsed_response
end.should raise_error(MusixMatch::API::APILimiReachedException)
end
end

0 comments on commit ce99254

Please sign in to comment.