Class: Anymock::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Server

Returns a new instance of Server.



10
11
12
13
14
15
16
# File 'lib/anymock.rb', line 10

def initialize(opts = {})
  @document_root = opts[:document_root]
  @address = opts[:address]
  @port = opts[:port]
  @response = opts[:response]
  @mirror = opts[:mirror]
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



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

def address
  @address
end

#document_rootObject

Returns the value of attribute document_root.



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

def document_root
  @document_root
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#output(req_string, body) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/anymock.rb', line 34

def output(req_string, body)
  puts "request:"
  puts "----"
  puts req_string
  puts "----"
  puts "response:"
  puts "----"
  puts body
  puts "----"
end

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/anymock.rb', line 18

def start
  server.mount_proc '/' do |req, res|
    req_string = { req.request_method => req.query }.to_json
    res.status = status_code(req.request_method)
    if @response
      res.body = @response
    elsif @mirror
      res.body = req_string
    else
      res.status = 204
    end
    output req_string, res.body
  end
  server.start
end