lib/ghost_writer/document.rb in ghost_writer-0.4.1 vs lib/ghost_writer/document.rb in ghost_writer-0.4.2

- old
+ new

@@ -1,80 +1,60 @@ +require 'ghost_writer/writer' + class GhostWriter::Document - attr_reader :title, :description, :location, :request_method, :path_info, :param_example, :status_example, :response_example, :output, :relative_path + attr_reader :basename, :relative_path, :title, :description, :location, :request_method, :path_info, :response_format, :param_example, :status_code + attr_accessor :header - def initialize(output, attrs) - format_module = "GhostWriter::Format::#{attrs[:format].to_s.classify}" - extend(format_module.constantize) - - @output = output - @relative_path = Pathname.new(output).relative_path_from(GhostWriter.output_path) + def initialize(basename, attrs) + @basename = basename + @relative_path = Pathname.new(basename).relative_path_from(GhostWriter.output_path) @title = attrs[:title] @description = attrs[:description] @location = attrs[:location] @request_method = attrs[:request_method] @path_info = attrs[:path_info] - @param_example = attrs[:param_example] - @status_example = attrs[:status_example] - @response_example = attrs[:response_example] + + param_example = attrs[:param_example].stringify_keys + @response_format = param_example.delete("format").to_s + @param_example = param_example + @status_code = attrs[:status_code] + @response_body = attrs[:response_body] end - def write_file(overwrite = false) - unless File.exist?(File.dirname(output)) - FileUtils.mkdir_p(File.dirname(output)) - end + def write_file(options = {}) + writer = GhostWriter::Writer.new(self, options) + writer.write_file + end - mode = overwrite ? "w" : "a" - doc = File.open("#{output}.#{extname}", mode) - - if overwrite - doc.write paragraph(document_header) - end - - doc.write(document_body) - - doc.close + def serialized_params + MultiJson.dump(param_example) end - def document_header - <<EOP -#{headword(title, 2)} - -EOP + def response_body + arrange_json(@response_body) end - def document_body - <<EOP -#{headword(description, 3)} - -#{headword("access path:", 4)} -#{quote("#{request_method} #{path_info}")} - -#{headword("request params:", 4)} -#{quote(param_example.inspect, :ruby)} - -#{headword("status code:", 4)} -#{quote(status_example)} - -#{headword("response:", 4)} -#{quote_response(response_example)} - -Generated by "#{description}\" at #{location} - -#{separator(32)} - -EOP + def response_format(json_convert_to_javascript = false) + if json_convert_to_javascript && @response_format == "json" + "javascript" + else + @response_format + end end - private - def quote_response(body) - if param_example[:format] && param_example[:format].to_sym == :json - quote(arrange_json(response_example), :javascript) + def content_type + if response_format == "json" + "application/json; charset=UTF-8" else - quote(response_example, param_example[:format]) + "text/html; charset=UTF-8" end end + private + def arrange_json(body) + return body unless response_format == "json" + data = ActiveSupport::JSON.decode(body) if data.is_a?(Array) || data.is_a?(Hash) JSON.pretty_generate(data) else data