Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
move request logic to new objects
  • Loading branch information
Greggory Rothmeier committed Feb 2, 2013
1 parent 6b51979 commit e993aef
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
19 changes: 12 additions & 7 deletions lib/lastfm.rb
Expand Up @@ -5,6 +5,8 @@

require 'lastfm/util'
require 'lastfm/response'
require 'lastfm/http_request.rb'
require 'lastfm/https_request.rb'
require 'lastfm/method_category/base'
require 'lastfm/method_category/album'
require 'lastfm/method_category/artist'
Expand All @@ -20,11 +22,6 @@
require 'lastfm/method_category/radio'

class Lastfm
API_ROOT = 'http://ws.audioscrobbler.com/2.0'

include HTTParty
base_uri API_ROOT

attr_accessor :session

class Error < StandardError; end
Expand Down Expand Up @@ -90,7 +87,7 @@ def radio
MethodCategory::Radio.new(self)
end

def request(method, params = {}, http_method = :get, with_signature = false, with_session = false)
def request(method, params = {}, http_method = :get, with_signature = false, with_session = false, use_https = false)
params[:method] = method
params[:api_key] = @api_key

Expand All @@ -106,7 +103,15 @@ def request(method, params = {}, http_method = :get, with_signature = false, wit
params.update(:sk => @session) if with_session
params.update(:api_sig => Digest::MD5.hexdigest(build_method_signature(params))) if with_signature

response = Response.new(self.class.send(http_method, '/', (http_method == :post ? :body : :query) => params).body)
request_args = [http_method, '/', (http_method == :post ? :body : :query) => params]

response = if use_https
HTTPSRequest.send(*request_args)
else
HTTPRequest.send(*request_args)
end

response = Response.new(response.body)
unless response.success?
raise ApiError.new(response.message, response.error)
end
Expand Down
6 changes: 6 additions & 0 deletions lib/lastfm/http_request.rb
@@ -0,0 +1,6 @@
class HTTPRequest
API_ROOT = 'http://ws.audioscrobbler.com/2.0'

include HTTParty
base_uri API_ROOT
end
6 changes: 6 additions & 0 deletions lib/lastfm/https_request.rb
@@ -0,0 +1,6 @@
class HTTPSRequest
HTTPS_API_ROOT = 'https://ws.audioscrobbler.com/2.0'

include HTTParty
base_uri HTTPS_API_ROOT
end
18 changes: 7 additions & 11 deletions spec/lastfm_spec.rb
Expand Up @@ -3,10 +3,6 @@
describe "Lastfm" do
before { init_lastfm }

it 'should have base_uri' do
Lastfm.base_uri.should == 'http://ws.audioscrobbler.com/2.0'
end

describe '.new' do
it 'should instantiate' do
@lastfm.should be_an_instance_of(Lastfm)
Expand All @@ -16,7 +12,7 @@
describe '#request' do
it 'should post' do
mock_response = mock(HTTParty::Response)
@lastfm.class.should_receive(:post).with('/', :body => {
HTTPRequest.should_receive(:post).with('/', :body => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
Expand All @@ -27,7 +23,7 @@

it 'should post with signature' do
mock_response = mock(HTTParty::Response)
@lastfm.class.should_receive(:post).with('/', :body => {
HTTPRequest.should_receive(:post).with('/', :body => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
Expand All @@ -40,7 +36,7 @@
it 'should post with signature and session (request with authentication)' do
mock_response = mock(HTTParty::Response)
@lastfm.session = 'abcdef'
@lastfm.class.should_receive(:post).with('/', :body => {
HTTPRequest.should_receive(:post).with('/', :body => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
Expand All @@ -53,7 +49,7 @@

it 'should get' do
mock_response = mock(HTTParty::Response)
@lastfm.class.should_receive(:get).with('/', :query => {
HTTPRequest.should_receive(:get).with('/', :query => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
Expand All @@ -64,7 +60,7 @@

it 'should get with signature (request for authentication)' do
mock_response = mock(HTTParty::Response)
@lastfm.class.should_receive(:get).with('/', :query => {
HTTPRequest.should_receive(:get).with('/', :query => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
Expand All @@ -77,7 +73,7 @@
it 'should get with signature and session' do
mock_response = mock(HTTParty::Response)
@lastfm.session = 'abcdef'
@lastfm.class.should_receive(:get).with('/', :query => {
HTTPRequest.should_receive(:get).with('/', :query => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
Expand All @@ -91,7 +87,7 @@
it 'should raise an error if an api error is ocuured' do
mock_response = mock(HTTParty::Response)
mock_response.should_receive(:body).and_return(open(fixture('ng.xml')).read)
@lastfm.class.should_receive(:post).and_return(mock_response)
HTTPRequest.should_receive(:post).and_return(mock_response)

lambda {
@lastfm.request('xxx.yyy', { :foo => 'bar' }, :post)
Expand Down

0 comments on commit e993aef

Please sign in to comment.