Class: Klepto::Bot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, &block) ⇒ Bot

Returns a new instance of Bot.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/klepto/bot.rb', line 19

def initialize(url=nil, &block)
  @config = Klepto::Config.new
  @vars   = {}
  @config.url url
  @queue  = []
  @browser = Klepto::Browser.new
  
  # Evaluate the block as DSL, proxy off anything that isn't on #config
  #   to a queue, then apply that queue to the top-level Klepto::Structure
  instance_eval &block

  # After DSL evaluation is queued up, put some methods onto this instance
  # and restore method_missing (for sanity sake)
  instance_eval <<-EOS
def queue; @queue; end;
def browser; @browser; end;
def url=(_url); @config.url(_url); end;
def process!; __process!; end;
def structure; @structure; end;
def method_missing(meth, *args, &block)
  raise NoMethodError.new("undefined method: Klepto::Bot#" + meth.to_s)
end
EOS

  __process!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



127
128
129
# File 'lib/klepto/bot.rb', line 127

def method_missing(meth, *args, &block)
  @queue.push([meth, args, block])
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/klepto/bot.rb', line 3

def config
  @config
end

#varsObject (readonly)

Returns the value of attribute vars.



3
4
5
# File 'lib/klepto/bot.rb', line 3

def vars
  @vars
end

Instance Method Details

#__process!Object

Structure all the pages



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/klepto/bot.rb', line 47

def __process!
  @structure = nil
  @browser.set_driver  @config.driver || :poltergeist
  @browser.set_headers @config.headers
  
  @config.before_handlers[:get].each { |bh| 
    bh.call(@browser,@config.url) 
  }
  
  begin
    @browser.page.driver.restart
    @browser.fetch! @config.url

    # Fire callbacks on GET
    @config.after_handlers[:get].each do |ah|
      ah.call(@browser, @config.url)
    end

    if @browser.was_redirected?
      @config.status_handler(:redirect).each {|sh| sh.call(:redirect, @browser) }

      if @config.abort_on_redirect?
        @config.after_handlers[:abort].each {|ah| ah.call(@browser) }
        return
      end
    end
            
    # Dispatch all the handlers for HTTP Status Codes.
    @browser.statuses.each do |status|
      @config.status_handler(status).each {|sh| sh.call(status, @browser) }
    end
    
    # This is here to debug, having a weird issue with getting a 200 and sometimes
    #   returning @browser.failure? => true
    sleep_counter = 0
    while @browser.failure? && sleep_counter < @config.sleep_tries
      sleep_counter +=1
      sleep @config.sleep_time
    end

    # If the page was not a failure or if not aborting, structure that bad boy.
    if (@browser.failure? && @config.abort_on_failure?) 
      @config.after_handlers[:abort].each {|ah| ah.call(@browser) }
    else
      @structure = __structure(@browser.page)
    end                  
  rescue Capybara::Poltergeist::TimeoutError => ex
    if @config.has_timeout_handler?
      @config.status_handler(:timeout).each{|th| th.call(ex, @browser, @config.url) }
    else
      raise ex
    end
  end

  # @browser.page.driver.client.stop
  # @browser.page.driver.server.stop
  # @browser.page.driver.quit #rescue nil 

  @structure
end

#__structure(context) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/klepto/bot.rb', line 108

def __structure(context)
  structure = Structure.new(context)
  structure._bot = self

  # A queue of DSL instructions
  queue.each do |instruction|
    if instruction[2]
      structure.send instruction[0], *instruction[1], &instruction[2]
    else
      structure.send instruction[0], *instruction[1]
    end
  end

  # Call after(:each) handlers...
  config.after_handlers[:structure].each { |ah| ah.call(structure._hash) }

  structure._hash
end

#get(key) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/klepto/bot.rb', line 9

def get(key)
  val = vars[key]
  
  if val.is_a? Proc
    val = val.call(@browser.page)
    set(key => val)
  end
  val
end

#set(hash) ⇒ Object



5
6
7
# File 'lib/klepto/bot.rb', line 5

def set(hash)
  @vars = vars.merge(hash)
end