tried to autoload modules (does not yet work as expected)

This commit is contained in:
Thomas Buchöster
2011-05-04 15:53:59 +02:00
parent 61271d7765
commit f0a85a3527
9 changed files with 145 additions and 130 deletions
+1
View File
@@ -1,4 +1,5 @@
class SearchController < ApplicationController
def index
if params[:q].nil?
params[:q] = "foobar"
+2 -2
View File
@@ -13,8 +13,8 @@ module Nineminutes
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/lib/LastFM)
# config.autoload_paths += Dir["#{config.root}/lib/LastFM"]
# config.autoload_paths += %W(#{config.root}/lib)
# config.autoload_paths += Dir["#{config.root}/lib/LastFM/"]
# config.autoload_paths << config.root.join('lib')
# Only load the plugins named here, in the order given (default is alphabetical).
+6 -1
View File
@@ -1,6 +1,11 @@
Nineminutes::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# 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
# since you don't have to restart the webserver when you make code changes.
+5 -3
View File
@@ -1,4 +1,6 @@
require "#{Rails.root.to_s}/lib/LastFM/LastFMRequest.rb"
require "#{Rails.root.to_s}/lib/LastFM/LastFM.rb"
require 'LastFM/LastFMRequest'
require 'LastFM/Artist'
require 'LastFM/Album'
require 'LastFM/Track'
LastFMRequest::api_key = '4c32e360f68553ec8fdca3711456b4f9'
LastFM::LastFMRequest::api_key = '4c32e360f68553ec8fdca3711456b4f9'
+20
View File
@@ -0,0 +1,20 @@
module LastFM
class Album < LastFMRequest
def self.search query, limit = nil, page = nil
album_request "search", "albummatches", query, nil, limit, page
end
# return: album name, artist, (top)tags, tracks ( name, duration (in sec) ) mbid (album), release date, image urls ( different sizes )
def self.getInfo album, artist
album_request "getInfo", nil, album, artist
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags album, artist
album_request "getTopTags", "toptags", album, artist
end
end
end
+35
View File
@@ -0,0 +1,35 @@
module LastFM
class Artist < LastFMRequest
def self.search query, limit = nil, page = nil
artist_request "search", "artistmatches", query, limit, page
end
# return: artist name, tags, similar artists, artist bio (summary / long), mbid, image urls ( different sizes )
def self.getInfo artist
artist_request "getInfo", nil, artist
end
# return: artists ( name, mbid, image urls ( different sizes ) )
def self.getSimilar artist, limit = nil
artist_request "getSimilar", "similarartists", artist, limit
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags artist
artist_request "getTopTags", "toptags", artist
end
# return: tracks ( name, image urls ( different sizes ) ) ordered by popularity
def self.getTopTracks artist, limit = nil, page = nil
artist_request "getTopTracks", "toptracks", artist, limit, page
end
# 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
end
end
end
-75
View File
@@ -1,75 +0,0 @@
module LastFM
class Artist < LastFMRequest
def self.search query, limit = nil, page = nil
artist_request "search", "artistmatches", query, limit, page
end
# return: artist name, tags, similar artists, artist bio (summary / long), mbid, image urls ( different sizes )
def self.getInfo artist
artist_request "getInfo", nil, artist
end
# return: artists ( name, mbid, image urls ( different sizes ) )
def self.getSimilar artist, limit = nil
artist_request "getSimilar", "similarartists", artist, limit
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags artist
artist_request "getTopTags", "toptags", artist
end
# return: tracks ( name, image urls ( different sizes ) ) ordered by popularity
def self.getTopTracks artist, limit = nil, page = nil
artist_request "getTopTracks", "toptracks", artist, limit, page
end
# 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
end
end
class Album < LastFMRequest
def self.search query, limit = nil, page = nil
album_request "search", "albummatches", query, nil, limit, page
end
# return: album name, artist, (top)tags, tracks ( name, duration (in sec) ) mbid (album), release date, image urls ( different sizes )
def self.getInfo album, artist
album_request "getInfo", nil, album, artist
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags album, artist
album_request "getTopTags", "toptags", album, artist
end
end
class Track < LastFMRequest
def self.search query, limit = nil, page = nil
track_request "search", "trackmatches", query, nil, limit, page
end
# return: track name, id, (top)tags, artist, duration (in msec), album ( artist, title, image urls ( different sizes) ), mbid
def self.getInfo track, artist
track_request "getInfo", nil, track, artist
end
# return: tracks ( name, artist (name, mbid), duration (in msec), image urls (sometimes) )
def self.getSimilar track, artist, limit = nil
track_request "getSimilar", "similartracks", track, artist, limit
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags track, artist
track_request "getTopTags", "toptags", track, artist
end
end
end
+52 -49
View File
@@ -2,56 +2,59 @@ require 'httparty'
require 'active_support'
require 'hashie'
class LastFMRequest
include HTTParty
base_uri "ws.audioscrobbler.com/2.0/"
parser Proc.new { |data| Hashie::Mash.new(ActiveSupport::JSON.decode(data)) }
default_params :format => 'json', :autocorrect => '1'
debug_output $>
format :json
module LastFM
def self.api_key=(key)
@@api_key = key
default_params :api_key => key
end
class LastFMRequest
include HTTParty
def self.limit=(limit)
@limit = limit
end
def self.limit
@limit
end
def self.page=(page)
@page = page
end
def self.page
@page
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 })
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 })
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 })
end
private
def self.prepare_result method, node, response_hash
if method.to_s == "search"
response_hash = response_hash.results
base_uri "ws.audioscrobbler.com/2.0/"
parser Proc.new { |data| Hashie::Mash.new(ActiveSupport::JSON.decode(data)) }
default_params :format => 'json', :autocorrect => '1'
debug_output $>
format :json
def self.api_key=(key)
@@api_key = key
default_params :api_key => key
end
response_hash.send(node.to_sym)
def self.limit=(limit)
@limit = limit
end
def self.limit
@limit
end
def self.page=(page)
@page = page
end
def self.page
@page
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 })
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 })
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 })
end
private
def self.prepare_result method, node, response_hash
if method.to_s == "search"
response_hash = response_hash.results
end
response_hash.send(node.to_sym)
end
end
end
end
+24
View File
@@ -0,0 +1,24 @@
module LastFM
class Track < LastFMRequest
def self.search query, limit = nil, page = nil
track_request "search", "trackmatches", query, nil, limit, page
end
# return: track name, id, (top)tags, artist, duration (in msec), album ( artist, title, image urls ( different sizes) ), mbid
def self.getInfo track, artist
track_request "getInfo", nil, track, artist
end
# return: tracks ( name, artist (name, mbid), duration (in msec), image urls (sometimes) )
def self.getSimilar track, artist, limit = nil
track_request "getSimilar", "similartracks", track, artist, limit
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags track, artist
track_request "getTopTags", "toptags", track, artist
end
end
end