Module: Apipie::Extractor

Defined in:
lib/apipie/extractor.rb,
lib/apipie/extractor/writer.rb,
lib/apipie/extractor/recorder.rb,
lib/apipie/extractor/collector.rb

Defined Under Namespace

Classes: ActionDescriptionUpdater, Collector, OrderedHash, Recorder, Writer

Class Method Summary collapse

Class Method Details

.apis_from_routesObject

TODO: this is a loooooooooong method :)



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
90
91
92
93
94
95
96
97
98
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
# File 'lib/apipie/extractor.rb', line 57

def apis_from_routes
  return @apis_from_routes if @apis_from_routes

  api_prefix = Apipie.api_base_url.sub(/\/$/,"")
  all_routes = Rails.application.routes.routes.map do |r|
    {
      :verb => case r.verb
               when Regexp then r.verb.source[/\w+/]
               else r.verb.to_s
               end,
      :path => case
               when r.path.respond_to?(:spec) then r.path.spec.to_s
               else r.path.to_s
               end,
      :controller => r.requirements[:controller],
      :action => r.requirements[:action]
    }


  end
  api_routes = all_routes.find_all do |r|
    r[:path].starts_with?(Apipie.api_base_url)
  end

  @apis_from_routes = Hash.new { |h, k| h[k] = [] }

  api_routes.each do |route|
    controller_path, action = route[:controller], route[:action]
    next unless controller_path && action

    controller = "#{controller_path}_controller".camelize

    path = if /^#{Regexp.escape(api_prefix)}(.*)$/ =~ route[:path]
             $1.sub!(/\(\.:format\)$/,"")
           else
             nil
           end

    if route[:verb].present?
      @apis_from_routes[[controller, action]] << {:method => route[:verb], :path => path}
    end
  end
  @apis_from_routes

  resource_descriptions = Apipie.resource_descriptions.values.map(&:values).flatten
  method_descriptions = resource_descriptions.map(&:method_descriptions).flatten
  apis_from_docs = method_descriptions.reduce({}) do |h, desc|
    apis = desc.method_apis_to_json.map do |api|
      { :method => api[:http_method],
        :path => api[:api_url],
        :desc => api[:short_description] }
    end
    h.update(desc.id => apis)
  end

  @apis_from_routes.each do |(controller, action), new_apis|
    method_key = "#{Apipie.get_resource_name(controller.constantize)}##{action}"
    old_apis = apis_from_docs[method_key] || []
    new_apis.each do |new_api|
      new_api[:path].sub!(/\(\.:format\)$/,"")
      old_api = old_apis.find do |api|
        api[:path] == "#{api_prefix}#{new_api[:path]}"
      end
      if old_api
        new_api[:desc] = old_api[:desc]
      end
    end
  end
  @apis_from_routes
end

.call_finishedObject



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

def call_finished
  @collector ||= Collector.new
  if record = call_recorder.record
    @collector.handle_record(record)
  end
ensure
  Thread.current[:apipie_call_recorder] = nil
end

.call_recorderObject



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

def call_recorder
  Thread.current[:apipie_call_recorder] ||= Recorder.new
end

.loggerObject



31
32
33
# File 'lib/apipie/extractor.rb', line 31

def logger
  Rails.logger
end

.write_docsObject



48
49
50
# File 'lib/apipie/extractor.rb', line 48

def write_docs
  Writer.new(@collector).write_docs if @collector
end

.write_examplesObject



52
53
54
# File 'lib/apipie/extractor.rb', line 52

def write_examples
  Writer.new(@collector).write_examples if @collector
end