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.



71
72
73
# File 'lib/apipie/extractor/writer.rb', line 71

def initialize(collector)
  @collector = collector
end

Class Method Details

.compressedObject



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

def compressed
  Apipie.configuration.compress_examples
end

.examples_fileObject



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

def examples_file
  pure_path = Rails.root.join(
    Apipie.configuration.doc_path, 'apipie_examples.json'
  )
  zipped_path = pure_path.to_s + '.gz'
  return zipped_path if compressed
  pure_path.to_s
end

.load_recorded_examplesObject



28
29
30
31
# File 'lib/apipie/extractor/writer.rb', line 28

def load_recorded_examples
  return {} unless File.exist?(examples_file)
  load_json_examples
end

.update_action_description(controller, action) ⇒ Object



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

def 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

.write_recorded_examples(examples) ⇒ Object



21
22
23
24
25
26
# File 'lib/apipie/extractor/writer.rb', line 21

def write_recorded_examples(examples)
  FileUtils.mkdir_p(File.dirname(examples_file))
  content = serialize_examples(examples)
  content = Zlib::Deflate.deflate(content).force_encoding('utf-8') if compressed
  File.open(examples_file, 'w') { |f| f << content }
end

Instance Method Details

#write_docsObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/apipie/extractor/writer.rb', line 81

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



76
77
78
79
# File 'lib/apipie/extractor/writer.rb', line 76

def write_examples
  merged_examples = merge_old_new_examples
  self.class.write_recorded_examples(merged_examples)
end