class RubyFm::Track
Attributes
artist[R]
duration[R]
image[R]
listeners[R]
mbid[R]
name[R]
rank[R]
streamable[R]
url[R]
Public Class Methods
new(attributes)
click to toggle source
# File lib/ruby_fm/track.rb, line 7 def initialize(attributes) @name = attributes['name'] @duration = attributes['duration'] @mbid = attributes['mbid'] @url = attributes['url'] @streamable = attributes['streamable'] @artist = attributes['artist']['name'].nil? ? attributes['artist'] : attributes['artist']['name'] @rank = attributes['@attr']['rank'] unless attributes['@attr'].nil? @listeners = attributes['listeners'] if attributes['image'] @images = attributes['image'].map do |image| image['#text'] end end end
search(phrase)
click to toggle source
# File lib/ruby_fm/track.rb, line 24 def search(phrase) res = connection.get('/2.0/', { method: 'track.search', track: phrase, api_key: RubyFm.api_key, format: 'json' }) parsed_results = JSON.parse(res.body) if parsed_results['results'] && parsed_results['results']['trackmatches'] track_results = [parsed_results['results']['trackmatches']['track']].flatten track_results.map do |track_attributes| new(track_attributes) end else [] end end
Protected Class Methods
connection()
click to toggle source
# File lib/ruby_fm/track.rb, line 43 def connection conn ||= Faraday.new(:url => 'http://ws.audioscrobbler.com') do |faraday| faraday.request :url_encoded # form-encode POST params faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end end