class Tinysong::Search

Public Class Methods

all(search, options = {}) click to toggle source

Search and return all songs

@search String Track to be found @options Amount of results @return Array<Song> A list of songs

# File lib/tinysong/search.rb, line 26
def self.all(search, options = {})
  limit = options[:limit] || -1
  Search.new(search, limit).perform
end
find(search) click to toggle source

Search and return first result

@search String Track to be found @return Song A song

# File lib/tinysong/search.rb, line 15
def self.find(search)
  Search.new(search, 1).perform.first
end

Public Instance Methods

perform() click to toggle source
# File lib/tinysong/search.rb, line 31
def perform
  res = JSON.parse(RestClient.post("http://tinysong.com/?s=s", {q: [search, 0]}))
  res = Nokogiri::HTML(res["html"]).css("ul.result")
  res.take(limit(res)).map do |result|
    song_id = result.attr("rel").match(/^(\d+)/).to_a.last
    res = JSON.parse(RestClient.post("http://tinysong.com/?s=sh", {q: [song_id, "search", search]}))
    build(Nokogiri::HTML(res["message"]), song_id)
  end
end

Private Instance Methods

build(element, song_id) click to toggle source
# File lib/tinysong/search.rb, line 43
def build(element, song_id)
  artist = element.at_css("h3").text
  title  = element.at_css("h2").text
  href   = element.at_css(".link a").attr("href")
  Song.new(title, artist, href, song_id.to_i)
end
limit(res) click to toggle source
# File lib/tinysong/search.rb, line 50
def limit(res)
  amount == -1 ? res.length : amount
end