Class: Apipie::Extractor::Writer

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collector) ⇒ Writer

Returns a new instance of Writer.



7
8
9
10
# File 'lib/apipie/extractor/writer.rb', line 7

def initialize(collector)
  @collector = collector
  @examples_file = File.join(Rails.root, "doc", "apipie_examples.yml")
end

Class Method Details

.update_action_description(controller, action) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/apipie/extractor/writer.rb', line 33

def self.update_action_description(controller, action)
  updater = ActionDescriptionUpdater.new(controller, action)
  yield updater
  updater.write!
rescue ActionDescriptionUpdater::ControllerNotFound
  logger.warn("REST_API: Couldn't find controller file for #{controller}")
rescue ActionDescriptionUpdater::ActionNotFound
  logger.warn("REST_API: Couldn't find action #{action} in #{controller}")
end

Instance Method Details

#write_docsObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/apipie/extractor/writer.rb', line 20

def write_docs
  descriptions = @collector.finalize_descriptions
  descriptions.each do |_, desc|
    if desc[:api].empty?
      logger.warn("REST_API: Couldn't find any path for #{desc_to_s(desc)}")
      next
    end
    self.class.update_action_description(desc[:controller], desc[:action]) do |u|
      u.update_generated_description desc
    end
  end
end

#write_examplesObject



12
13
14
15
16
17
18
# File 'lib/apipie/extractor/writer.rb', line 12

def write_examples
  merged_examples = merge_old_new_examples
  FileUtils.mkdir_p(File.dirname(@examples_file))
  File.open(@examples_file, "w") do |f|
    f << YAML.dump(OrderedHash[*merged_examples.sort_by(&:first).flatten(1)])
  end
end