Class: Klepto::Bot
- Inherits:
-
Object
- Object
- Klepto::Bot
- Defined in:
- lib/klepto/bot.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#__process! ⇒ Object
Structure all the pages.
- #__structure(context) ⇒ Object
-
#initialize(url = nil, &block) ⇒ Bot
constructor
A new instance of Bot.
- #method_missing(meth, *args, &block) ⇒ Object
Constructor Details
#initialize(url = nil, &block) ⇒ Bot
Returns a new instance of Bot.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/klepto/bot.rb', line 5 def initialize(url=nil, &block) @config = Klepto::Config.new @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
108 109 110 |
# File 'lib/klepto/bot.rb', line 108 def method_missing(meth, *args, &block) @queue.push([meth, args, block]) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/klepto/bot.rb', line 3 def config @config end |
Instance Method Details
#__process! ⇒ Object
Structure all the pages
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 |
# File 'lib/klepto/bot.rb', line 32 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.fetch! @config.url #@browser.page.driver.restart #@browser.page.driver.quit #rescue nil # 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 @structure end |
#__structure(context) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/klepto/bot.rb', line 90 def __structure(context) structure = Structure.new(context) # 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 |