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_images(page = 1, limit = 50) ⇒ Object
- #get_tags ⇒ Object
-
#initialize(params = {}) ⇒ Artist
constructor
A new instance of Artist.
Constructor Details
#initialize(params = {}) ⇒ Artist
17 18 19 |
# File 'lib/firstfm/artist.rb', line 17 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
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/firstfm/artist.rb', line 63 def self.get_correction(artist) response = get("/2.0/", {:query => {:method => 'artist.getcorrection', :artist => artist, :api_key => Firstfm.config.api_key}}) if response && response["lfm"] && response["lfm"] && response["lfm"]["status"] == "ok" if response["lfm"]["corrections"] && response["lfm"]["corrections"]["correction"] init_from_hash(response["lfm"]["corrections"]["correction"]["artist"]) rescue nil elsif response["lfm"].key?("corrections") init_from_hash({"name" => artist}) end end end |
.get_tags(artist) ⇒ Object
28 29 30 |
# File 'lib/firstfm/artist.rb', line 28 def self.(artist) self.new(name: artist). end |
.get_top_tracks(artist, page = 1, limit = 50) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/firstfm/artist.rb', line 53 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}}) tracks_array = (response and response["lfm"] and response["lfm"]["toptracks"] and response["lfm"]["toptracks"]["track"]) || [] tracks = Track.init_from_array(tracks_array) WillPaginate::Collection.create(page, limit) do |pager| pager.replace tracks pager.total_entries = response["lfm"]["toptracks"]["total"].to_i rescue 0 end end |
.init_from_array(array) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/firstfm/artist.rb', line 74 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
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/firstfm/artist.rb', line 82 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
43 44 45 46 47 48 49 50 51 |
# File 'lib/firstfm/artist.rb', line 43 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}}) artists_array = (response and response["lfm"] and response["lfm"]["results"] and response["lfm"]["results"]["artistmatches"] and response["lfm"]["results"]["artistmatches"]["artist"]) || [] artists = Artist.init_from_array(artists_array) WillPaginate::Collection.create(page, limit) do |pager| pager.replace artists pager.total_entries = response['lfm']['results']['opensearch:totalResults'].to_i rescue 0 end end |
Instance Method Details
#get_images(page = 1, limit = 50) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/firstfm/artist.rb', line 32 def get_images(page = 1, limit = 50) name_params = self.mbid.nil? ? {:artist => self.name} : {:mbid => self.mbid} response = self.class.get("/2.0/", {:query => {:method => 'artist.getImages', :page => page, :limit => limit, :api_key => Firstfm.config.api_key}.merge(name_params)}) images_array = (response["lfm"] and response["lfm"]["images"] and response["lfm"]["images"]["image"]) || [] images = Image.init_from_array(images_array) WillPaginate::Collection.create(page, limit) do |pager| pager.replace images pager.total_entries = response["lfm"]["images"]["total"].to_i rescue 0 end end |
#get_tags ⇒ Object
21 22 23 24 25 26 |
# File 'lib/firstfm/artist.rb', line 21 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}.merge(name_params)}) = (response["lfm"] and response["lfm"]["artist"] and response["lfm"]["artist"]["tags"] and response["lfm"]["artist"]["tags"]["tag"]) || [] .is_a?(Array) ? .map {|t| t["name"] } : [] end |