Module: Stratagem::Crawler::ParameterResolver

Included in:
RouteInvoker
Defined in:
lib/stratagem/crawler/parameter_resolver.rb

Instance Method Summary collapse

Instance Method Details

#resolve_id_with_convention(route_container, unknown_params, resolved_params) ⇒ Object

resolve the ID parameter by looking at the previous segment of the URL



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/stratagem/crawler/parameter_resolver.rb', line 81

def resolve_id_with_convention(route_container, unknown_params, resolved_params)
  if (unknown_params.include?('id'))
    route_container.path.split('/').inject(nil) {|prev,part|
      if (prev && part =~ /^\:id/)
        begin
          resolved_params['id'] = prev.singularize.camelize.constantize
          unknown_params.delete('id')
        rescue
          Stratagem.logger.error($!)
        end
      end
      part
    }
  end
end

#resolve_parameter_types(route_container) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stratagem/crawler/parameter_resolver.rb', line 4

def resolve_parameter_types(route_container)
  resolved_params = {}
  route_infos, params = build_url(route_container, resolved_params)
  route_info = route_infos.first
  unknown_params = params.keys
  puts "resolving parameter types - #{unknown_params.inspect}"

  if (unknown_params.size > 0)
    resolve_with_convention(unknown_params, resolved_params)
    resolve_with_instrumentation(route_container, resolved_params)
    resolve_id_with_convention(route_container, unknown_params, resolved_params)
    log "\tresolved parameter types - #{resolved_params.keys.inspect}, unknown - #{unknown_params.inspect}"
  end
  
  if (resolved_params.size > 0)
    resolved_params
  else
    nil
  end
end

#resolve_with_convention(unknown_params, resolved_params) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/stratagem/crawler/parameter_resolver.rb', line 61

def resolve_with_convention(unknown_params, resolved_params)
  unknown_params.clone.each do |param|
    if (param.to_s =~ /_id$/)
      begin
        puts "\t\tattempting to resolve #{param.sub(/^:/,'').sub(/_id$/, '').camelize}"
        model = param.sub(/^:/,'').sub(/_id$/, '').camelize.constantize
        #success
        resolved_params[param] = model
        unknown_params.delete(param)
        puts "\t\tresolved #{param} to #{model.name} using convention"
      rescue NameError
        puts "NAMEERROR: #{$!.message}"
      rescue
        puts "ERROR: #{$!.message}"
      end
    end
  end
end

#resolve_with_instrumentation(route_container, resolved_params) ⇒ Object



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/parameter_resolver.rb', line 25

def resolve_with_instrumentation(route_container, resolved_params)
  route_infos, params = build_url(route_container, resolved_params)
  route_info = route_infos.first
  unknown_params = params.keys

  progress = nil
  while ((unknown_params.size > 0) && (progress.nil? || (progress > 0)))
    progress = 0

    delta = model_invocations_for_request do
      call_route(route_info, false)
    end

    puts "\tcalled route, found #{delta.size} invocations"

    unknown_params.clone.each do |key|
      value = params[key]
      value_s = params[key].map {|v| v.to_s }
      delta.each do |invocation|
        # TODO inspect is a hack, refactor
        if (invocation.args.include?(value.first)) || (invocation.args.inspect.include?('"'+value.first.to_s+'"'))
          # found match
          puts "\t\tresolved #{key} to #{invocation.model_class} using instrumentation"
          unknown_params.delete(key)
          resolved_params[key] = invocation.model_class
          progress += 1
          break
        end
      end
    end

    route_infos, params = build_url(route_container, resolved_params)
    route_info = route_infos.first
  end
end