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

#call_route!(route_info, track_invocations = true) ⇒ 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
# File 'lib/stratagem/crawler/route_invoker.rb', line 23

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

  puts route_info[:verb].downcase+" "+route_info[: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'
      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: #{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) }
    end
  else
    puts "ERROR: did not call #{route_info.inspect}"
  end
end

#do_get(route_info) ⇒ Object



59
60
61
# File 'lib/stratagem/crawler/route_invoker.rb', line 59

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

#do_put(route_info) ⇒ Object



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

def do_put(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)
  
  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
  
  p hash_reads
  
  # 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)
  
  p params
  
  guess_unknown_params(models_by_hash_key, params, 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