Class: Skrape::Page
- Inherits:
-
Object
show all
- Defined in:
- lib/skrape.rb
Instance Method Summary
collapse
Constructor Details
#initialize(url) ⇒ Page
Returns a new instance of Page.
12
13
14
15
16
17
|
# File 'lib/skrape.rb', line 12
def initialize url
@fail_loudly = false
@extracted_info = {}
agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.102 Chrome/32.0.1700.102 Safari/537.36"
@document = Nokogiri::HTML(open(url, "User-Agent" => agent))
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, args) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/skrape.rb', line 29
def method_missing name, args
feature_name = name.to_s.gsub('extract_', '').to_sym
element = @document.css args[:with]
if @fail_loudly
raise NoElementsFoundError, "the css selector for '#{feature_name}' did not return anything" if element.empty?
end
if args[:and_run]
@extracted_info[feature_name] = args[:and_run].call(element)
else
@extracted_info[feature_name] = element.text
end
end
|
Instance Method Details
#error_when_selector_returns_nothing(value) ⇒ Object
25
26
27
|
# File 'lib/skrape.rb', line 25
def error_when_selector_returns_nothing value
@fail_loudly = value
end
|
19
20
21
22
23
|
# File 'lib/skrape.rb', line 19
def
block = Proc.new
instance_eval &block
@extracted_info
end
|