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

The Examples formatting was using string concatenation, I needed more control and I needed to preprocess the docs to allow for changing the output at run time #327

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion apipie-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.require_paths = ["lib"]

s.add_dependency "rails", ">= 4.1"
s.add_dependency "rails", ">= 3.2"
s.add_development_dependency "rspec-rails", "~> 3.0"
s.add_development_dependency "sqlite3"
s.add_development_dependency "minitest"
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/apipie/apipies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def index
@resource = @doc[:resources].first if params[:resource].present?
@method = @resource[:methods].first if params[:method].present?
@languages = Apipie.configuration.languages

if Apipie.configuration.prerender_processor
@doc = Apipie.configuration.prerender_processor.call(@doc, params)
end


if @resource && @method
render 'method'
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/apipie_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ def heading(title, level=1)
end
end

def format_example_data(data)
case data
when Array, Hash
JSON.pretty_generate(data).gsub(/: \[\s*\]/,": []").gsub(/\{\s*\}/,"{}")
else
data
end
end

end
12 changes: 12 additions & 0 deletions app/views/apipie/apipies/_example.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% if example["title"] && example["title"].present? %>
<%= example["title"] %>
<% end %>
<%= example["verb"] -%> <%= example["path"] -%>
<% if example["request_data"] -%>
<%= format_example_data(example["request_data"]).to_s %>
<% end %>
<%= example["code"] %>
<% if example["response_data"] -%>
<%= format_example_data(example["response_data"]).to_s %>
<% end %>

7 changes: 7 additions & 0 deletions app/views/apipie/apipies/_method_detail.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
<% end %>
<% end %>

<% unless method[:examples_data].blank? %>
<%= heading(t('apipie.examples'), h_level) %>
<% method[:examples].each do |example| %>
<%= render(:partial => "example", :locals => {:example => example}) %>
<% end %>
<% end %>

<% unless method[:params].blank? %>
<%= heading(t('apipie.params'), h_level) %>
<table class='table'>
Expand Down
8 changes: 7 additions & 1 deletion lib/apipie/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Configuration
:persist_show_in_doc, :authorize,
:swagger_include_warning_tags, :swagger_content_type_input, :swagger_json_input_uses_refs,
:swagger_suppress_warnings, :swagger_api_host, :swagger_generate_x_computed_id_field,
:swagger_allow_additional_properties_in_response, :swagger_responses_use_refs
:swagger_allow_additional_properties_in_response, :swagger_responses_use_refs, :render_examples_with_template, :prerender_processor

alias_method :validate?, :validate
alias_method :required_by_default?, :required_by_default
Expand Down Expand Up @@ -143,6 +143,10 @@ def api_routes
@api_routes || Rails.application.routes
end

def prerender_processor=(lambda_processor)
@prerender_processor = lambda_processor
end

def initialize
@markup = Apipie::Markup::RDoc.new
@app_name = "Another API"
Expand Down Expand Up @@ -181,6 +185,8 @@ def initialize
@swagger_generate_x_computed_id_field = false
@swagger_allow_additional_properties_in_response = false
@swagger_responses_use_refs = true
@render_examples_with_template = false
@prerender_processor = nil
end
end
end
15 changes: 14 additions & 1 deletion lib/apipie/method_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(method, path, desc, options)

end

attr_reader :full_description, :method, :resource, :apis, :examples, :see, :formats, :metadata, :headers, :show
attr_reader :full_description, :method, :resource, :apis, :examples, :see, :formats, :metadata, :headers, :show, :examples_data

def initialize(method, resource, dsl_data)
@method = method.to_s
Expand Down Expand Up @@ -47,6 +47,8 @@ def initialize(method, resource, dsl_data)
@formats = dsl_data[:formats]
@examples = dsl_data[:examples]
@examples += load_recorded_examples
@examples_data = []
@examples_data = load_recorded_examples_data if Apipie::configuration.render_examples_with_template

@metadata = dsl_data[:meta]

Expand Down Expand Up @@ -188,6 +190,7 @@ def to_json(lang=nil)
:params => params_ordered.map{ |param| param.to_json(lang) }.flatten,
:returns => @returns.map{ |return_item| return_item.to_json(lang) }.flatten,
:examples => @examples,
:examples_data => @examples_data,
:metadata => @metadata,
:see => see.map(&:to_json),
:headers => headers,
Expand Down Expand Up @@ -232,13 +235,21 @@ def merge_returns(returns, new_returns)
end

def load_recorded_examples
return [] if Apipie::configuration.render_examples_with_template
(Apipie.recorded_examples[id] || []).
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) }
end

def load_recorded_examples_data
(Apipie.recorded_examples[id] || []).
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"] }
end

def format_example_data(data)
case data
when Array, Hash
Expand All @@ -249,6 +260,7 @@ def format_example_data(data)
end

def format_example(ex)

example = ""
example << "// #{ex[:title]}\n" if ex[:title].present?
example << "#{ex[:verb]} #{ex[:path]}"
Expand All @@ -257,6 +269,7 @@ def format_example(ex)
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)
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def params_ordered
end

def validate(value)
return false if !value.is_a? Hash
return false if !value.respond_to? :keys
if @hash_params
@hash_params.each do |k, p|
if Apipie.configuration.validate_presence?
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Apipie
VERSION = '0.5.15'
VERSION = '0.5.16'
end
12 changes: 12 additions & 0 deletions lib/tasks/apipie.rake
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ namespace :apipie do
Apipie.url_prefix = "./#{subdir}"
doc = Apipie.to_json(args[:version], nil, nil, lang)
doc[:docs][:link_extension] = "#{lang_ext(lang)}.html"

if Apipie.configuration.prerender_processor
doc[:docs] = Apipie.configuration.prerender_processor.call(doc[:docs], ENV)
end

generate_one_page(out, doc, lang)
generate_plain_page(out, doc, lang)
generate_index_page(out, doc, false, false, lang)
Apipie.url_prefix = "../#{subdir}"
generate_resource_pages(args[:version], out, doc, false, lang)
Apipie.url_prefix = "../../#{subdir}"

generate_method_pages(args[:version], out, doc, false, lang)
end
end
Expand Down Expand Up @@ -254,6 +260,9 @@ namespace :apipie do
FileUtils.mkdir_p(File.dirname(resource_file_base)) unless File.exists?(File.dirname(resource_file_base))

doc = Apipie.to_json(version, resource_name, nil, lang)
if Apipie.configuration.prerender_processor
doc[:docs] = Apipie.configuration.prerender_processor.call(doc[:docs], ENV)
end
doc[:docs][:link_extension] = (lang ? ".#{lang}.html" : ".html")
render_page("#{resource_file_base}#{lang_ext(lang)}.html", "resource", {:doc => doc[:docs],
:resource => doc[:docs][:resources].first, :language => lang, :languages => Apipie.configuration.languages})
Expand All @@ -268,6 +277,9 @@ namespace :apipie do
FileUtils.mkdir_p(File.dirname(method_file_base)) unless File.exists?(File.dirname(method_file_base))

doc = Apipie.to_json(version, resource_name, method[:name], lang)
if Apipie.configuration.prerender_processor
doc[:docs] = Apipie.configuration.prerender_processor.call(doc[:docs], ENV)
end
doc[:docs][:link_extension] = (lang ? ".#{lang}.html" : ".html")
render_page("#{method_file_base}#{lang_ext(lang)}.html", "method", {:doc => doc[:docs],
:resource => doc[:docs][:resources].first,
Expand Down