Class: Apipie::Extractor::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/apipie/extractor/collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

Returns a new instance of Collector.



6
7
8
9
10
11
12
13
# File 'lib/apipie/extractor/collector.rb', line 6

def initialize
  @api_controllers_paths = Apipie.api_controllers_paths
  @ignored = Apipie.configuration.ignored_by_recorder
  @descriptions = Hash.new do |h, k|
    h[k] = {:params => {}, :errors => Set.new}
  end
  @records = Hash.new { |h,k| h[k] = [] }
end

Instance Attribute Details

#descriptionsObject (readonly)

Returns the value of attribute descriptions.



4
5
6
# File 'lib/apipie/extractor/collector.rb', line 4

def descriptions
  @descriptions
end

#recordsObject (readonly)

Returns the value of attribute records.



4
5
6
# File 'lib/apipie/extractor/collector.rb', line 4

def records
  @records
end

Instance Method Details

#add_routes_info(desc) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/apipie/extractor/collector.rb', line 98

def add_routes_info(desc)
  api_prefix = Apipie.api_base_url.sub(%r{/$},"")
  desc[:api] = Apipie::Extractor.apis_from_routes[[desc[:controller].name, desc[:action]]]
  if desc[:api]
    desc[:params].each do |name, param|
      if desc[:api].all? { |a| a[:path].include?(":#{name}") }
        param[:required] = true
      end
    end
  end
end

#add_to_records(record) ⇒ Object



35
36
37
38
# File 'lib/apipie/extractor/collector.rb', line 35

def add_to_records(record)
  key = "#{Apipie.get_resource_id(record[:controller])}##{record[:action]}"
  @records[key] << record
end

#controller_full_path(controller) ⇒ Object



15
16
17
# File 'lib/apipie/extractor/collector.rb', line 15

def controller_full_path(controller)
  Apipie::Extractor.controller_path controller.controller_path
end

#finalize_descriptionsObject



91
92
93
94
95
96
# File 'lib/apipie/extractor/collector.rb', line 91

def finalize_descriptions
  @descriptions.each do |method, desc|
    add_routes_info(desc)
  end
  return @descriptions
end

#handle_record(record) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/apipie/extractor/collector.rb', line 26

def handle_record(record)
  add_to_records(record)
  if ignore_call?(record)
    Extractor.logger.info("REST_API: skipping #{record_to_s(record)}")
  else
    refine_description(record)
  end
end

#ignore_call?(record) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/apipie/extractor/collector.rb', line 19

def ignore_call?(record)
  return true unless record[:controller]
  return true if @ignored.include?(record[:controller].name)
  return true if @ignored.include?("#{Apipie.get_resource_id(record[:controller].name)}##{record[:action]}")
  return true unless @api_controllers_paths.include?(controller_full_path(record[:controller]))
end

#record_to_s(record) ⇒ Object



110
111
112
# File 'lib/apipie/extractor/collector.rb', line 110

def record_to_s(record)
  "#{record[:controller]}##{record[:action]}"
end

#refine_description(record) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/apipie/extractor/collector.rb', line 40

def refine_description(record)
  description = @descriptions["#{record[:controller].name}##{record[:action]}"]
  description[:controller] ||= record[:controller]
  description[:action] ||= record[:action]

  refine_errors_description(description, record)
  refine_params_description(description[:params], record[:params])
end

#refine_errors_description(description, record) ⇒ Object



49
50
51
52
53
# File 'lib/apipie/extractor/collector.rb', line 49

def refine_errors_description(description, record)
  if record[:code].to_i >= 300 && !description[:errors].any? { |e| e[:code].to_i == record[:code].to_i }
    description[:errors] << {:code => record[:code]}
  end
end

#refine_params_description(params_desc, recorded_params) ⇒ Object



55
56
57
58
59
60
61
62
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
# File 'lib/apipie/extractor/collector.rb', line 55

def refine_params_description(params_desc, recorded_params)
  recorded_params.each do |key, value|
    params_desc[key] ||= {}
    param_desc = params_desc[key]

    if value.nil?
      param_desc[:allow_nil] = true
    else
      # we specify what type it might be. At the end the first type
      # that left is taken as the more general one
      unless param_desc[:type]
        param_desc[:type] = [:bool, :boolean, :number]
        param_desc[:type] << Hash if value.is_a? Hash
        param_desc[:type] << :undef
      end

      if [:boolean, :bool].include?(param_desc[:type].first) && (! [true, false, 1, 0].include?(value))
        param_desc[:type].shift
      end

      if param_desc[:type].first == :number && (key.to_s !~ /id$/ || !Apipie::Validator::NumberValidator.validate(value))
        param_desc[:type].shift
      end

      if param_desc[:type].first == :decimal && (key.to_s !~ /id$/ || !Apipie::Validator::DecimalValidator.validate(value))
        param_desc[:type].shift
      end
    end

    if value.is_a? Hash
      param_desc[:nested] ||= {}
      refine_params_description(param_desc[:nested], value)
    end
  end
end