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, form) ⇒ FormsSubmit

Returns a new instance of FormsSubmit.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/glib/json_crawler/action_crawlers/forms_submit.rb', line 4

def initialize(http, form)
  super(http)

  raise 'Submit action needs to be inside a form' unless form

  @http = http

  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

Instance Method Details

#form_post_param_groupsObject



76
77
78
79
80
81
# File 'lib/glib/json_crawler/action_crawlers/forms_submit.rb', line 76

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.



89
90
91
# File 'lib/glib/json_crawler/action_crawlers/forms_submit.rb', line 89

def post_request_scenarios
  {}
end

#submit_form(view, action, method) ⇒ Object



23
24
25
26
27
28
29
30
31
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
# File 'lib/glib/json_crawler/action_crawlers/forms_submit.rb', line 23

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