Class: Webspicy::Mocker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Mocker

Returns a new instance of Mocker.



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

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.



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

def config
  @config
end

#generatorObject (readonly)

Returns the value of attribute generator.



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

def generator
  @generator
end

Instance Method Details

#call(env) ⇒ Object



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

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