Class: Lecture7::Search

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

Direct Known Subclasses

Phim123

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Search

Returns a new instance of Search.



6
7
8
9
# File 'lib/lecture7/search.rb', line 6

def initialize(options = {})
  @url = options[:url]
  @pattern = options[:pattern]
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



4
5
6
# File 'lib/lecture7/search.rb', line 4

def pattern
  @pattern
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/lecture7/search.rb', line 4

def url
  @url
end

Instance Method Details

#moviesObject



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

def movies
  if url
    search_on_url(url)
  else
    []
  end
end

#search_on_url(url) ⇒ Object



19
20
21
22
23
# File 'lib/lecture7/search.rb', line 19

def search_on_url(url)
  agent = Mechanize.new
  page = agent.get(url)
  page.links_with(pattern).map(&:text)
end