Class: Interpol::StubApp::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/interpol/stub_app.rb

Overview

Private: Builds a stub sinatra app for the given interpol configuration.

Instance Attribute Summary collapse

Instance Method Summary collapse

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
# 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.") }

    def self.name
      "Interpol::StubApp (anonymous)"
    end
  end
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



19
20
21
# File 'lib/interpol/stub_app.rb', line 19

def app
  @app
end

#configObject (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

#buildObject



38
39
40
41
42
# File 'lib/interpol/stub_app.rb', line 38

def build
  config.endpoints.each do |endpoint|
    app.send(endpoint.method, endpoint.route, &endpoint_definition(endpoint))
  end
end

#endpoint_definition(endpoint) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/interpol/stub_app.rb', line 44

def endpoint_definition(endpoint)
  lambda do
    example, version = settings.
                       stub_app_builder.
                       example_and_version_for(endpoint, self)
    example.validate!
    status endpoint.find_example_status_code_for!(version)
    JSON.dump(example.data)
  end
end

#example_and_version_for(endpoint, app) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/interpol/stub_app.rb', line 55

def example_and_version_for(endpoint, app)
  version = config.response_version_for(app.request.env, endpoint)
  example = endpoint.find_example_for!(version, 'response')
rescue NoEndpointDefinitionFoundError
  config.sinatra_request_version_unavailable(app, version,
                                             endpoint.available_response_versions)
else
  example = example.apply_filters(config.filter_example_data_blocks, app.request.env)
  return example, version
end