Class: Glib::JsonCrawler::FormsSubmit

Inherits:
ActionCrawler show all
Defined in:
lib/glib/json_crawler/action_crawlers/forms_submit.rb

Instance Method Summary collapse

Methods inherited from ActionCrawler

#click, #crawl, #perform

Constructor Details

#initialize(http, args) ⇒ FormsSubmit

Returns a new instance of FormsSubmit.



4
5
6
7
8
9
10
11
# File 'lib/glib/json_crawler/action_crawlers/forms_submit.rb', line 4

def initialize(http, args)
  super(http)

  return if http.router.should_defer_crawl?(self, args)

  form = http.router.last_form
  execute(form)
end

Instance Method Details

#execute(form) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/glib/json_crawler/action_crawlers/forms_submit.rb', line 13

def execute(form)
  http = @http

  raise "Submit action needs to be inside a form: #{http.router.last_log}" unless form

  case form['view']
  when 'panels/bulkEdit2-v1'
    method = 'post'
    action = "forms/#{method}"
    url = form['import']['submitUrl']
    # TODO: Simulate data submission and check response
    @http.router.log action, url
  else
    method = form['method']
    action = "forms/#{method}"

    case method
    when 'patch', 'put', 'post'
      submit_form(form, action, method.to_sym)
    else
      url = form['url']
      http.router.log action, url
    end
  end
end

#form_post_param_groupsObject



92
93
94
95
96
97
# File 'lib/glib/json_crawler/action_crawlers/forms_submit.rb', line 92

def form_post_param_groups
  route = Rails.application.routes.recognize_path(@http.router.page_url)
  action_path = "#{route[:controller]}##{route[:action]}"

  post_request_scenarios[action_path]
end

#post_request_scenariosObject

Redeclare this class and implement this method.



105
106
107
# File 'lib/glib/json_crawler/action_crawlers/forms_submit.rb', line 105

def post_request_scenarios
  {}
end

#submit_form(view, action, method) ⇒ Object



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
89
90
# File 'lib/glib/json_crawler/action_crawlers/forms_submit.rb', line 39

def submit_form(view, action, method)
  url = view['url']
  fields = []
  params = {}
  child_views = view['childViews'] || []

  # If this form is page level, we need to check the child views of the entire page.
  if view['view'] == 'panels/fullPageForm'
    child_views += @http.router.page_spec['header']&.[]('childViews') || []
    child_views += @http.router.page_spec['body']&.[]('childViews') || []
    child_views += @http.router.page_spec['footer']&.[]('childViews') || []
  end

  @http.router.crawl_multiple child_views, ->(child) do
    next if child['template'].present?

    name = child['view'] || ''
    if name.start_with?('fields/')
      fields << child

      include_params = case name
                       when 'fields/check', 'fields/check-v1'
                         child['checkValue'] == child['value']
                       else
                         true
      end

      params[child['name']] = child['value'] if include_params
    end
  end

  if (on_submit = view['onSubmit']) && !on_submit[:__executed_by_crawler]
    on_submit[:__executed_by_crawler] = true
    perform(on_submit)
    return
  end

  case method
  when :patch, :put
    json = @http.patch url, action, params
    perform(json['onResponse'])
  when :post
    if (groups = form_post_param_groups)
      groups.each do |group_params|
        json = @http.post url, action, group_params
        perform(json['onResponse'])
      end
    else
      @http.router.log action, url
    end
  end
end