Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/request data json #6

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
3,295 changes: 1,973 additions & 1,322 deletions app/public/apipie/javascripts/bundled/bootstrap.js

Large diffs are not rendered by default.

695 changes: 6 additions & 689 deletions app/public/apipie/stylesheets/bundled/bootstrap.min.css

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions app/views/apipie/apipies/_example.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div class="panel panel-<%= example[:code].to_i >= 400 ? 'danger' : 'success' %>">
<div class="panel-heading">
<a role href="#example-<%= index %>" data-toggle="collapse">
<%= example[:path] %>
<%- if !example[:request_data].blank? %>
with
<%= example[:request_data] %>
<% end %>
<span class="caret right"></span>
</a>
</div>
<div class="panel-body container">
<div class="row">
<div class="col-sm-12">
<h4>Path:</h4>
<pre class="prettyprint"><%= example[:path] %>
</pre>
<%- if !example[:request_data].blank? %>
<h4>Request Data:</h4>
<pre class="prettyprint"><%= example[:request_data] %></pre>
<h4>Request Data Json:</h4>
<pre class="prettyprint"><%= example[:request_data_json] %></pre>
<% end %>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<h4>Response Data:</h4>
<pre class="prettyprint"><%= example[:response_data] %></pre>
</div>
</div>
</div>
</div>
20 changes: 20 additions & 0 deletions app/views/apipie/apipies/_example_string.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div class='panel panel-default'>
<div class='panel-heading'>
<a role='button'
href='#example-<%= "#{index}#{example.lines.first.underscore}" %>'
data-toggle="collapse"
aria-controls='example-<%= "#{index}#{example.lines.first.underscore}" %>'>
<%= example.lines.first %>
<span class='caret right'></span>
</a>
</div>
<div id='example-<%= "#{index}#{example.lines.first.underscore}" %>'
aria-controls='example-<%= "#{index}#{example.lines.first.underscore}" %>'
aria-expanded='false'
class='panel-collapse collapse'>
<div class='panel-body'>
<h4>Response Data:</h4>
<pre class="prettyprint pre-scrollable" style='max-height: 600px'><%= example %></pre>
</div>
</div>
</div>
18 changes: 11 additions & 7 deletions app/views/apipie/apipies/_method_detail.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
<%= render(:partial => "metadata", :locals => {:meta => method[:metadata]}) %>
<% end %>

<% unless method[:examples].blank? %>
<%= heading(t('apipie.examples'), h_level) %>
<% method[:examples].each do |example| %>
<pre class="prettyprint"><%= example %></pre>
<% end %>
<% end %>

<% unless method[:params].blank? %>
<%= heading(t('apipie.params'), h_level) %>
<table class='table'>
Expand All @@ -38,3 +31,14 @@
<% end %>

<%= render(:partial => "headers", :locals => {:headers => method[:headers], :h_level => h_level }) %>

<% unless method[:examples].blank? %>
<%= heading(t('apipie.examples'), h_level) %>
<% method[:examples].each.with_index do |example, index| %>
<%- if example.is_a?(String) %>
<%= render partial: 'example_string', locals: { example: example, index: index} %>
<% else %>
<%= render partial: 'example', locals: { example: example, index: index} %>
<% end %>
<% end %>
<% end %>
9 changes: 6 additions & 3 deletions lib/apipie/extractor/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ def analyse_env(env)
@verb = env["REQUEST_METHOD"].to_sym
@path = env["PATH_INFO"].sub(/^\/*/,"/")
@query = env["QUERY_STRING"] unless env["QUERY_STRING"].blank?
input = env['rack.input'].read
@params = Rack::Utils.parse_nested_query(@query)
@params.merge!(env["action_dispatch.request.request_parameters"] || {})
if data = parse_data(env["rack.input"].read)
.merge(env['action_dispatch.request.request_parameters'] || {})
.merge(Rack::Utils.parse_nested_query(input) || {})
if data = parse_data(input)
@request_data = data
env["rack.input"].rewind
elsif form_hash = env["rack.request.form_hash"]
Expand Down Expand Up @@ -109,7 +111,8 @@ def record
:path => @path,
:params => @params,
:query => @query,
:request_data => @request_data,
:request_data => request_data,
:request_data_json => @params,
:response_data => @response_data,
:code => @code}
else
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/extractor/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def desc_to_s(description)
def ordered_call(call)
call = call.stringify_keys
ordered_call = OrderedHash.new
%w[title verb path versions query request_data response_data code show_in_doc recorded].each do |k|
%w[title verb path versions query request_data request_data_json 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
Expand Down
23 changes: 6 additions & 17 deletions lib/apipie/method_description.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'set'
module Apipie

class MethodDescription

class Api

attr_accessor :short_description, :path, :http_method, :from_routes, :options
Expand All @@ -14,7 +12,6 @@ def initialize(method, path, desc, options)
@from_routes = options[:from_routes]
@options = options
end

end

attr_reader :full_description, :method, :resource, :apis, :examples, :see, :formats, :metadata, :headers, :show
Expand Down Expand Up @@ -194,7 +191,12 @@ def load_recorded_examples
find_all { |ex| ex["show_in_doc"].to_i > 0 }.
find_all { |ex| ex["versions"].nil? || ex["versions"].include?(self.version) }.
sort_by { |ex| ex["show_in_doc"] }.
map { |ex| format_example(ex.symbolize_keys) }
map do |ex|
%w(request_data response_data request_data_json).each do |key|
ex[key] = format_example_data(ex[key]) if ex[key]
end
ex
end.map(&:symbolize_keys)
end

def format_example_data(data)
Expand All @@ -206,17 +208,6 @@ def format_example_data(data)
end
end

def format_example(ex)
example = ""
example << "// #{ex[:title]}\n" if ex[:title].present?
example << "#{ex[:verb]} #{ex[:path]}"
example << "?#{ex[:query]}" unless ex[:query].blank?
example << "\n" << format_example_data(ex[:request_data]).to_s if ex[:request_data]
example << "\n" << ex[:code].to_s
example << "\n" << format_example_data(ex[:response_data]).to_s if ex[:response_data]
example
end

def concern_subst(string)
return if string.nil?
if from_concern?
Expand All @@ -225,7 +216,5 @@ def concern_subst(string)
string
end
end

end

end
14 changes: 4 additions & 10 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -619,16 +619,10 @@ def reload_controllers
describe "examples" do

it "should be able to load examples from yml file" do
expect(Apipie.get_method_description(UsersController, :show).examples).to eq [<<EOS1, <<EOS2].map(&:chomp)
GET /users/14?verbose=true
200
{
"name": "Test User"
}
EOS1
GET /users/15
404
EOS2
examples = Apipie.get_method_description(UsersController, :show).examples
expect(examples).to all(be_a(Hash))
expect(examples.first[:path]).to eq('/users/14')
expect(examples.second[:path]).to eq('/users/15')
end

describe "document" do
Expand Down