Skip to content

Commit

Permalink
Merge pull request #239 from arathunku/feature/start-recording-exampl…
Browse files Browse the repository at this point in the history
…es-from-tests

New configuration option to always record examples.
  • Loading branch information
iNecas committed May 12, 2014
2 parents 4b65fea + c908d20 commit b77e510
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 36 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ show_all_examples

link_extension
The extension to use for API pages ('.html' by default). Link extensions
in static API docs cannot be changed from '.html'.
in static API docs cannot be changed from '.html'.

Example:

Expand Down Expand Up @@ -940,7 +940,7 @@ of information is already included in this tests, it just needs to be
extracted somehow. Luckily, Apipie provides such a feature.

When running the tests, set the ``APIPIE_RECORD=params`` environment
variable. You can either use it with functional tests
variable or call ``Apipie.record('params')`` from specs starter. You can either use it with functional tests

.. code::
Expand All @@ -962,7 +962,7 @@ Examples Recording

You can also use the tests to generate up-to-date examples for your
code. Similarly to the bootstrapping, you can use it with functional
tests or a running server, setting ``APIPIE_RECORD=examples``
tests or a running server, setting ``APIPIE_RECORD=examples`` or by calling ``Apipie.record('examples')`` in your specs starter.

.. code::
Expand Down
3 changes: 3 additions & 0 deletions lib/apipie/apipie_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ def self.api_base_url_version_valid?(version)
version && self.configuration.api_base_url.has_key?(version)
end

def self.record(record)
Apipie::Extractor.start record
end
end
3 changes: 2 additions & 1 deletion lib/apipie/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Configuration
:default_version, :debug, :version_in_url, :namespaced_resources,
:validate, :validate_value, :validate_presence, :authenticate, :doc_path,
:show_all_examples, :process_params, :update_checksum, :checksum_path,
:link_extension
:link_extension, :record

alias_method :validate?, :validate
alias_method :required_by_default?, :required_by_default
Expand Down Expand Up @@ -141,6 +141,7 @@ def initialize
@checksum_path = [@doc_base_url, '/api/']
@update_checksum = false
@link_extension = ".html"
@record = false
end
end
end
61 changes: 35 additions & 26 deletions lib/apipie/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
require 'apipie/extractor/collector'

class Apipie::Railtie
if ENV["APIPIE_RECORD"]
initializer 'apipie.extractor' do |app|
ActiveSupport.on_load :action_controller do
before_filter do |controller|
initializer 'apipie.extractor' do |app|
ActiveSupport.on_load :action_controller do
before_filter do |controller|
if Apipie.configuration.record
Apipie::Extractor.call_recorder.analyse_controller(controller)
end
end
app.middleware.use ::Apipie::Extractor::Recorder::Middleware
ActionController::TestCase::Behavior.instance_eval do
include Apipie::Extractor::Recorder::FunctionalTestRecording
end
end
app.middleware.use ::Apipie::Extractor::Recorder::Middleware
ActionController::TestCase::Behavior.instance_eval do
include Apipie::Extractor::Recorder::FunctionalTestRecording
end
end
end
Expand All @@ -28,6 +28,29 @@ module Extractor

class << self

def start(record)
Apipie.configuration.record = record
Apipie.configuration.force_dsl = true
end

def finish
record_params, record_examples = false, false
case Apipie.configuration.record
when "params" then record_params = true
when "examples" then record_examples = true
when "all" then record_params = true, record_examples = true
end

if record_examples
puts "Writing examples to a file"
write_examples
end
if record_params
puts "Updating auto-generated documentation"
write_docs
end
end

def logger
Rails.logger
end
Expand Down Expand Up @@ -144,28 +167,14 @@ def update_api_descriptions
end
end
end

end
end
end

if ENV["APIPIE_RECORD"]
Apipie.configuration.force_dsl = true
at_exit do
record_params, record_examples = false, false
case ENV["APIPIE_RECORD"]
when "params" then record_params = true
when "examples" then record_examples = true
when "all" then record_params = true, record_examples = true
end
Apipie::Extractor.start ENV["APIPIE_RECORD"]
end

if record_examples
puts "Writing examples to a file"
Apipie::Extractor.write_examples
end
if record_params
puts "Updating auto-generated documentation"
Apipie::Extractor.write_docs
end
end
at_exit do
Apipie::Extractor.finish
end
14 changes: 8 additions & 6 deletions lib/apipie/extractor/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ def initialize(app)
end

def call(env)
analyze(env) do
@app.call(env)
if Apipie.configuration.record
analyze(env) do
@app.call(env)
end
end
end

Expand All @@ -112,15 +114,15 @@ def self.included(base)

def process_with_api_recording(*args) # action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
ret = process_without_api_recording(*args)
Apipie::Extractor.call_recorder.analyze_functional_test(self)
Apipie::Extractor.call_finished
if Apipie.configuration.record
Apipie::Extractor.call_recorder.analyze_functional_test(self)
Apipie::Extractor.call_finished
end
ret
ensure
Apipie::Extractor.clean_call_recorder
end
end

end

end
end

0 comments on commit b77e510

Please sign in to comment.