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.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/interpol/stub_app.rb', line 22

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

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#buildObject



40
41
42
43
44
# File 'lib/interpol/stub_app.rb', line 40

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

#endpoint_def_for(endpoint, app) ⇒ Object



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

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



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

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



64
65
66
67
# File 'lib/interpol/stub_app.rb', line 64

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