Class: Firstfm::Artist
- Inherits:
-
Object
- Object
- Firstfm::Artist
- Includes:
- HTTParty
- Defined in:
- lib/firstfm/artist.rb
Instance Attribute Summary collapse
-
#images ⇒ Object
Returns the value of attribute images.
-
#listeners ⇒ Object
Returns the value of attribute listeners.
-
#mbid ⇒ Object
Returns the value of attribute mbid.
-
#name ⇒ Object
Returns the value of attribute name.
-
#playcount ⇒ Object
Returns the value of attribute playcount.
-
#streamable ⇒ Object
Returns the value of attribute streamable.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .get_correction(artist) ⇒ Object
- .get_tags(artist) ⇒ Object
- .get_top_tracks(artist, page = 1, limit = 50) ⇒ Object
- .init_from_array(array) ⇒ Object
- .init_from_hash(hash) ⇒ Object
- .search(artist, page = 1, limit = 50) ⇒ Object
Instance Method Summary collapse
- #get_tags ⇒ Object
-
#initialize(params = {}) ⇒ Artist
constructor
A new instance of Artist.
Constructor Details
#initialize(params = {}) ⇒ Artist
Returns a new instance of Artist.
11 12 13 |
# File 'lib/firstfm/artist.rb', line 11 def initialize(params = {}) @name = params[:name] end |
Instance Attribute Details
#images ⇒ Object
Returns the value of attribute images.
5 6 7 |
# File 'lib/firstfm/artist.rb', line 5 def images @images end |
#listeners ⇒ Object
Returns the value of attribute listeners.
5 6 7 |
# File 'lib/firstfm/artist.rb', line 5 def listeners @listeners end |
#mbid ⇒ Object
Returns the value of attribute mbid.
5 6 7 |
# File 'lib/firstfm/artist.rb', line 5 def mbid @mbid end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/firstfm/artist.rb', line 5 def name @name end |
#playcount ⇒ Object
Returns the value of attribute playcount.
5 6 7 |
# File 'lib/firstfm/artist.rb', line 5 def playcount @playcount end |
#streamable ⇒ Object
Returns the value of attribute streamable.
5 6 7 |
# File 'lib/firstfm/artist.rb', line 5 def streamable @streamable end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/firstfm/artist.rb', line 5 def url @url end |
Class Method Details
.get_correction(artist) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/firstfm/artist.rb', line 46 def self.get_correction(artist) response = get("/2.0/", {:query => {:method => 'artist.getcorrection', :artist => artist, :api_key => Firstfm.config.api_key, :format => :json}}) if response["corrections"] && response["corrections"]["correction"] init_from_hash(response["corrections"]["correction"]["artist"]) rescue nil elsif response.key?("corrections") init_from_hash({"name" => artist}) end end |
.get_tags(artist) ⇒ Object
22 23 24 |
# File 'lib/firstfm/artist.rb', line 22 def self.(artist) self.new(name: artist). end |
.get_top_tracks(artist, page = 1, limit = 50) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/firstfm/artist.rb', line 36 def self.get_top_tracks(artist, page = 1, limit = 50) response = get("/2.0/", {:query => {:method => 'artist.getTopTracks', :artist => artist, :page => page, :limit => limit, :api_key => Firstfm.config.api_key, :format => :json}}) tracks_array = (response and response and response["toptracks"] and response["toptracks"]["track"]) || [] tracks = Track.init_from_array(tracks_array) WillPaginate::Collection.create(page, limit) do |pager| pager.replace tracks pager.total_entries = response["toptracks"]["@attr"]["total"].to_i rescue 0 end end |
.init_from_array(array) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/firstfm/artist.rb', line 55 def self.init_from_array(array) return [] unless array.is_a?(Array) array.inject([]) do |arr, artist| arr << init_from_hash(artist) arr end end |
.init_from_hash(hash) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/firstfm/artist.rb', line 63 def self.init_from_hash(hash) return nil unless hash.is_a?(Hash) Artist.new.tap do |artist| artist.name = hash["name"] artist.mbid = hash["mbid"] artist.url = hash["url"] artist.listeners = hash["listeners"].to_i artist.streamable = hash["streamable"] == "1" artist.images = hash["image"] artist.playcount = hash["playcount"].to_i end end |
.search(artist, page = 1, limit = 50) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/firstfm/artist.rb', line 26 def self.search(artist, page = 1, limit = 50) response = get("/2.0/", {:query => {:method => 'artist.search', :artist => artist, :page => page, :limit => limit, :api_key => Firstfm.config.api_key, :format => :json}}) artists_array = (response and response["results"] and response["results"]["artistmatches"] and response["results"]["artistmatches"]["artist"]) || [] artists = Artist.init_from_array(artists_array) WillPaginate::Collection.create(page, limit) do |pager| pager.replace artists pager.total_entries = response['results']['opensearch:totalResults'].to_i rescue 0 end end |
Instance Method Details
#get_tags ⇒ Object
15 16 17 18 19 20 |
# File 'lib/firstfm/artist.rb', line 15 def name_params = self.mbid.nil? ? {:artist => self.name} : {:mbid => self.mbid} response = self.class.get("/2.0/", {:query => {:method => 'artist.getInfo', :api_key => Firstfm.config.api_key, :format => :json}.merge(name_params)}) = (response and response["artist"] and response["artist"]["tags"] and response["artist"]["tags"]["tag"]) || [] .is_a?(Array) ? .map {|t| t["name"] } : [] end |