Class: Tinysong::Search

Inherits:
Struct
  • Object
show all
Defined in:
lib/tinysong/search.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#amountObject

Returns the value of attribute amount

Returns:

  • (Object)

    the current value of amount



8
9
10
# File 'lib/tinysong/search.rb', line 8

def amount
  @amount
end

#searchObject

Returns the value of attribute search

Returns:

  • (Object)

    the current value of search



8
9
10
# File 'lib/tinysong/search.rb', line 8

def search
  @search
end

Class Method Details

.all(search, options = {}) ⇒ Object

Search and return all songs

@options Amount of results



26
27
28
29
# File 'lib/tinysong/search.rb', line 26

def self.all(search, options = {})
  limit = options[:limit] || -1
  Search.new(search, limit).perform
end

.find(search) ⇒ Object

Search and return first result

Returns:

  • Song A song



15
16
17
# File 'lib/tinysong/search.rb', line 15

def self.find(search)
  Search.new(search, 1).perform.first
end

Instance Method Details

#performObject



31
32
33
34
35
36
37
38
39
# 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