Class: Webspicy::Web::Mocker

Inherits:
Object
  • Object
show all
Defined in:
lib/webspicy/web/mocker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Mocker

Returns a new instance of Mocker.



7
8
9
10
# File 'lib/webspicy/web/mocker.rb', line 7

def initialize(config)
  @config = Configuration.dress(config)
  @generator = config.generator || Finitio::Generation.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/webspicy/web/mocker.rb', line 11

def config
  @config
end

#generatorObject (readonly)

Returns the value of attribute generator.



11
12
13
# File 'lib/webspicy/web/mocker.rb', line 11

def generator
  @generator
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/webspicy/web/mocker.rb', line 13

def call(env)
  req = Rack::Request.new(env)
  path = req.path
  meth = req.request_method
  if meth == "OPTIONS" && has_service?(path)
    [204, {}, []]
  elsif service = find_service(meth, path)
    status = best_status_code(service)
    body = status == 204 ? "" : random_body(service, req)
    headers = generate_headers(service)
    [ status, headers, [ body ].compact ]
  else
    [404, {}, []]
  end
end