Skip to content

Commit 07546e7

Browse files
author
Gerard
committed
Disable the docs on prod
1 parent e51b7a5 commit 07546e7

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

lib/apiculture.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ def self.extended(in_class)
1515
super
1616
end
1717

18+
class Void
19+
def <<(_item); end
20+
def map; []; end
21+
def select; []; end
22+
end
23+
1824
IDENTITY_PROC = ->(arg) { arg }
1925

2026
AC_APPLY_TYPECAST_PROC = ->(cast_proc_or_method, v) {
@@ -276,7 +282,16 @@ def api_method(http_verb, path, options={}, &blk)
276282
end
277283

278284
def apiculture_stack
279-
@apiculture_actions_and_docs ||= []
285+
if environment == "development"
286+
@apiculture_actions_and_docs ||= []
287+
else
288+
@apiculture_actions_and_docs ||= Void.new
289+
end
280290
@apiculture_actions_and_docs
281291
end
292+
293+
# Based on the RACK_ENV it will generate documentation or not
294+
def environment
295+
@environment ||= ENV.fetch("RACK_ENV", "development")
296+
end
282297
end

lib/apiculture/app_documentation.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,38 @@ class TaggedMarkdown < Struct.new(:string, :section_class)
66
def to_markdown
77
string.to_markdown.to_s rescue string.to_s
88
end
9-
9+
1010
def to_html
1111
'<section class="%s">%s</section>' % [Rack::Utils.escape_html(section_class), render_markdown(to_markdown)]
1212
end
13-
13+
1414
def render_markdown(s)
1515
GitHub::Markup.render('section.markdown', s.to_s)
1616
end
1717
end
18-
18+
1919
def initialize(app, mountpoint, action_definitions_and_markdown_segments)
2020
@app_title = app.to_s
2121
@mountpoint = mountpoint
2222
@chunks = action_definitions_and_markdown_segments
2323
end
24-
24+
2525
# Generates a Markdown string that contains the entire API documentation
2626
def to_markdown
2727
(['## %s' % @app_title] + to_markdown_slices).join("\n\n")
28-
end
28+
end
2929

3030
def to_openapi
3131
OpenApiDocumentation::Base.new(@app_title, @mountpoint, @chunks)
3232
end
33-
33+
3434
# Generates an HTML fragment string that can be included into another HTML document
3535
def to_html_fragment
3636
to_markdown_slices.map do |tagged_markdown|
3737
tagged_markdown.to_html
3838
end.join("\n\n")
3939
end
40-
40+
4141
def to_markdown_slices
4242
markdown_slices = @chunks.map do | action_def_or_doc |
4343
if action_def_or_doc.respond_to?(:http_verb) # ActionDefinition
@@ -48,7 +48,7 @@ def to_markdown_slices
4848
end
4949
end
5050
end
51-
51+
5252
# Generates a complete HTML document string that can be saved into a file
5353
def to_html
5454
require 'mustache'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require_relative '../spec_helper'
2+
3+
describe "Apiculture.api_documentation in prod environments" do
4+
let(:app) do
5+
Class.new(Apiculture::App) do
6+
extend Apiculture
7+
8+
set_environment "production"
9+
10+
markdown_string 'This API is very important. Because it has to do with pancakes.'
11+
12+
documentation_build_time!
13+
14+
desc 'Check the pancake status'
15+
route_param :id, 'Pancake ID to check status on'
16+
responds_with 200, 'When the pancake is found', { status: 'Baking' }
17+
responds_with 404, 'When no such pancake exists', { status: 'No such pancake' }
18+
api_method :get, '/pancake/:id' do
19+
end
20+
21+
desc 'Throw away the pancake'
22+
route_param :id, 'Pancake ID to delete'
23+
api_method :delete, '/pancake/:id' do
24+
end
25+
26+
desc 'Pancake ingredients are in the URL'
27+
route_param :topping_id, 'Pancake topping ID', Integer, cast: :to_i
28+
api_method :get, '/pancake/with/:topping_id' do |topping_id|
29+
end
30+
end
31+
end
32+
33+
it 'does not generate any html in non-dev environments' do
34+
docco = app.api_documentation
35+
generated_html = docco.to_html_fragment
36+
37+
expect(generated_html).to eq("")
38+
end
39+
40+
# It still generates some small bits of Markdown but not a lot
41+
it 'does not generate app documentation in Markdown' do
42+
docco = app.api_documentation
43+
generated_markdown = docco.to_markdown
44+
45+
expect(generated_markdown.length).to eq(30)
46+
end
47+
end

spec/support/app.rb

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ def middleware_configurations
1212
@middleware_configurations || []
1313
end
1414

15+
# For testing only
16+
def set_environment(env)
17+
@environment ||= env
18+
end
19+
1520
def get(url, **options, &handler_blk)
1621
define_action :get, url, **options, &handler_blk
1722
end

0 commit comments

Comments
 (0)