Module: LastFM

Extended by:
LastFM
Included in:
LastFM
Defined in:
lib/lastfm-client.rb,
lib/lastfm-client/version.rb,
lib/lastfm-client/api_class.rb,
lib/lastfm-client/api_classes/geo.rb,
lib/lastfm-client/api_classes/tag.rb,
lib/lastfm-client/api_classes/auth.rb,
lib/lastfm-client/api_classes/user.rb,
lib/lastfm-client/api_classes/album.rb,
lib/lastfm-client/api_classes/chart.rb,
lib/lastfm-client/api_classes/event.rb,
lib/lastfm-client/api_classes/group.rb,
lib/lastfm-client/api_classes/radio.rb,
lib/lastfm-client/api_classes/track.rb,
lib/lastfm-client/api_classes/venue.rb,
lib/lastfm-client/api_classes/artist.rb,
lib/lastfm-client/api_classes/library.rb,
lib/lastfm-client/api_classes/playlist.rb,
lib/lastfm-client/api_classes/tasteometer.rb

Defined Under Namespace

Classes: APIClass, APIException, Album, Artist, Auth, Chart, Event, Geo, Group, InvalidData, Library, Playlist, Radio, Tag, Tasteometer, Track, User, Venue

Constant Summary collapse

DEFAULT_API_URL =
'http://ws.audioscrobbler.com/2.0/'
DEFAULT_AUTH_URL =
'http://www.last.fm/api/auth/'
VERSION =
"0.0.3"

Instance Method Summary collapse

Instance Method Details

#api_keyObject



42
43
44
# File 'lib/lastfm-client.rb', line 42

def api_key
  @api_key or raise "API Key is not set"
end

#api_key=(key) ⇒ Object



46
47
48
# File 'lib/lastfm-client.rb', line 46

def api_key=(key)
  @api_key = key
end

#api_urlObject



34
35
36
# File 'lib/lastfm-client.rb', line 34

def api_url
  @api_url || DEFAULT_API_URL
end

#api_url=(url) ⇒ Object



38
39
40
# File 'lib/lastfm-client.rb', line 38

def api_url=(url)
  @api_url = url
end

#auth_urlObject



66
67
68
# File 'lib/lastfm-client.rb', line 66

def auth_url
  (@auth_url || DEFAULT_AUTH_URL) + "?api_key=#{self.api_key}"
end

#auth_url=(url) ⇒ Object



70
71
72
# File 'lib/lastfm-client.rb', line 70

def auth_url=(url)
  @auth_url = url
end

#client_nameObject



58
59
60
# File 'lib/lastfm-client.rb', line 58

def client_name
  @client_name or raise "Client name is not set"
end

#client_name=(name) ⇒ Object



62
63
64
# File 'lib/lastfm-client.rb', line 62

def client_name=(name)
  @client_name = name
end

#fetch_data(url) ⇒ Object



94
95
96
97
98
# File 'lib/lastfm-client.rb', line 94

def fetch_data(url)
  open(url, "User-Agent" => client_name) do |page|
    ::JSON.parse(page.read)
  end
end

#generate_signature(params) ⇒ Object



120
121
122
123
124
125
# File 'lib/lastfm-client.rb', line 120

def generate_signature(params)
  params    = params.sort_by { |k,v| k.to_s }
  signature = params.map { |param| "#{param[0].to_s}#{param[1].to_s}" }.join('')
  
  Digest::MD5.hexdigest(signature + self.secret)
end

#hash_to_params(hash) ⇒ Object



127
128
129
# File 'lib/lastfm-client.rb', line 127

def hash_to_params(hash)
  hash.map { |key, value| "#{key.to_s}=#{::CGI.escape(value.to_s)}" }.join('&')
end

#post_data(url, params) ⇒ Object

FIXME: returns either 403 or bad signature errors



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/lastfm-client.rb', line 103

def post_data(url, params)
  url = URI.parse(url)
  req = Net::HTTP::Post.new(url.path)
  req.set_form_data(params)

  res = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) }

  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    return ::JSON.parse(res.body)
  when Net::HTTPClientError
    raise LastFM::InvalidData, res.body if res.code == "422" # unprocessable entity
  end

  raise LastFM::APIException, res.error!
end

#secretObject



50
51
52
# File 'lib/lastfm-client.rb', line 50

def secret
  @secret or raise "Secret is not set"
end

#secret=(secret) ⇒ Object



54
55
56
# File 'lib/lastfm-client.rb', line 54

def secret=(secret)
  @secret = secret
end

#send_api_request(method, params, request_method = :get) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lastfm-client.rb', line 74

def send_api_request(method, params, request_method = :get)
  raise "Invalid params" unless params.is_a?(Hash)

  params[:method]  = method
  params[:api_key] = self.api_key

  if params[:api_sig] == true
    params.delete(:api_sig)
    params[:api_sig] = generate_signature(params)
  end

  params[:format] = "json"

  if request_method == :post
    post_data(self.api_url, params)
  else
    fetch_data(self.api_url + "?" + hash_to_params(params))
  end
end