diff --git a/9min.zip b/9min.zip new file mode 100644 index 0000000..592c820 Binary files /dev/null and b/9min.zip differ diff --git a/Gemfile b/Gemfile index f706288..38d6027 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,10 @@ gem 'rake' #, '0.8.7' gem 'rails', '3.0.5' gem 'jquery-rails', '>= 0.2.6' -# templating / styling +gem 'sqlite3' +gem 'sqlite3-ruby', :require => 'sqlite3' + +# templating gem 'haml-rails' gem "sass" @@ -13,7 +16,9 @@ gem "youtube_it" # for using last.fm api gem 'typhoeus' -# gem 'httparty' + +# for object caching +gem 'redis' # for using 7digital api gem '7digital' @@ -22,7 +27,7 @@ gem '7digital' gem 'json' gem 'hashie' -# authentication, authorization, roles +# authentication gem 'devise' gem 'oa-oauth', :require => 'omniauth/oauth' gem 'oa-openid', :require => 'omniauth/openid' @@ -40,22 +45,8 @@ group :development, :test do # alternative servers (because openID URIs are too long for WEBRick) gem 'mongrel', '1.2.0.pre2' - # gem 'thin' - + end # Deploy with Capistrano # gem 'capistrano' - -# Bundle the extra gems: -# gem 'bj' -gem 'sqlite3' -gem 'sqlite3-ruby', :require => 'sqlite3' -# gem 'aws-s3', :require => 'aws/s3' - -# Bundle gems for the local environment. Make sure to -# put test-only gems in this group so their generators -# and rake tasks are available in development mode: -# group :development, :test do -# gem 'webrat' -# end diff --git a/Gemfile.lock b/Gemfile.lock index 8291c49..2785e89 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -113,6 +113,7 @@ GEM rake (>= 0.8.7) thor (~> 0.14.4) rake (0.9.2) + redis (2.2.0) ruby-openid (2.1.8) ruby-openid-apps-discovery (1.2.0) ruby-openid (>= 2.1.7) @@ -153,6 +154,7 @@ DEPENDENCIES paperclip (~> 2.3) rails (= 3.0.5) rake + redis ruby_parser sass sqlite3 diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index 6d29e5e..06c0288 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -12,7 +12,7 @@ class ArtistsController < ApplicationController @albums = LastFM::Artist.getTopAlbums artist, @@limit[:albums] @related= LastFM::Artist.getSimilar artist, @@limit[:similar] - LastFM::LastFMRequest.run_queue! + LastFM::Request.run_queue! render :partial=>"show", :locals=>{ :artist=>@artist, @@ -28,17 +28,17 @@ class ArtistsController < ApplicationController def more_tracks - prepare params + more params @tracks = LastFM::Artist.getTopTracks params[:q], @@limit[:tracks], (params[:size] / @@limit[:tracks] + 1) - LastFM::LastFMRequest.run_queue! + LastFM::Request.run_queue! render :partial=>"more_tracks", :locals=>{:tracks=>@tracks} end def more_albums - prepare params + more params @albums = LastFM::Artist.getTopAlbums params[:q], @@limit[:albums], (params[:size] / @@limit[:albums] + 1) - LastFM::LastFMRequest.run_queue! + LastFM::Request.run_queue! render :partial=>"more_albums", :locals=>{:albums=>@albums} end @@ -54,4 +54,4 @@ class ArtistsController < ApplicationController params[:q] = CGI.unescape params[:q] params[:size] = params[:size].to_i end -end \ No newline at end of file +end diff --git a/app/controllers/charts_controller.rb b/app/controllers/charts_controller.rb index 3f3e8f9..69b9654 100644 --- a/app/controllers/charts_controller.rb +++ b/app/controllers/charts_controller.rb @@ -1,7 +1,7 @@ class ChartsController < ApplicationController def index @charts = LastFM::Chart.getTopArtists 20 - LastFM::LastFMRequest.run_queue! + LastFM::Request.run_queue! render :partial=>"index" end @@ -11,7 +11,7 @@ class ChartsController < ApplicationController params[:size] = params[:size].to_i @charts = LastFM::Chart.getTopArtists 20, (params[:size] / 20 + 1) - LastFM::LastFMRequest.run_queue! + LastFM::Request.run_queue! render :partial=>"more_charts", :locals=>{:charts=>@charts} end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 0499755..92bab50 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,7 +1,8 @@ class HomeController < ApplicationController + def show @charts = LastFM::Chart.getTopArtists 10 - LastFM::LastFMRequest.run_queue! + LastFM::Request.run_queue! render :partial=>"index" end diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 817c9a8..c02304e 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -8,7 +8,7 @@ class SearchController < ApplicationController @artists = LastFM::Artist.search @q, @@limit[:artists] @tracks = LastFM::Track.search @q, @@limit[:tracks] @albums = LastFM::Album.search @q, @@limit[:albums] - + LastFM::LastFMRequest.run_queue! render :partial=>"show", :locals=>{ diff --git a/config/environments/development.rb b/config/environments/development.rb index 5f31963..8bdcc0f 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -3,8 +3,6 @@ Nineminutes::Application.configure do # Reloads modules and files in lib directory config.autoload_paths += %W(#{config.root}/lib) - config.autoload_paths += Dir["#{config.root}/lib/**/"] - # In the development environment your application's code is reloaded on # every request. This slows down response time but is perfect for development diff --git a/config/initializers/lastfm.rb b/config/initializers/lastfm.rb new file mode 100644 index 0000000..1601a07 --- /dev/null +++ b/config/initializers/lastfm.rb @@ -0,0 +1,3 @@ +ActionDispatch::Callbacks.to_prepare do + LastFM::Request::api_key = APP_CONFIG['lastfm_api_key'] +end \ No newline at end of file diff --git a/config/initializers/lastfm_api_key.rb b/config/initializers/lastfm_api_key.rb deleted file mode 100644 index 3330d1f..0000000 --- a/config/initializers/lastfm_api_key.rb +++ /dev/null @@ -1,3 +0,0 @@ -ActionDispatch::Callbacks.to_prepare do - LastFM::LastFMRequest::api_key = APP_CONFIG['lastfm_api_key'] -end \ No newline at end of file diff --git a/lib/last_fm/album.rb b/lib/last_fm/album.rb index 85eb966..46eb5ca 100644 --- a/lib/last_fm/album.rb +++ b/lib/last_fm/album.rb @@ -1,11 +1,11 @@ module LastFM - class Album < LastFMRequest + class Album < Request def self.search query, limit = nil, page = nil 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 + update_results [current_class_name, __method__], result end end diff --git a/lib/last_fm/artist.rb b/lib/last_fm/artist.rb index 4cb9b62..a742ef6 100644 --- a/lib/last_fm/artist.rb +++ b/lib/last_fm/artist.rb @@ -1,19 +1,20 @@ module LastFM - class Artist < LastFMRequest + class Artist < Request def self.search query, limit = nil, page = nil 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 + 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 do |info| - LastFM::Validator.validate_mash info, "tag", "image" + info = LastFM::Validator.validate_mash info, "tag", "image" + update_results [current_class_name, __method__], info end end @@ -37,7 +38,7 @@ module LastFM artist_request "getTopAlbums", "topalbums", artist, limit, page do |albums| albums = LastFM::Validator.validate_mash albums, "album" albums = Hashie::Mash.new({ :album => [] }) if albums.total == "0" - update_results "#{current_class_name}_#{__method__}", albums + update_results [current_class_name, __method__], albums end end @@ -45,7 +46,7 @@ module LastFM 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 + update_results [current_class_name, __method__], images end end diff --git a/lib/last_fm/chart.rb b/lib/last_fm/chart.rb index 9476c57..d4b8b01 100644 --- a/lib/last_fm/chart.rb +++ b/lib/last_fm/chart.rb @@ -1,17 +1,17 @@ module LastFM - class Chart < LastFMRequest + class Chart < Request def self.getTopArtists limit = nil, page = nil chart_request "getTopArtists", "artists", limit, page do |artists| LastFM::Validator.validate_mash artists, "image" - update_results "#{current_class_name}_#{__method__}", artists + update_results [current_class_name, __method__], artists end end def self.getTopTracks limit = nil, page = nil chart_request "getTopTracks", "tracks", limit, page do |tracks| LastFM::Validator.validate_mash tracks - update_results "#{current_class_name}_#{__method__}", tracks + update_results [current_class_name, __method__], tracks end end end diff --git a/lib/last_fm/last_fm_request.rb b/lib/last_fm/request.rb similarity index 67% rename from lib/last_fm/last_fm_request.rb rename to lib/last_fm/request.rb index e5023f2..f50d635 100644 --- a/lib/last_fm/last_fm_request.rb +++ b/lib/last_fm/request.rb @@ -3,7 +3,7 @@ require 'hashie' module LastFM - class LastFMRequest + class Request @@base_uri = 'http://ws.audioscrobbler.com/2.0/' @@default_params = { :format => 'json', :autocorrect => '1' } @@ -16,15 +16,16 @@ module LastFM end def self.base_uri - return @@base_uri + @@base_uri end def self.default_params - return @@default_params + @@default_params end - def self.results - return @@results + def self.results keys = nil + return @@results if keys.nil? + @@results[("#{keys[0]}_#{keys[1]}".to_sym)] end def self.run_queue! @@ -34,29 +35,30 @@ module LastFM end def self.artist_request method, node, q, limit = nil, page = nil, &block - request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :artist => q, :limit => limit, :page => page ) + request_params = default_params.merge( :method => "#{current_class_name}.#{method}", :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 - request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :album => q, :artist => artist, :limit => limit, :page => page ) + request_params = default_params.merge( :method => "#{current_class_name}.#{method}", :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 - request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :track => q, :artist => artist, :limit => limit, :page => page ) + request_params = default_params.merge( :method => "#{current_class_name}.#{method}", :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 - request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :limit => limit, :page => page ) + request_params = default_params.merge( :method => "#{current_class_name}.#{method}", :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 + def self.update_results keys, hash + puts "updated" + @@results[("#{keys[0]}_#{keys[1]}".to_sym)].update hash end def self.current_class_name @@ -69,23 +71,23 @@ module LastFM request = Typhoeus::Request.new(base_uri, :params => request_params) # :timeout => APP_CONFIG["request_timeout"], handle_response request, method, node, block - puts ">> ---- '#{current_class_name}.#{method.to_s}' request is queued ----" if @@hydra.queue request - @@results[(current_class_name + "_" + method).to_sym] = Hashie::Mash.new + puts ">> ---- '#{current_class_name}.#{method}' request is queued ----" if @@hydra.queue request + @@results["#{current_class_name}_#{method}".to_sym] = Hashie::Mash.new end def self.handle_response request, method, node, block request.on_complete do |response| if response.success? - puts "<< ---- '#{current_class_name}.#{method.to_s}' -- TIME: #{response.time.to_s} ----" + puts "<< ---- '#{current_class_name}.#{method}' -- TIME: #{response.time} ----" hash = Hashie::Mash.new(JSON.parse(response.body)) hash = method.to_s == "search" ? hash.results.send(node.to_sym) : hash.send(node.to_sym) - update_results "#{current_class_name}_#{method}", hash unless hash.blank? + update_results [current_class_name, method], hash if hash.present? and block.nil? + block.call( hash ) unless block.nil? - block.call(@@results[("#{current_class_name}_#{method}").to_sym]) unless block.nil? elsif response.timed_out? - puts ">> -- '#{current_class_name}.#{method.to_s}' -- TIMED OUT -- <<" + puts ">> -- '#{current_class_name}.#{method}' -- TIMED OUT -- <<" end end end diff --git a/lib/last_fm/track.rb b/lib/last_fm/track.rb index 4b596e9..324ea1e 100644 --- a/lib/last_fm/track.rb +++ b/lib/last_fm/track.rb @@ -1,11 +1,11 @@ module LastFM - class Track < LastFMRequest + class Track < Request def self.search query, limit = nil, page = nil 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 + update_results [current_class_name, __method__], result end end