# File lib/apipie/extractor/writer.rb, line 5 def initialize(collector) @collector = collector @examples_file = File.join(Rails.root, "doc", "apipie_examples.yml") end
# File lib/apipie/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
# File lib/apipie/extractor/writer.rb, line 104 def self.logger Extractor.logger end
# File lib/apipie/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
# File lib/apipie/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
# File lib/apipie/extractor/writer.rb, line 43 def desc_to_s(description) "#{description[:controller].name}##{description[:action]}" end
# File lib/apipie/extractor/writer.rb, line 117 def hash_nodes_count(node) case node when Hash 1 + (node.values.map { |v| hash_nodes_count(v) }.reduce(:+) || 0) when Array node.map { |v| hash_nodes_count(v) }.reduce(:+) || 1 else 1 end end
# File lib/apipie/extractor/writer.rb, line 77 def load_new_examples @collector.records.reduce({}) do |h, (method, calls)| show_in_doc = nil recorded_examples = calls.map do |call| if show_in_doc.nil? show_in_doc = 1 if showable_in_doc?(call.with_indifferent_access) else show_in_doc = 0 end example = call.merge(:show_in_doc => show_in_doc.to_i, :recorded => true) example end h.update(method => recorded_examples) end end
# File lib/apipie/extractor/writer.rb, line 93 def load_old_examples if File.exists?(@examples_file) return YAML.load(File.read(@examples_file)) end return {} end
# File lib/apipie/extractor/writer.rb, line 100 def logger self.class.logger end
# File lib/apipie/extractor/writer.rb, line 62 def merge_old_new_examples new_examples = self.load_new_examples old_examples = self.load_old_examples merged_examples = [] (new_examples.keys + old_examples.keys).uniq.each do |key| if new_examples.has_key?(key) records = new_examples[key] else records = old_examples[key] end merged_examples << [key, records.map { |r| ordered_call(r) } ] end return merged_examples end
# File lib/apipie/extractor/writer.rb, line 47 def ordered_call(call) call = call.stringify_keys ordered_call = OrderedHash.new %w[verb path query request_data response_data code show_in_doc recorded].each do |k| next unless call.has_key?(k) ordered_call[k] = case call[k] when ActiveSupport::HashWithIndifferentAccess JSON.parse(call[k].to_json) # to_hash doesn't work recursively and I'm too lazy to write the recursion:) else call[k] end end return ordered_call end
# File lib/apipie/extractor/writer.rb, line 108 def showable_in_doc?(call) # we don't want to mess documentation with too large examples if hash_nodes_count(call["request_data"]) + hash_nodes_count(call["response_data"]) > 100 return false else return 1 end end