Class: Interpol::StubApp::Builder
- Inherits:
-
Object
- Object
- Interpol::StubApp::Builder
- Defined in:
- lib/interpol/stub_app.rb
Overview
Private: Builds a stub sinatra app for the given interpol configuration.
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #build ⇒ Object
- #endpoint_def_for(endpoint, app) ⇒ Object
- #endpoint_definition(endpoint) ⇒ Object
- #example_for(endpoint_def, app) ⇒ Object
-
#initialize(config) ⇒ Builder
constructor
A new instance of Builder.
Constructor Details
#initialize(config) ⇒ Builder
Returns a new instance of Builder.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/interpol/stub_app.rb', line 21 def initialize(config) builder = self @config = config @app = ::Sinatra.new do set :stub_app_builder, builder not_found { JSON.dump(:error => "The requested resource could not be found") } before { content_type "application/json;charset=utf-8" } before('/__ping') { skip_param_parsing! if respond_to?(:skip_param_parsing!) } get('/__ping') { JSON.dump(:message => "Interpol stub app running.") } enable :perform_validations def self.name "Interpol::StubApp (anonymous)" end end end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
19 20 21 |
# File 'lib/interpol/stub_app.rb', line 19 def app @app end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
19 20 21 |
# File 'lib/interpol/stub_app.rb', line 19 def config @config end |
Instance Method Details
#build ⇒ Object
39 40 41 42 43 |
# File 'lib/interpol/stub_app.rb', line 39 def build config.endpoints.each do |endpoint| app.send(endpoint.method, endpoint.route, &endpoint_definition(endpoint)) end end |
#endpoint_def_for(endpoint, app) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/interpol/stub_app.rb', line 55 def endpoint_def_for(endpoint, app) version = config.response_version_for(app.request.env, endpoint) endpoint_def = endpoint.find_definition!(version, 'response') rescue NoEndpointDefinitionFoundError config.sinatra_request_version_unavailable \ app, version, endpoint.available_response_versions end |
#endpoint_definition(endpoint) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/interpol/stub_app.rb', line 45 def endpoint_definition(endpoint) lambda do endpoint_def = settings.stub_app_builder.endpoint_def_for(endpoint, self) example = settings.stub_app_builder.example_for(endpoint_def, self) example.validate! if settings.perform_validations? status endpoint_def.example_status_code JSON.dump(example.data) end end |
#example_for(endpoint_def, app) ⇒ Object
63 64 65 66 |
# File 'lib/interpol/stub_app.rb', line 63 def example_for(endpoint_def, app) example = config.example_response_for(endpoint_def, app.request.env) example.apply_filters(config.filter_example_data_blocks, app.request.env) end |