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
21
22
# 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
    # TODO - add exception as a response page
    puts "ERROR: #{$!.message}"
    puts $!.backtrace
  end
end

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



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

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'
        do_get(route_info)
        puts "\tresponse code: #{response.code}" if response
      when 'post'
        do_post(route_info)
      when 'put'
        do_put(route_info)
      when 'delete'
      else
        raise "Unsupported verb: #{route[:verb]}"
    end
  end

  if (response)
    if (track_invocations)
      changes = detect_attribute_changes_in_models(invocations)
      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}" if changes.size > 0
      site_model.add(route_info[:route_container], controller, request, response, invocations, changes) {|redirect_url| redirect_proc.call(redirect_url) }
      puts "\tadded to site model"
    end
  else
    puts "ERROR: did not call #{route_info.inspect}"
  end
end

#do_get(route_info) ⇒ Object



63
64
65
# File 'lib/stratagem/crawler/route_invoker.rb', line 63

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

#do_post(route_info) ⇒ Object



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

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



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

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