Class: Scrapie

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

Defined Under Namespace

Classes: NoAttributesException, ScrapieException

Class Method Summary collapse

Class Method Details

.after_fetchObject



51
52
53
# File 'lib/scrapie.rb', line 51

def self.after_fetch
  
end

.attributes(attributes) ⇒ Object



16
17
18
19
20
21
# File 'lib/scrapie.rb', line 16

def self.attributes(attributes)
  @attributes = attributes
  attributes.each {|name,page_selector|
    self.send(:attr_accessor, name)
  }
end

.before_fetchObject

Callbacks # TODO



47
48
49
# File 'lib/scrapie.rb', line 47

def self.before_fetch
  
end

.find(opts = {}) ⇒ Object

find() find(:foo => bar) find(:foo => bar, :baz => bizzle)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/scrapie.rb', line 26

def self.find(opts = {})
  raise NoAttributesException unless (@attributes and @attributes.size > 0)
  a = Mechanize.new
  
  # Let's build out the parameters now
  params = Hash[opts.collect{|k,v|
    [@params[k], v] if @params and @params[k]
  }]
  
  page = a.send(@http_method || :get, @url, params)
  
  new_object = self.new
  @attributes.each {|name, page_selector|
    new_object.send(name + '=', page.search(page_selector).inner_html)
  }
  
  new_object
end

.http_method(method) ⇒ Object



13
14
15
# File 'lib/scrapie.rb', line 13

def self.http_method(method)
  @http_method = method
end

.params(params) ⇒ Object



10
11
12
# File 'lib/scrapie.rb', line 10

def self.params(params)
  @params = params
end

.url(url) ⇒ Object



7
8
9
# File 'lib/scrapie.rb', line 7

def self.url(url)
  @url = url
end