Class: Restapi::Extractor::Writer

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collector) ⇒ Writer

Returns a new instance of Writer.



5
6
7
8
# File 'lib/restapi/extractor/writer.rb', line 5

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

Class Method Details

.update_action_description(controller, action) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/restapi/extractor/writer.rb', line 31

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



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/restapi/extractor/writer.rb', line 18

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



10
11
12
13
14
15
16
# File 'lib/restapi/extractor/writer.rb', line 10

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