Class: ApiDoc::Document
- Inherits:
-
Object
- Object
- ApiDoc::Document
- Defined in:
- lib/api_doc/document.rb
Instance Method Summary collapse
- #generate! ⇒ Object
-
#initialize(env, options = {}) ⇒ Document
constructor
A new instance of Document.
- #request_headers ⇒ Object
- #request_json ⇒ Object
- #request_params ⇒ Object
- #response_headers ⇒ Object
- #response_json ⇒ Object
Constructor Details
#initialize(env, options = {}) ⇒ Document
Returns a new instance of Document.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/api_doc/document.rb', line 4 def initialize(env, = {}) # puts "options: #{options.inspect}" @env = env # puts "@env: #{@env.inspect}" @request = @env.request # puts "@request.headers: #{@request.headers.inspect}" # @request.env.each do |key, value| # puts key # end # puts "@request.env['action_dispatch.request.parameters']: #{@request.env['action_dispatch.request.parameters'].inspect}" # puts "@request.env['action_dispatch.request.request_parameters']: #{@request.env['action_dispatch.request.request_parameters'].inspect}" # puts "@request.env['action_dispatch.request.content_type']: #{@request.env['action_dispatch.request.content_type'].inspect}" # puts "@request.env['action_dispatch.request.path_parameters']: #{@request.env['action_dispatch.request.path_parameters'].inspect}" @response = @env.response @params = @request.env['action_dispatch.request.parameters'] @path_parameters = @request.env['action_dispatch.request.path_parameters'] # puts File.join(@params["controller"], @params["action"]) .reverse_merge!(path: File.join(@params["controller"], @params["action"])) .reverse_merge!(slug: [:path].parameterize) @options = # puts "@options: #{@options.inspect}" end |
Instance Method Details
#generate! ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/api_doc/document.rb', line 74 def generate! template = ERB.new(File.read(File.join(File.dirname(__FILE__), "templates", "page.html.erb"))) html = template.result(binding) # puts html dir_path = File.join([ApiDoc::Config.view_path, File.dirname(@options[:path]).split("/")].flatten) # puts "dir_path: #{dir_path.inspect}" FileUtils.mkdir_p(dir_path) name = [@request.method, File.basename(@options[:path]), @options[:name].try(:gsub, " ", "_"), "(#{@response.status})"] name.compact! name = name.join("_") # File.open(File.join(dir_path, "#{name.join("_").gsub(" ", "_")}.html.erb"), 'w') do |f| # name = "#{@request.method}_#{File.basename(@options[:path])}_(#{@response.status})" # puts "name: #{name.inspect}" File.open(File.join(dir_path, "#{name}.html.erb"), 'w') do |f| f.write html end end |
#request_headers ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/api_doc/document.rb', line 62 def request_headers unless @request_headers @request_headers = {} @request.headers.each do |key, value| unless key.match(/^(action_dispatch|rack)/) @request_headers[key] = value end end end return @request_headers.map {|k, v| "'#{k}': #{v.to_json}"}.join("\n") end |
#request_json ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/api_doc/document.rb', line 28 def request_json # JSON.pretty_generate(JSON.parse(@response.body)) body = @request.env['rack.input'] if body.present? body.rewind # ret = URI.unescape(body.read) ret = body.read if ret.present? return JSON.pretty_generate(Rack::Utils.parse_nested_query(CGI.unescape(ret))) else return "// No request body necessary." end else return "// No request body necessary." end end |
#request_params ⇒ Object
45 46 47 |
# File 'lib/api_doc/document.rb', line 45 def request_params CGI.unescape(@params.except(*@path_parameters.keys, "format").to_query) end |
#response_headers ⇒ Object
58 59 60 |
# File 'lib/api_doc/document.rb', line 58 def response_headers @response.headers.map {|k, v| "#{k}: #{v.to_json}"}.join("\n") end |
#response_json ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/api_doc/document.rb', line 49 def response_json body = @response.body.strip if body.present? return JSON.pretty_generate(JSON.parse(body)) else return "// No response body returned." end end |