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.



11
12
13
14
15
16
17
18
19
20
# File 'lib/anymock.rb', line 11

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

Instance Attribute Details

#addressObject

Returns the value of attribute address.



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

def address
  @address
end

#document_rootObject

Returns the value of attribute document_root.



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

def document_root
  @document_root
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#output(params) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/anymock.rb', line 46

def output(params)
  if @json
    logger.info params.to_json
  else
    logger.info '----'
    i(path method query response).each do |n|
      logger.info "#{n}:"
      logger.info params[n]
    end
    logger.info '----'
  end
end

#startObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/anymock.rb', line 22

def start
  server.mount_proc '/' do |req, res|
    output_params = {
      path: req.path,
      method: req.request_method,
      query: req.query
    }
    res.status = status_code(req.request_method)
    if @file
      File.open(@file) do |f|
        res.body = f.read
      end
    elsif @response
      res.body = @response
    elsif @mirror
      res.body = req_string
    else
      res.status = 204
    end
    output output_params.merge({ response: res.body })
  end
  server.start
end