Class: ScraperUtils::MechanizeActions
- Inherits:
-
Object
- Object
- ScraperUtils::MechanizeActions
- Defined in:
- lib/scraper_utils/mechanize_actions.rb
Overview
Class for executing a series of mechanize actions with flexible replacements
Instance Attribute Summary collapse
-
#agent ⇒ Mechanize
readonly
The mechanize agent used for actions.
-
#results ⇒ Array
readonly
The results of each action performed.
Instance Method Summary collapse
-
#initialize(agent, replacements = {}) ⇒ MechanizeActions
constructor
Initialize a new MechanizeActions processor.
-
#process(page, actions) ⇒ Mechanize::Page
Process a sequence of actions on a page.
Constructor Details
#initialize(agent, replacements = {}) ⇒ MechanizeActions
Initialize a new MechanizeActions processor
37 38 39 40 41 |
# File 'lib/scraper_utils/mechanize_actions.rb', line 37 def initialize(agent, replacements = {}) @agent = agent @replacements = replacements || {} @results = [] end |
Instance Attribute Details
#agent ⇒ Mechanize (readonly)
Returns The mechanize agent used for actions.
28 29 30 |
# File 'lib/scraper_utils/mechanize_actions.rb', line 28 def agent @agent end |
#results ⇒ Array (readonly)
Returns The results of each action performed.
31 32 33 |
# File 'lib/scraper_utils/mechanize_actions.rb', line 31 def results @results end |
Instance Method Details
#process(page, actions) ⇒ Mechanize::Page
Process a sequence of actions on a page
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/scraper_utils/mechanize_actions.rb', line 58 def process(page, actions) @results = [] current_page = page actions.each do |action| args = action.dup action_type = args.shift current_page, result = case action_type when :click handle_click(current_page, args) when :block handle_block(current_page, args) else raise ArgumentError, "Unknown action type: #{action_type}" end @results << result end current_page end |