implemented a basic validator module for lastfm results (improvement necessary)

This commit is contained in:
Thomas Buchöster
2011-06-05 16:56:05 +02:00
parent 9c3532d8bb
commit 0e92f0c0ba
11 changed files with 111 additions and 48 deletions
+2
View File
@@ -1,5 +1,7 @@
source 'http://rubygems.org'
gem 'rake', '!= 0.9.0'
gem 'rails', '3.0.5'
gem 'jquery-rails', '>= 0.2.6'
+4 -3
View File
@@ -43,7 +43,7 @@ GEM
httparty (0.7.7)
crack (= 0.1.8)
i18n (0.6.0)
jquery-rails (1.0.7)
jquery-rails (1.0.9)
railties (~> 3.0)
thor (~> 0.14)
json (1.5.1)
@@ -73,7 +73,7 @@ GEM
activesupport (= 3.0.5)
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.9.0)
rake (0.9.1)
ruby_parser (2.0.6)
sexp_processor (~> 3.0)
sexp_processor (3.0.5)
@@ -88,7 +88,7 @@ GEM
mime-types
mime-types
tzinfo (0.3.27)
youtube_it (1.4.2)
youtube_it (1.4.3)
builder
oauth (>= 0.4.4)
@@ -103,6 +103,7 @@ DEPENDENCIES
jquery-rails (>= 0.2.6)
json
rails (= 3.0.5)
rake (!= 0.9.0)
ruby_parser
sqlite3
sqlite3-ruby
+1 -1
View File
@@ -7,7 +7,7 @@ class ArtistsController < ApplicationController
@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
@related = LastFM::Artist.getSimilar CGI.unescape(params[:id]), 6
LastFM::LastFMRequest.run_queue!
end
+1 -1
View File
@@ -5,7 +5,7 @@ module ApplicationHelper
@attr = (type=="gallery") ? "name" : "size"
image.each do |img|
if img[@attr] == size.to_s
if img["#text"].empty?
if img["#text"].blank?
return "/images/placeholder/#{type.to_s}.png"
end
return img["#text"]
+1 -2
View File
@@ -28,8 +28,7 @@
%ul.songs.long
- @tracks.track.each do |track|
%li
- id = @youtube_client.videos_by(:query => "#{track.name} #{track.artist.name} official", :page => 1, :per_page => 1, :order_by => "relevance")
= image_tag "ico/play.png", :class=>"play", :videoid=>id.videos.first.unique_id
= image_tag "ico/play.png", :class=>"play"
= image_tag "ico/add.png", :class=>"add"
%span= track.name
= link_to trimString(track.artist.name, 50), :controller=>"artists",:action=>"show",:id=>CGI.escape(track.artist.name)
+5 -1
View File
@@ -2,7 +2,11 @@ module LastFM
class Album < LastFMRequest
def self.search query, limit = nil, page = nil
album_request "search", "albummatches", query, nil, limit, page
album_request "search", "albummatches", query, nil, limit, page do |result|
result = LastFM::Validator.validate_mash result
result = Hashie::Mash.new({ :album => [] }) if result.blank?
update_results "#{current_class_name}_#{__method__}", result
end
end
# return: album name, artist, (top)tags, tracks ( name, duration (in sec) ) mbid (album), release date, image urls ( different sizes )
+17 -6
View File
@@ -1,13 +1,20 @@
module LastFM
class Artist < LastFMRequest
def self.search query, limit = nil, page = nil
artist_request "search", "artistmatches", query, limit, page
artist_request "search", "artistmatches", query, limit, page do |result|
result = LastFM::Validator.validate_mash result
result = Hashie::Mash.new({ :artist => [] }) if result.blank?
update_results "#{current_class_name}_#{__method__}", result
end
end
# return: artist name, tags, similar artists, artist bio (summary / long), mbid, image urls ( different sizes )
def self.getInfo artist
artist_request "getInfo", "artist", artist
artist_request "getInfo", "artist", artist do |info|
LastFM::Validator.validate_mash info, "tag", "image"
end
end
# return: artists ( name, mbid, image urls ( different sizes ) )
@@ -28,14 +35,18 @@ module LastFM
# return: albums ( name, mbid, album url, image urls ( different sizes ) ) ordered by popularity
def self.getTopAlbums artist, limit = nil, page = nil
artist_request "getTopAlbums", "topalbums", artist, limit, page do |albums|
if albums["@attr"].total.to_i == 1
albums.album = [albums.album] # wrap with Array
end
albums = LastFM::Validator.validate_mash albums, "album"
albums = Hashie::Mash.new({ :album => [] }) if albums.total == "0"
update_results "#{current_class_name}_#{__method__}", albums
end
end
def self.getImages artist, limit = nil
artist_request "getImages", "images", artist, limit
artist_request "getImages", "images", artist, limit do |images|
images = LastFM::Validator.validate_mash images, "image"
images = Hashie::Mash.new({ :image => [] }) if images.total == "0"
update_results "#{current_class_name}_#{__method__}", images
end
end
end
+9 -2
View File
@@ -1,11 +1,18 @@
module LastFM
class Chart < LastFMRequest
def self.getTopArtists limit = nil, page = nil
chart_request "getTopArtists", "artists", limit, page
chart_request "getTopArtists", "artists", limit, page do |artists|
LastFM::Validator.validate_mash artists, "image"
update_results "#{current_class_name}_#{__method__}", artists
end
end
def self.getTopTracks limit = nil, page = nil
chart_request "getTopTracks", "tracks", limit, page
chart_request "getTopTracks", "tracks", limit, page do |tracks|
LastFM::Validator.validate_mash tracks
update_results "#{current_class_name}_#{__method__}", tracks
end
end
end
+28 -21
View File
@@ -34,52 +34,59 @@ module LastFM
end
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
request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :artist => q, :limit => limit, :page => page )
create_and_queue_request method, node, block, request_params
end
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
request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :album => q, :artist => artist, :limit => limit, :page => page )
create_and_queue_request method, node, block, request_params
end
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
request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :track => q, :artist => artist, :limit => limit, :page => page )
create_and_queue_request method, node, block, request_params
end
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
request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :limit => limit, :page => page )
create_and_queue_request method, node, block, request_params
end
protected
def self.update_results key, hash
@@results[(key.to_sym)].update hash
end
def self.current_class_name
self.name.split('::').last.downcase!
end
private
def self.create_and_queue_request klass_name, method, node, block, request_params
def self.create_and_queue_request method, node, block, request_params
request = Typhoeus::Request.new(base_uri, :params => request_params)
handle_response request, klass_name, method, node, block
handle_response request, 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
puts ">> ---- '#{current_class_name}.#{method.to_s}' request is queued ----" if @@hydra.queue request
@@results[(current_class_name + "_" + method).to_sym] = Hashie::Mash.new
end
def self.handle_response request, klass_name, method, node, block
def self.handle_response request, method, node, block
request.on_complete do |response|
if response.success?
puts "<< ---- '#{klass_name}.#{method.to_s}' -- TIME: #{response.time.to_s} ----"
puts "<< ---- '#{current_class_name}.#{method.to_s}' -- TIME: #{response.time.to_s} ----"
hash = Hashie::Mash.new(JSON.parse(response.body))
puts hash
hash = method.to_s == "search" ? hash.results.send(node.to_sym) : hash.send(node.to_sym)
@@results[(klass_name + "_" + method).to_sym].update hash
update_results "#{current_class_name}_#{method}", hash unless hash.blank?
block.call(@@results[(klass_name + "_" + method).to_sym]) unless block.nil?
block.call(@@results[("#{current_class_name}_#{method}").to_sym]) unless block.nil?
elsif response.timed_out?
puts ">> -- '#{klass_name}.#{method.to_s}' -- TIMED OUT -- <<"
puts ">> -- '#{current_class_name}.#{method.to_s}' -- TIMED OUT -- <<"
end
end
end
+5 -1
View File
@@ -2,7 +2,11 @@ module LastFM
class Track < LastFMRequest
def self.search query, limit = nil, page = nil
track_request "search", "trackmatches", query, nil, limit, page
track_request "search", "trackmatches", query, nil, limit, page do |result|
result = LastFM::Validator.validate_mash result
result = Hashie::Mash.new({ :track => [] }) if result.blank?
update_results "#{current_class_name}_#{__method__}", result
end
end
# return: track name, id, (top)tags, artist, duration (in msec), album ( artist, title, image urls ( different sizes) ), mbid
+28
View File
@@ -0,0 +1,28 @@
module LastFM
class Validator
def self.validate_mash mash, *args
args.flatten!
return [] if mash.blank?
mash.each do |key, value|
if args.include? key.to_s || value.is_a?(Array)
mash[key] = Array.wrap value
mash[key].map do |mashie|
validate_mash mashie, args
end
elsif value.is_a? Hashie::Mash
mash[key] = validate_mash mash[key], args
end
mash[key] = "" if value.blank? && value.is_a?(String)
mash[key] = [] if value.blank? && value.is_a?(Array)
end
return mash
end
end
end