Class: Mocapi::MockResponse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ MockResponse

Returns a new instance of MockResponse.



17
18
19
# File 'lib/mocapi.rb', line 17

def initialize(app = nil)
  @app = app
end

Class Method Details

.load_response_map(response_map_yaml) ⇒ Object



8
9
10
# File 'lib/mocapi.rb', line 8

def load_response_map(response_map_yaml)
  @response_map = YAML.load(File.read(response_map_yaml))
end

.response_mapObject



12
13
14
# File 'lib/mocapi.rb', line 12

def response_map
  @response_map || {}
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  request = Rack::Request.new(env)

  response = if @app
    @app.call(env)
  else
    [200, {}, ['']]
  end

  if mock_response = response_map[request.path]
    [
      mock_response['status_code'],
      mock_response['headers'],
      mock_response['body']
    ]
  else
    response
  end
end