Skip to content

Commit b77e510

Browse files
committed
Merge pull request Apipie#239 from arathunku/feature/start-recording-examples-from-tests
New configuration option to always record examples.
2 parents 4b65fea + c908d20 commit b77e510

File tree

5 files changed

+51
-36
lines changed

5 files changed

+51
-36
lines changed

README.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ show_all_examples
554554

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

559559
Example:
560560

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

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

945945
.. code::
946946
@@ -962,7 +962,7 @@ Examples Recording
962962

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

967967
.. code::
968968

lib/apipie/apipie_module.rb

+3
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,7 @@ def self.api_base_url_version_valid?(version)
5959
version && self.configuration.api_base_url.has_key?(version)
6060
end
6161

62+
def self.record(record)
63+
Apipie::Extractor.start record
64+
end
6265
end

lib/apipie/configuration.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Configuration
66
:default_version, :debug, :version_in_url, :namespaced_resources,
77
:validate, :validate_value, :validate_presence, :authenticate, :doc_path,
88
:show_all_examples, :process_params, :update_checksum, :checksum_path,
9-
:link_extension
9+
:link_extension, :record
1010

1111
alias_method :validate?, :validate
1212
alias_method :required_by_default?, :required_by_default
@@ -141,6 +141,7 @@ def initialize
141141
@checksum_path = [@doc_base_url, '/api/']
142142
@update_checksum = false
143143
@link_extension = ".html"
144+
@record = false
144145
end
145146
end
146147
end

lib/apipie/extractor.rb

+35-26
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
require 'apipie/extractor/collector'
88

99
class Apipie::Railtie
10-
if ENV["APIPIE_RECORD"]
11-
initializer 'apipie.extractor' do |app|
12-
ActiveSupport.on_load :action_controller do
13-
before_filter do |controller|
10+
initializer 'apipie.extractor' do |app|
11+
ActiveSupport.on_load :action_controller do
12+
before_filter do |controller|
13+
if Apipie.configuration.record
1414
Apipie::Extractor.call_recorder.analyse_controller(controller)
1515
end
1616
end
17-
app.middleware.use ::Apipie::Extractor::Recorder::Middleware
18-
ActionController::TestCase::Behavior.instance_eval do
19-
include Apipie::Extractor::Recorder::FunctionalTestRecording
20-
end
17+
end
18+
app.middleware.use ::Apipie::Extractor::Recorder::Middleware
19+
ActionController::TestCase::Behavior.instance_eval do
20+
include Apipie::Extractor::Recorder::FunctionalTestRecording
2121
end
2222
end
2323
end
@@ -28,6 +28,29 @@ module Extractor
2828

2929
class << self
3030

31+
def start(record)
32+
Apipie.configuration.record = record
33+
Apipie.configuration.force_dsl = true
34+
end
35+
36+
def finish
37+
record_params, record_examples = false, false
38+
case Apipie.configuration.record
39+
when "params" then record_params = true
40+
when "examples" then record_examples = true
41+
when "all" then record_params = true, record_examples = true
42+
end
43+
44+
if record_examples
45+
puts "Writing examples to a file"
46+
write_examples
47+
end
48+
if record_params
49+
puts "Updating auto-generated documentation"
50+
write_docs
51+
end
52+
end
53+
3154
def logger
3255
Rails.logger
3356
end
@@ -144,28 +167,14 @@ def update_api_descriptions
144167
end
145168
end
146169
end
147-
148170
end
149171
end
150172
end
151173

152174
if ENV["APIPIE_RECORD"]
153-
Apipie.configuration.force_dsl = true
154-
at_exit do
155-
record_params, record_examples = false, false
156-
case ENV["APIPIE_RECORD"]
157-
when "params" then record_params = true
158-
when "examples" then record_examples = true
159-
when "all" then record_params = true, record_examples = true
160-
end
175+
Apipie::Extractor.start ENV["APIPIE_RECORD"]
176+
end
161177

162-
if record_examples
163-
puts "Writing examples to a file"
164-
Apipie::Extractor.write_examples
165-
end
166-
if record_params
167-
puts "Updating auto-generated documentation"
168-
Apipie::Extractor.write_docs
169-
end
170-
end
178+
at_exit do
179+
Apipie::Extractor.finish
171180
end

lib/apipie/extractor/recorder.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ def initialize(app)
8989
end
9090

9191
def call(env)
92-
analyze(env) do
93-
@app.call(env)
92+
if Apipie.configuration.record
93+
analyze(env) do
94+
@app.call(env)
95+
end
9496
end
9597
end
9698

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

113115
def process_with_api_recording(*args) # action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
114116
ret = process_without_api_recording(*args)
115-
Apipie::Extractor.call_recorder.analyze_functional_test(self)
116-
Apipie::Extractor.call_finished
117+
if Apipie.configuration.record
118+
Apipie::Extractor.call_recorder.analyze_functional_test(self)
119+
Apipie::Extractor.call_finished
120+
end
117121
ret
118122
ensure
119123
Apipie::Extractor.clean_call_recorder
120124
end
121125
end
122-
123126
end
124-
125127
end
126128
end

0 commit comments

Comments
 (0)