Module: Stratagem::Crawler::RouteInvoker

Includes:
ParameterResolver
Included in:
Session
Defined in:
lib/stratagem/crawler/route_invoker.rb

Constant Summary collapse

IGNORE_PARAMETERS =
[:utf8, :_method, :authenticity_token, 'utf8', '_method', 'authenticity_token']

Instance Method Summary collapse

Methods included from ParameterResolver

#resolve_id_with_convention, #resolve_parameter_types, #resolve_with_convention, #resolve_with_instrumentation

Instance Method Details

#call_route(route_info, track_invocations = true) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/stratagem/crawler/route_invoker.rb', line 14

def call_route(route_info, track_invocations=true)
  begin
    call_route!(route_info, track_invocations)
  rescue Exception
    Stratagem.logger.error($!)
  end
end

#call_route!(route_info, track_invocations = true) ⇒ Object



22
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
# File 'lib/stratagem/crawler/route_invoker.rb', line 22

def call_route!(route_info, track_invocations=true)
  return if route_info.nil?

  puts route_info[:verb].downcase+" "+route_info[:path]+" - #{route_info[:route_container].path}"
  verb = route_info[:verb].downcase
  verb = 'get' if verb == '' || verb == 'any'

  invocations = model_invocations_for_request do
    case verb
      when 'get'
        puts "\t#{route_info[:path].to_s == '/connections'} - #{route_info[:path]}"
        do_get(route_info)
      when 'post'
        do_post(route_info)
      when 'put'
        do_put(route_info)
      when 'delete'
      else
        raise "Unsupported verb: #{route[:verb]}"
    end
  end

  if (response && track_invocations)
    changes = detect_attribute_changes_in_models(invocations)
    if (![500].include?(response.code) || (changes.size > 0) || (invocations.size > 0))
      puts "\tfound #{invocations.size} invocations"
      invocations.each do |i|
        puts "\t\t#{i.controller_action} -> #{i.model_class}"
      end
      puts "\tchanges values: #{changes.values.inspect}"
      page = site_model.add(route_info[:route_container], controller, request, response, invocations, changes) {|redirect_url| redirect_proc.call(redirect_url) }
      puts "\tresponse code: #{response.code} - body size: #{page.response_body.size}"
      puts "\tadded to site model"
    end
  else
    puts "ERROR: did not call #{route_info[:path]}"
  end
end

#do_get(route_info) ⇒ Object



61
62
63
# File 'lib/stratagem/crawler/route_invoker.rb', line 61

def do_get(route_info)
  get route_info[:path]
end

#do_post(route_info) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/stratagem/crawler/route_invoker.rb', line 99

def do_post(route_info)
  raise "unable to invoke PUT requests, application must first be crawled with GET requests for phase #{phase}." unless site_model.pages.size > 0

  form = guess_form_for_route(route_info)
  if (form.nil?)
    puts "WARNING: could not locate form for route #{route_info[:route_container].path}"
  end
  
  params = {}

  # note: this should fail to generate anything meaningful, as we have not yet set up the parameters
  hash_reads = Hash.track_parameter_reads do
    begin
      post route_info[:path], params
    rescue
      # TODO - log error as page response
      puts "ERROR: #{response.code}"
    end
  end
  
  # let's find out what the method is looking for in the params object
  models_by_hash_key = infer_models_for_param_reads(route_info[:route_container],hash_reads)
  params = map_models_to_attributes(models_by_hash_key)
  
  guess_unknown_params(models_by_hash_key, params, form) if form

  # run again with the params
  puts "POSTING: #{route_info[:path]} with #{params.inspect}"

  invocation_delta = model_invocations_for_request(:write) do
    post route_info[:path], params
  end
end

#do_put(route_info) ⇒ Object



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
91
92
93
94
95
96
97
# File 'lib/stratagem/crawler/route_invoker.rb', line 65

def do_put(route_info)
  # raise "unable to invoke PUT requests, application must first be crawled with GET requests for phase #{site_model.name}." unless site_model.pages.size > 0

  form = guess_form_for_route(route_info)
  if (form.nil?)
    puts "WARNING: could not locate form for route #{route_info[:route_container].path}"
  end
  
  params = {}

  # note: this should fail to generate anything meaningful, as we have not yet set up the parameters
  hash_reads = Hash.track_parameter_reads do
    begin
      put route_info[:path], params
    rescue
      # TODO - log error as page response
      puts "ERROR: #{response.code}"
    end
  end
  
  # let's find out what the method is looking for in the params object
  models_by_hash_key = infer_models_for_param_reads(route_info[:route_container],hash_reads)
  params = map_models_to_attributes(models_by_hash_key)
  
  guess_unknown_params(models_by_hash_key, params, form) if form

  # run again with the params
  puts "PUTTING: #{route_info[:path]} with #{params.inspect}"

  invocation_delta = model_invocations_for_request(:write) do
    put route_info[:path], params
  end
end

#visit(route_container) ⇒ Object



7
8
9
10
11
12
# File 'lib/stratagem/crawler/route_invoker.rb', line 7

def visit(route_container)
  # puts "Visiting #{route_container.route}"
  build_urls(route_container).each do |route_info|
    call_route(route_info)
  end
end