Class: ElVallenato::Search

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode, term) ⇒ Search

Returns a new instance of Search.



10
11
12
13
14
15
16
# File 'lib/elvallenato/search.rb', line 10

def initialize(mode,term)
  @current_link = 0
  @mode = CGI.escape(mode)
  @term = CGI.escape(term)
  @url = "http://www.elvallenato.com/letras/busqueda.php?x=#{@term}&busca=#{@mode}&image.x=12&image.y=11" 
  @base = "http://" + URI.parse(@url).host
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

#termObject (readonly)

Returns the value of attribute term.



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

def term
  @term
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#clean_url(url) ⇒ Object



52
53
54
55
56
57
# File 'lib/elvallenato/search.rb', line 52

def clean_url(url)
  url =~ /letras\/letras\/(\d+)\/(.+?)$/
  id = $1
  song_name = CGI.escape($2)
  [@base,"letras","letras",id,song_name].join("/")
end

#contentObject



18
19
20
# File 'lib/elvallenato/search.rb', line 18

def content
  @content ||= OpenURI.open_uri(@url).read
end

#letrasObject



59
60
61
62
63
# File 'lib/elvallenato/search.rb', line 59

def letras 
  liric_links.collect do |link|
    Letra.new(OpenURI.open_uri(link).read)
  end
end


40
41
42
43
44
# File 'lib/elvallenato/search.rb', line 40

def links 
  @page_links ||= Nokogiri::HTML.parse(content).css("span.class5 a").collect do |e|
    e["href"]  if e["href"] =~ /pg=\d+$/
  end.compact.sort.uniq
end


46
47
48
49
50
# File 'lib/elvallenato/search.rb', line 46

def liric_links
 @liric_links ||= Nokogiri::HTML.parse(content).css('.class10 a').collect do |e| 
   clean_url(e["href"])
 end.compact.sort.uniq
end

#next_urlObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/elvallenato/search.rb', line 22

def next_url
  return nil if @current_link == pages 
  if @current_link > 0
    link = @all_links[@current_link]["href"]
    @current_link += 1
    return [@base,link].join("/")
  else
    @all_links ||= Nokogiri::HTML.parse(content).css("span.class5 a")
    link = @all_links[0]["href"]
    @current_link += 1
    return [@base,link].join("/")
  end

end

#pagesObject



36
37
38
# File 'lib/elvallenato/search.rb', line 36

def pages
   links.size
end