From 9c3532d8bb753783846eceb1c4da27ed30caa867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Bucho=CC=88ster?= Date: Tue, 24 May 2011 22:02:00 +0200 Subject: [PATCH] managed to get the response from lib to controller, typhoeus is basically working, validations still required --- Rakefile | 10 +++ app/controllers/artists_controller.rb | 16 ++-- app/controllers/home_controller.rb | 9 +-- app/controllers/search_controller.rb | 15 ++-- app/controllers/tracks_controller.rb | 2 +- lib/last_fm/artist.rb | 8 +- lib/last_fm/last_fm_request.rb | 109 +++++++++++++------------- 7 files changed, 83 insertions(+), 86 deletions(-) diff --git a/Rakefile b/Rakefile index 1251c37..30851a6 100644 --- a/Rakefile +++ b/Rakefile @@ -4,4 +4,14 @@ require File.expand_path('../config/application', __FILE__) require 'rake' +module ::Nineminutes + class Application + include Rake::DSL + end +end + +module ::RakeFileUtils + extend Rake::FileUtilsExt +end + Nineminutes::Application.load_tasks diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index aa24754..05f81ed 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -2,18 +2,14 @@ require 'uri' class ArtistsController < ApplicationController - def index - @albums = LastFM::Artist.getTopAlbums "Avril Lavigne", 20 - puts @albums - end - def show @artist = LastFM::Artist.getInfo CGI.unescape(params[:id]) - @tracks = LastFM::Artist.getTopTracks @artist.name, 10 - @albums = LastFM::Artist.getTopAlbums @artist.name, 8 - @images = LastFM::Artist.getImages @artist.name, 18 - @related= LastFM::Artist.getSimilar @artist.name, 6 - puts @artist.to_yaml + @tracks = LastFM::Artist.getTopTracks CGI.unescape(params[:id]), 10 + @albums = LastFM::Artist.getTopAlbums CGI.unescape(params[:id]), 8 + @images = LastFM::Artist.getImages CGI.unescape(params[:id]), 18 + @related= LastFM::Artist.getSimilar CGI.unescape(params[:id]), 6 + + LastFM::LastFMRequest.run_queue! end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 9134dde..4050b5b 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,12 +1,9 @@ class HomeController < ApplicationController def index - LastFM::Chart.getTopTracks 25 - LastFM::Chart.getTopArtists 10 + @tracks = LastFM::Chart.getTopTracks 25 + @artists = LastFM::Chart.getTopArtists 10 - LastFM::LastFMRequest.run - - @artists = LastFM::LastFMRequest.results[0] - @tracks = LastFM::LastFMRequest.results[1] + LastFM::LastFMRequest.run_queue! end end diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 949f06b..dd60a2a 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -1,16 +1,13 @@ class SearchController < ApplicationController def index - if params[:q].nil? - params[:q] = "foobar" - end + redirect_to home_index_path if params[:q].blank? - if !params[:q].nil? - @artists = LastFM::Artist.search(params[:q].to_s.lstrip, 8) - @tracks = LastFM::Track.search(params[:q].to_s.lstrip, 24) - @albums = LastFM::Album.search(params[:q].to_s.lstrip, 16) - end - puts params + @artists = LastFM::Artist.search(params[:q].to_s.lstrip, 8) + @tracks = LastFM::Track.search(params[:q].to_s.lstrip, 24) + @albums = LastFM::Album.search(params[:q].to_s.lstrip, 16) + + LastFM::LastFMRequest.run_queue! end end \ No newline at end of file diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb index 9240225..215cc97 100644 --- a/app/controllers/tracks_controller.rb +++ b/app/controllers/tracks_controller.rb @@ -2,4 +2,4 @@ class TracksController < ApplicationController def index end -end +end \ No newline at end of file diff --git a/lib/last_fm/artist.rb b/lib/last_fm/artist.rb index d0762e2..cbd0b35 100644 --- a/lib/last_fm/artist.rb +++ b/lib/last_fm/artist.rb @@ -27,11 +27,11 @@ module LastFM # return: albums ( name, mbid, album url, image urls ( different sizes ) ) ordered by popularity def self.getTopAlbums artist, limit = nil, page = nil - @albums = artist_request "getTopAlbums", "topalbums", artist, limit, page - if @albums["@attr"].total.to_i == 1 - @albums.album = [@albums.album] # wrap with Array + artist_request "getTopAlbums", "topalbums", artist, limit, page do |albums| + if albums["@attr"].total.to_i == 1 + albums.album = [albums.album] # wrap with Array + end end - @albums end def self.getImages artist, limit = nil diff --git a/lib/last_fm/last_fm_request.rb b/lib/last_fm/last_fm_request.rb index 51ddc82..4fee99e 100644 --- a/lib/last_fm/last_fm_request.rb +++ b/lib/last_fm/last_fm_request.rb @@ -1,90 +1,87 @@ -require 'httparty' require 'json' require 'hashie' module LastFM class LastFMRequest - include HTTParty - base_uri 'http://ws.audioscrobbler.com/2.0//' - parser Proc.new { |data| Hashie::Mash.new(JSON.parse(data)) } + @@base_uri = 'http://ws.audioscrobbler.com/2.0/' + @@default_params = { :format => 'json', :autocorrect => '1' } @@hydra = Typhoeus::Hydra.new - default_params :format => 'json', :autocorrect => '1' - #debug_output $> - format :json - @@results = Array.new() + @@results = {} def self.api_key=(key) @@api_key = key - default_params :api_key => key + @@default_params[:api_key] = key end + def self.base_uri + return @@base_uri + end + + def self.default_params + return @@default_params + end + def self.results return @@results end - - def self.run - puts "<< ----------- hydra run ----------" + + def self.run_queue! + puts "<< ----------- started request queue ---------" @@hydra.run - puts "<< ----------- hydra finished ----------" + puts "<< -------------- queue finished -------------" end - def self.artist_request method, node, q, limit = nil, page = nil - prepare_result method, node, get('/', :query => { :method => "artist.#{method.to_s}", :artist => q, :limit => limit, :page => page }) + def self.artist_request method, node, q, limit = nil, page = nil, &block + klass_name = self.name.split('::').last.downcase! + request_params = default_params.merge( :method => "#{klass_name}.#{method.to_s}", :artist => q, :limit => limit, :page => page ) + create_and_queue_request klass_name, method, node, block, request_params end - def self.album_request method, node, q, artist = nil, limit = nil, page = nil - prepare_result method, node, get('/', :query => { :method => "album.#{method.to_s}", :album => q, :artist => artist, :limit => limit, :page => page }) + def self.album_request method, node, q, artist = nil, limit = nil, page = nil, &block + klass_name = self.name.split('::').last.downcase! + request_params = default_params.merge( :method => "#{klass_name}.#{method.to_s}", :album => q, :artist => artist, :limit => limit, :page => page ) + create_and_queue_request klass_name, method, node, block, request_params end - def self.track_request method, node, q, artist = nil, limit = nil, page = nil - prepare_result method, node, get('/', :query => { :method => "track.#{method.to_s}", :track => q, :artist => artist, :limit => limit, :page => page }) + def self.track_request method, node, q, artist = nil, limit = nil, page = nil, &block + klass_name = self.name.split('::').last.downcase! + request_params = default_params.merge( :method => "#{klass_name}.#{method.to_s}", :track => q, :artist => artist, :limit => limit, :page => page ) + create_and_queue_request klass_name, method, node, block, request_params end - def self.chart_request method, node, limit = nil, page = nil - request = Typhoeus::Request.new(base_uri, :params => default_params.merge(:method => "chart.#{method.to_s}", :limit => limit, :page => page)) - - puts '----------------------' - puts request.url - puts '----------------------' - - request.on_complete do |response| - if response.success? - puts "---- '#{method.to_s}' -- TIME: #{response.time.to_s} ----" - - 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 - - puts "---- '#{method.to_s}' request is queued ----" if @@hydra.queue request + def self.chart_request method, node, limit = nil, page = nil, &block + klass_name = self.name.split('::').last.downcase! + request_params = default_params.merge( :method => "#{klass_name}.#{method.to_s}", :limit => limit, :page => page ) + create_and_queue_request klass_name, method, node, block, request_params end private - def self.handle_response method, node, request - puts '----------------------' - puts request.url - puts '----------------------' - request.on_complete do |response| - if response.success? - puts "---- '#{method.to_s}' -- TIME: #{response.time.to_s} ----" - - hash = Hashie::Mash.new(JSON.parse(response.body)) - method.to_s == "search" ? hash.results.send(node.to_sym) : hash.send(node.to_sym) - puts hash - elsif response.timed_out? - puts ">> -- '#{method.to_s}' -- TIMED OUT -- <<" - end - end + def self.create_and_queue_request klass_name, method, node, block, request_params + request = Typhoeus::Request.new(base_uri, :params => request_params) + handle_response request, klass_name, method, node, block + + puts ">> ---- '#{klass_name}.#{method.to_s}' request is queued ----" if @@hydra.queue request + @@results[(klass_name + "_" + method).to_sym] = Hashie::Mash.new end - def self.prepare_result method, node, response_hash - method.to_s == "search" ? hash.results.send(node.to_sym) : hash.send(node.to_sym) + def self.handle_response request, klass_name, method, node, block + request.on_complete do |response| + if response.success? + puts "<< ---- '#{klass_name}.#{method.to_s}' -- TIME: #{response.time.to_s} ----" + + 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[(klass_name + "_" + method).to_sym].update hash + + block.call(@@results[(klass_name + "_" + method).to_sym]) unless block.nil? + elsif response.timed_out? + puts ">> -- '#{klass_name}.#{method.to_s}' -- TIMED OUT -- <<" + end + end end end