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

Stop creating the definitions on prod #38

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/apiculture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
module Apiculture
require_relative 'apiculture/version'
require_relative 'apiculture/indifferent_hash'
require_relative 'apiculture/app'
require_relative 'apiculture/action'
require_relative 'apiculture/sinatra_instance_methods'
require_relative 'apiculture/action_definition'
Expand All @@ -16,6 +15,12 @@ def self.extended(in_class)
super
end

class Void
def <<(_item); end
def map; []; end
def select; []; end
end

IDENTITY_PROC = ->(arg) { arg }

AC_APPLY_TYPECAST_PROC = ->(cast_proc_or_method, v) {
Expand Down Expand Up @@ -277,7 +282,16 @@ def api_method(http_verb, path, options={}, &blk)
end

def apiculture_stack
@apiculture_actions_and_docs ||= []
if environment == "development"
@apiculture_actions_and_docs ||= []
else
@apiculture_actions_and_docs ||= Void.new
end
@apiculture_actions_and_docs
end

# Based on the RACK_ENV it will generate documentation or not
def environment
@environment ||= ENV.fetch("RACK_ENV", "development")
end
end
16 changes: 8 additions & 8 deletions lib/apiculture/app_documentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@ class TaggedMarkdown < Struct.new(:string, :section_class)
def to_markdown
string.to_markdown.to_s rescue string.to_s
end

def to_html
'<section class="%s">%s</section>' % [Rack::Utils.escape_html(section_class), render_markdown(to_markdown)]
end

def render_markdown(s)
GitHub::Markup.render('section.markdown', s.to_s)
end
end

def initialize(app, mountpoint, action_definitions_and_markdown_segments)
@app_title = app.to_s
@mountpoint = mountpoint
@chunks = action_definitions_and_markdown_segments
end

# Generates a Markdown string that contains the entire API documentation
def to_markdown
(['## %s' % @app_title] + to_markdown_slices).join("\n\n")
end
end

def to_openapi
OpenApiDocumentation::Base.new(@app_title, @mountpoint, @chunks)
end

# Generates an HTML fragment string that can be included into another HTML document
def to_html_fragment
to_markdown_slices.map do |tagged_markdown|
tagged_markdown.to_html
end.join("\n\n")
end

def to_markdown_slices
markdown_slices = @chunks.map do | action_def_or_doc |
if action_def_or_doc.respond_to?(:http_verb) # ActionDefinition
Expand All @@ -48,7 +48,7 @@ def to_markdown_slices
end
end
end

# Generates a complete HTML document string that can be saved into a file
def to_html
require 'mustache'
Expand Down
2 changes: 1 addition & 1 deletion lib/apiculture/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Apiculture
VERSION = '0.2.1'.freeze
VERSION = '0.2.2'.freeze
end
47 changes: 47 additions & 0 deletions spec/apiculture/app_documentation_prod_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require_relative '../spec_helper'

describe "Apiculture.api_documentation in prod environments" do
let(:app) do
Class.new(Apiculture::App) do
extend Apiculture

set_environment "production"

markdown_string 'This API is very important. Because it has to do with pancakes.'

documentation_build_time!

desc 'Check the pancake status'
route_param :id, 'Pancake ID to check status on'
responds_with 200, 'When the pancake is found', { status: 'Baking' }
responds_with 404, 'When no such pancake exists', { status: 'No such pancake' }
api_method :get, '/pancake/:id' do
end

desc 'Throw away the pancake'
route_param :id, 'Pancake ID to delete'
api_method :delete, '/pancake/:id' do
end

desc 'Pancake ingredients are in the URL'
route_param :topping_id, 'Pancake topping ID', Integer, cast: :to_i
api_method :get, '/pancake/with/:topping_id' do |topping_id|
end
end
end

it 'does not generate any html in non-dev environments' do
docco = app.api_documentation
generated_html = docco.to_html_fragment

expect(generated_html).to eq("")
end

# It still generates some small bits of Markdown but not a lot
it 'does not generate app documentation in Markdown' do
docco = app.api_documentation
generated_markdown = docco.to_markdown

expect(generated_markdown.length).to eq(30)
end
end
7 changes: 6 additions & 1 deletion lib/apiculture/app.rb → spec/support/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Apiculture::App

class << self
def use(middlreware_factory, middleware_options, &middleware_blk)
def use(middleware_factory, middleware_options, &middleware_blk)
@middleware_configurations ||= []
@middleware_configurations << [middleware_factory, middleware_options, middleware_blk]
end
Expand All @@ -12,6 +12,11 @@ def middleware_configurations
@middleware_configurations || []
end

# For testing only
def set_environment(env)
@environment ||= env
end

def get(url, **options, &handler_blk)
define_action :get, url, **options, &handler_blk
end
Expand Down