FIX: parallel requests for LastFM
This commit is contained in:
+4
-4
@@ -42,8 +42,8 @@ GEM
|
||||
hashie (1.0.0)
|
||||
httparty (0.7.7)
|
||||
crack (= 0.1.8)
|
||||
i18n (0.5.0)
|
||||
jquery-rails (1.0.5)
|
||||
i18n (0.6.0)
|
||||
jquery-rails (1.0.7)
|
||||
railties (~> 3.0)
|
||||
thor (~> 0.14)
|
||||
json (1.5.1)
|
||||
@@ -55,7 +55,7 @@ GEM
|
||||
mime-types (1.16)
|
||||
oauth (0.4.4)
|
||||
polyglot (0.3.1)
|
||||
rack (1.2.2)
|
||||
rack (1.2.3)
|
||||
rack-mount (0.6.14)
|
||||
rack (>= 1.0.0)
|
||||
rack-test (0.5.7)
|
||||
@@ -73,7 +73,7 @@ GEM
|
||||
activesupport (= 3.0.5)
|
||||
rake (>= 0.8.7)
|
||||
thor (~> 0.14.4)
|
||||
rake (0.8.7)
|
||||
rake (0.9.0)
|
||||
ruby_parser (2.0.6)
|
||||
sexp_processor (~> 3.0)
|
||||
sexp_processor (3.0.5)
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
class HomeController < ApplicationController
|
||||
def index
|
||||
@tracks = LastFM::Chart.getTopTracks 25
|
||||
@artists = LastFM::Chart.getTopArtists 10
|
||||
LastFM::Chart.getTopTracks 25
|
||||
LastFM::Chart.getTopArtists 10
|
||||
|
||||
LastFM::LastFMRequest.run
|
||||
|
||||
@artists = LastFM::LastFMRequest.results[0]
|
||||
@tracks = LastFM::LastFMRequest.results[1]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -13,11 +13,16 @@ module LastFM
|
||||
default_params :format => 'json', :autocorrect => '1'
|
||||
#debug_output $>
|
||||
format :json
|
||||
@@results = Array.new()
|
||||
|
||||
def self.api_key=(key)
|
||||
@@api_key = key
|
||||
default_params :api_key => key
|
||||
end
|
||||
|
||||
def self.results
|
||||
return @@results
|
||||
end
|
||||
|
||||
def self.run
|
||||
puts "<< ----------- hydra run ----------"
|
||||
@@ -38,10 +43,8 @@ module LastFM
|
||||
end
|
||||
|
||||
def self.chart_request method, node, limit = nil, page = nil
|
||||
# hydra = Typhoeus::Hydra.new
|
||||
request = Typhoeus::Request.new(base_uri, :params => default_params.merge(:method => "chart.#{method.to_s}", :limit => limit, :page => page))
|
||||
|
||||
# handle_response method, node, request
|
||||
puts '----------------------'
|
||||
puts request.url
|
||||
puts '----------------------'
|
||||
@@ -52,17 +55,13 @@ module LastFM
|
||||
|
||||
hash = Hashie::Mash.new(JSON.parse(response.body))
|
||||
hash = method.to_s == "search" ? hash.results.send(node.to_sym) : hash.send(node.to_sym)
|
||||
@@results.push(hash)
|
||||
elsif response.timed_out?
|
||||
puts ">> -- '#{method.to_s}' -- TIMED OUT -- <<"
|
||||
end
|
||||
end
|
||||
|
||||
request.on_complete
|
||||
|
||||
puts "---- '#{method.to_s}' request is queued ----" if @@hydra.queue request
|
||||
# hydra.queue request
|
||||
|
||||
# hydra.run
|
||||
# prepare_result method, node, get('/', :query => { :method => "chart.#{method.to_s}", :limit => limit, :page => page })
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user