Class: Firstfm::Track

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/firstfm/track.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#artistObject

Returns the value of attribute artist.



5
6
7
# File 'lib/firstfm/track.rb', line 5

def artist
  @artist
end

#imagesObject

Returns the value of attribute images.



5
6
7
# File 'lib/firstfm/track.rb', line 5

def images
  @images
end

#listenersObject

Returns the value of attribute listeners.



5
6
7
# File 'lib/firstfm/track.rb', line 5

def listeners
  @listeners
end

#mbidObject

Returns the value of attribute mbid.



5
6
7
# File 'lib/firstfm/track.rb', line 5

def mbid
  @mbid
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/firstfm/track.rb', line 5

def name
  @name
end

#rankObject

Returns the value of attribute rank.



5
6
7
# File 'lib/firstfm/track.rb', line 5

def rank
  @rank
end

#streamableObject

Returns the value of attribute streamable.



5
6
7
# File 'lib/firstfm/track.rb', line 5

def streamable
  @streamable
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/firstfm/track.rb', line 5

def url
  @url
end

Class Method Details

.init_from_array(array) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/firstfm/track.rb', line 21

def self.init_from_array(array)
  return [] unless array.is_a?(Array)
  array.inject([]) do |arr, track|
    arr << init_from_hash(track)
    arr
  end
end

.init_from_hash(hash) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/firstfm/track.rb', line 29

def self.init_from_hash(hash)
  return nil unless hash.is_a?(Hash)
  Track.new.tap do |track|
    track.rank = hash["@attr"]["rank"].to_i if hash["@attr"]
    track.name = hash["name"]
    track.mbid = hash["mbid"].to_s.size > 0 ? hash["mbid"] : nil
    track.url = hash["url"]
    track.listeners = hash["listeners"].to_i
    track.streamable = false
    track.images = hash["image"]
    track.artist = hash["artist"].is_a?(Hash) ? Artist.init_from_hash(hash["artist"]) : Artist.new(:name => hash["artist"])
  end
end

.search(track, page = 1, limit = 30) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/firstfm/track.rb', line 11

def self.search(track, page = 1, limit = 30)
  response = get("/2.0/", {:query => {:method => 'track.search', :track => track, :page => page, :limit => limit, :api_key => Firstfm.config.api_key, :format => :json}})
  tracks_array = (response and response["results"] and response["results"]["trackmatches"] and response["results"]["trackmatches"]["track"]) || []
  tracks = Track.init_from_array(tracks_array)
  WillPaginate::Collection.create(page, limit) do |pager|
    pager.replace tracks
    pager.total_entries = response['results']['opensearch:totalResults'].to_i
  end
end