module Apipie::Extractor

Public Class Methods

apis_from_routes() click to toggle source
# File lib/apipie/extractor.rb, line 81
def apis_from_routes
  return @apis_from_routes if @apis_from_routes

  @api_prefix = Apipie.api_base_url.sub(/\/$/,"")
  populate_api_routes
  update_api_descriptions

  @apis_from_routes
end
call_finished() click to toggle source
# File lib/apipie/extractor.rb, line 62
def call_finished
  @collector ||= Collector.new
  if record = call_recorder.record
    @collector.handle_record(record)
  end
end
call_recorder() click to toggle source
# File lib/apipie/extractor.rb, line 58
def call_recorder
  Thread.current[:apipie_call_recorder] ||= Recorder.new
end
clean_call_recorder() click to toggle source
# File lib/apipie/extractor.rb, line 69
def clean_call_recorder
  Thread.current[:apipie_call_recorder] = nil
end
controller_path(name) click to toggle source
# File lib/apipie/extractor.rb, line 91
def controller_path(name)
  Apipie.api_controllers_paths.detect { |p| p.include?("#{name}_controller.rb") }
end
finish() click to toggle source
# File lib/apipie/extractor.rb, line 36
def finish
  record_params, record_examples = false, false
  case Apipie.configuration.record
  when "params"   then record_params = true
  when "examples" then record_examples = true
  when "all"      then record_params = true, record_examples = true
  end

  if record_examples
    puts "Writing examples to a file"
    write_examples
  end
  if record_params
    puts "Updating auto-generated documentation"
    write_docs
  end
end
logger() click to toggle source
# File lib/apipie/extractor.rb, line 54
def logger
  Rails.logger
end
start(record) click to toggle source
# File lib/apipie/extractor.rb, line 31
def start(record)
  Apipie.configuration.record = record
  Apipie.configuration.force_dsl = true
end
write_docs() click to toggle source
# File lib/apipie/extractor.rb, line 73
def write_docs
  Writer.new(@collector).write_docs if @collector
end
write_examples() click to toggle source
# File lib/apipie/extractor.rb, line 77
def write_examples
  Writer.new(@collector).write_examples if @collector
end

Private Class Methods

all_api_routes() click to toggle source
# File lib/apipie/extractor.rb, line 97
def all_api_routes
  all_routes = Apipie.configuration.api_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

  return all_routes.find_all do |r|
    r[:path].starts_with?(Apipie.api_base_url)
  end
end
all_apis_from_docs() click to toggle source
# File lib/apipie/extractor.rb, line 140
def all_apis_from_docs
  resource_descriptions = Apipie.resource_descriptions.values.map(&:values).flatten
  method_descriptions = resource_descriptions.map(&:method_descriptions).flatten

  return 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
end
populate_api_routes() click to toggle source
# File lib/apipie/extractor.rb, line 118
def populate_api_routes
  @apis_from_routes = Hash.new { |h, k| h[k] = [] }

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

    controller_path = controller_path.split('::').map(&:camelize).join('::')
    controller = "#{controller_path}Controller"

    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
end
update_api_descriptions() click to toggle source
# File lib/apipie/extractor.rb, line 154
def update_api_descriptions
  apis_from_docs = all_apis_from_docs
  @apis_from_routes.each do |(controller, action), new_apis|
    method_key = "#{Apipie.get_resource_name(controller.safe_constantize || next)}##{action}"
    old_apis = apis_from_docs[method_key] || []
    new_apis.each do |new_api|
      new_api[:path].sub!(/\(\.:format\)$/,"") if new_api[:path]
      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
end