Skip to content

Commit

Permalink
Remove redundant aliases; rename asset helper
Browse files Browse the repository at this point in the history
  • Loading branch information
k-stewart committed May 21, 2018
1 parent a6b7e57 commit 84204e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module FormFiller::AssetsHelper
module FormFillHelper
def save_screenshot_and_store_poltergeist
screenshot_location = random_screenshot_location
@session.save_screenshot(screenshot_location, full: true)
Expand Down
28 changes: 11 additions & 17 deletions lib/form_filler/capybara.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class FormFiller::Capybara
include FormFiller::AssetsHelper
include FormFillHelper

CAPTCHA_SOLUTION = "$CAPTCHA_SOLUTION"

delegate :bioguide_id, to: :rep
attr_accessor :rep
attr_accessor :fields

def initialize(rep, fields, session: nil)
@fields = fields
Expand All @@ -18,19 +20,19 @@ def fill_out(starting_action = nil, &block)
form_fill_log("begin")

begin
actions = @rep.actions.order(:step)
actions = rep.actions.order(:step)

if starting_action
actions = actions.drop_while{ |a| a.id != starting_action.id }
end

actions.each do |a|
action = FormFiller::CapybaraAction.new(a, @session, @fields)
action = FormFiller::CapybaraAction.new(a, @session, fields)
log_action(action)

if action.value == CAPTCHA_SOLUTION
yield(url_for(action), @session, @rep) unless @fields[CAPTCHA_SOLUTION]
@session.find(action.selector).set(@fields[CAPTCHA_SOLUTION])
yield(url_for(action), @session, rep) unless fields[CAPTCHA_SOLUTION]
@session.find(action.selector).set(fields[CAPTCHA_SOLUTION])
else
action.execute(&block)
end
Expand All @@ -50,22 +52,14 @@ def fill_out(starting_action = nil, &block)
message[:screenshot] = save_screenshot_and_store_poltergeist
raise e
ensure
@session.driver.quit unless @rep.persist_session?
@session.driver.quit unless rep.persist_session?
end
end

def rep
@rep
end

def fields
@fields
end

private

def url_for(action)
location = CAPTCHA_LOCATIONS.fetch(@rep.bioguide_id, nil)
location = CAPTCHA_LOCATIONS.fetch(rep.bioguide_id, nil)
location ||= @session.driver.evaluate_script(
'document.querySelector("' + action.captcha_selector.gsub('"', '\"') + '").getBoundingClientRect();'
)
Expand All @@ -80,14 +74,14 @@ def log_action(a)
end

def check_success body_text
criteria = YAML.load(@rep.success_criteria)
criteria = YAML.load(rep.success_criteria)
# TODO: check headers
body = criteria.fetch("body", nil)
body && body_text.include?(body["contains"])
end

def form_fill_log(message)
log_message = "#{@rep.bioguide_id} fill (#{[@rep.bioguide_id, @fields].hash.to_s(16)}): #{message}" << " #{message.try(:message)}"
log_message = "#{rep.bioguide_id} fill (#{[rep.bioguide_id, fields].hash.to_s(16)}): #{message}" << " #{message.try(:message)}"
Padrino.logger.info(log_message)

Raven.extra_context(fill_log: "") unless Raven.context.extra.key?(:fill_log)
Expand Down

0 comments on commit 84204e9

Please sign in to comment.