class Autodoc::Document
Constants
- DEFAULT_DOCUMENT_PATH_FROM_EXAMPLE
Public Class Methods
Source
# File lib/autodoc/document.rb, line 20 def initialize(context, example) @context = context @example = example end
Source
# File lib/autodoc/document.rb, line 16 def self.render(*args) new(*args).render end
Public Instance Methods
Source
# File lib/autodoc/document.rb, line 43 def identifier title.gsub(" ", "-").gsub(/[:\/]/, "").downcase end
Source
# File lib/autodoc/document.rb, line 25 def line_number @context.inspect[/:([\d]+)\)>$/, 1].to_i end
Source
# File lib/autodoc/document.rb, line 29 def pathname @pathname ||= begin Autodoc.configuration.pathname + document_path_from_example.call(example) end end
Source
# File lib/autodoc/document.rb, line 35 def render ERB.new(Autodoc.configuration.template, trim_mode: "-").result(binding) end
Private Instance Methods
Source
# File lib/autodoc/document.rb, line 181 def controller request.params[:controller] end
Source
# File lib/autodoc/document.rb, line 197 def description if @context.respond_to?(:description) @context.description.strip_heredoc else example.description.sub(/\A./, &:upcase).concat('.') end end
Source
# File lib/autodoc/document.rb, line 49 def document_path_from_example Autodoc.configuration.document_path_from_example || DEFAULT_DOCUMENT_PATH_FROM_EXAMPLE end
Source
# File lib/autodoc/document.rb, line 53 def example if ::RSpec::Core::Version::STRING.match(/\A(?:3\.|2.99\.)/) @example else @context.example end end
Source
# File lib/autodoc/document.rb, line 219 def has_validators? !!(defined?(WeakParameters) && validators) end
Source
# File lib/autodoc/document.rb, line 215 def parameters validators.map {|validator| Parameter.new(validator) }.join("\n") end
Source
# File lib/autodoc/document.rb, line 209 def parameters_section if has_validators? && parameters.present? "\n### Parameters\n#{parameters}\n" end end
Source
# File lib/autodoc/document.rb, line 205 def path example.full_description[%r<(GET|POST|PATCH|PUT|DELETE) ([^ ]+)>, 2] end
Source
# File lib/autodoc/document.rb, line 61 def request @request ||= begin if using_rack_test? ActionDispatch::Request.new(@context.last_request.env) else @context.request end end end
Source
# File lib/autodoc/document.rb, line 122 def request_body if instance_variable_defined?(:@request_body) @request_body else @request_body = begin case when request.try(:media_type) == "multipart/form-data" "multipart/form-data" when request.headers["Content-Type"].try(:include?, "application/json") request_body_parsed_as_json else request.body.string end end end end
Source
# File lib/autodoc/document.rb, line 139 def request_body_parsed_as_json JSON.pretty_generate(JSON.parse(request.body.string)) rescue JSON::ParserError end
Source
# File lib/autodoc/document.rb, line 118 def request_body_section "\n\n#{request_body}" if request_body.present? end
Source
# File lib/autodoc/document.rb, line 85 def request_header table = request_header_from_fixed_keys table.merge!(request_header_from_http_prefix) table.reject! {|key, value| value.blank? } table = Hash[table.map {|key, value| [key.split(?_).map(&:downcase).map(&:camelize).join(?-), value] }] table.except!(*Autodoc.configuration.suppressed_request_header) table.map {|key, value| [key, value].join(": ") }.sort.join("\n") end
Source
# File lib/autodoc/document.rb, line 104 def request_header_from_fixed_keys table = request.headers table = table.env if table.respond_to?(:env) table.slice("CONTENT_TYPE", "CONTENT_LENGTH", "LOCATION") end
Source
# File lib/autodoc/document.rb, line 94 def request_header_from_http_prefix request.headers.inject({}) do |table, (key, value)| if key.to_s.start_with?("HTTP_") table.merge(key.to_s.gsub(/^HTTP_/, "") => value) else table end end end
Source
# File lib/autodoc/document.rb, line 110 def request_http_version request.env["HTTP_VERSION"] || "HTTP/1.1" end
Source
# File lib/autodoc/document.rb, line 114 def request_query "?#{URI.decode_www_form_component(request.query_string.force_encoding(Encoding::UTF_8))}" unless request.query_string.empty? end
Source
# File lib/autodoc/document.rb, line 71 def response @response ||= begin if using_rack_test? @context.last_response else @context.response end end end
Source
# File lib/autodoc/document.rb, line 159 def response_body if instance_variable_defined?(:@response_body) @response_body else @response_body = begin case when response.header["Content-Type"].try(:include?, "image/") response.header["Content-Type"] when response.header["Content-Type"].try(:include?, "application/json") response_body_parsed_as_json else response.body end end end end
Source
# File lib/autodoc/document.rb, line 176 def response_body_parsed_as_json JSON.pretty_generate(JSON.parse(response.body)) rescue JSON::ParserError end
Source
# File lib/autodoc/document.rb, line 155 def response_body_section "\n\n#{response_body}" if response_body.present? end
Source
# File lib/autodoc/document.rb, line 144 def response_header table = response.headers.clone table = table.to_hash if table.respond_to?(:to_hash) table.except!(*Autodoc.configuration.suppressed_response_header) table.map {|key, value| [key, value].join(": ") }.sort.join("\n") end
Source
# File lib/autodoc/document.rb, line 151 def response_http_version response.header["HTTP_VERSION"] || "HTTP/1.1" end
Source
# File lib/autodoc/document.rb, line 193 def transaction @transaction ||= Autodoc::Transaction.build(@context) end
Source
# File lib/autodoc/document.rb, line 189 def using_rack_test? !!defined?(Rack::Test::Methods) && @context.class.ancestors.include?(Rack::Test::Methods) end
Source
# File lib/autodoc/document.rb, line 223 def validators WeakParameters.stats[controller][action].try(:validators) end