Created
November 5, 2017 13:46
-
-
Save hiroki-uchida/9581097267a8b9ca8b4c13877ea107ba to your computer and use it in GitHub Desktop.
autodoc with curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% # coding: UTF-8 | |
# Generate curl options | |
curl_options = [] | |
# request | |
curl_options << "-X #{method} https://#{request.host}#{request.path}" | |
# headers | |
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.each do |key, value| | |
curl_options << "-H \"#{key}: #{value}\"" | |
end | |
# GET params | |
Hash[URI::decode_www_form(URI.unescape(URI.encode(request.query_string)))].each do |key, value| | |
curl_options << "-d \"#{key}=#{value}\"" | |
end | |
# POST, PUT, DELETE params | |
Hash[URI::decode_www_form(request_body)].map do |key, value| | |
curl_options << "-d \"#{key}=#{value}\"" | |
end | |
%> | |
## <%= title %> | |
<%= description %> | |
<%= parameters_section %> | |
### Example | |
#### Request | |
##### curl | |
``` | |
$ curl \ | |
<%= curl_options.map{|option| "#{option}"}.join(" \\\n") %> | |
``` | |
##### RAW | |
``` | |
<%= method %> <%= request.path %><%= request_query %> <%= request_http_version %> | |
<%= request_header %><%= request_body_section %> | |
``` | |
#### Response | |
status code: <%= response.status %> | |
``` | |
<%= response_body_section %> | |
``` | |
##### RAW | |
``` | |
<%= response_http_version %> <%= response.status %> | |
<%= response_header %><%= response_body_section %> | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment