Class: CongressForms::WebForm

Inherits:
Form
  • Object
show all
Defined in:
lib/congress_forms/web_form.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Form

find, #missing_required_params, repo

Constructor Details

#initialize(actions = [], bioguide: nil, success_status: nil, success_content: nil, updated_at: nil) ⇒ WebForm

Returns a new instance of WebForm.



38
39
40
41
42
43
44
45
46
47
# File 'lib/congress_forms/web_form.rb', line 38

def initialize(actions = [], bioguide: nil,
               success_status: nil,
               success_content: nil,
               updated_at: nil)
  self.bioguide = bioguide
  self.actions = actions
  self.success_status = success_status
  self.success_content = success_content
  self.updated_at = updated_at
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



3
4
5
# File 'lib/congress_forms/web_form.rb', line 3

def actions
  @actions
end

#bioguideObject

Returns the value of attribute bioguide.



3
4
5
# File 'lib/congress_forms/web_form.rb', line 3

def bioguide
  @bioguide
end

#success_contentObject

Returns the value of attribute success_content.



4
5
6
# File 'lib/congress_forms/web_form.rb', line 4

def success_content
  @success_content
end

#success_statusObject

Returns the value of attribute success_status.



4
5
6
# File 'lib/congress_forms/web_form.rb', line 4

def success_status
  @success_status
end

#updated_atObject

Returns the value of attribute updated_at.



5
6
7
# File 'lib/congress_forms/web_form.rb', line 5

def updated_at
  @updated_at
end

Class Method Details

.create_browserObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/congress_forms/web_form.rb', line 26

def self.create_browser
  if ENV["HEADLESS"] == "0"
    Capybara::Session.new(:chrome)
  elsif ENV["WEBDRIVER_HOST"] && !ENV["WEBDRIVER_HOST"].empty?
    Capybara::Session.new(:remote)
  else
    Capybara::Session.new(:headless_chrome)
  end.tap do |browser|
    browser.current_window.resize_to(1920, 1080)
  end
end

.parse(spec, attrs = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/congress_forms/web_form.rb', line 7

def self.parse(spec, attrs={})
  yaml = YAML.load(spec)

  actions = yaml.dig("contact_form", "steps").map do |step|
    Actions.build(step)
  end.flatten

  new(
    actions,
    attrs.merge(
      bioguide: yaml["bioguide"],
      success_status:
        yaml.dig("contact_form", "success", "headers", "status"),
      success_content:
        yaml.dig("contact_form", "success", "body", "contains"),
    )
  )
end

Instance Method Details

#fill(values, browser: self.class.create_browser, validate_only: false) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/congress_forms/web_form.rb', line 64

def fill(values, browser: self.class.create_browser, validate_only: false)
  log("#{bioguide} fill")

  actions.each do |action|
    break if action.submit? && validate_only

    log(action.inspect)

    action.perform(browser, values)
  end

  log("done: success")
rescue Capybara::CapybaraError, Selenium::WebDriver::Error::WebDriverError => e
  log("done: error")

  error = Error.new(e.message)
  error.set_backtrace(e.backtrace)

  attach_screenshot(browser, error)

  raise error
ensure
  browser.quit
end

#required_paramsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/congress_forms/web_form.rb', line 49

def required_params
  required_actions = actions.dup

  required_actions.select!(&:required?)
  required_actions.select!(&:placeholder_value?)

  required_actions.map do |action|
    {
      value: action.value,
      max_length: action.max_length,
      options: action.select_options
    }
  end
end