Class: Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition:, file_utils:, server:) ⇒ Response

Returns a new instance of Response.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mull/response.rb', line 13

def initialize(definition:, file_utils:, server:)
  self.file_utils = file_utils
  self.server     = server

  self.filepath  = definition['file']
  self.headers   = definition['matcher_headers'] || {}
  self.method    = definition['method'] || 'GET'
  self.post_body = definition['post_body'] || {}
  self.status    = definition['status'] || 200
  self.matcher   = definition['matcher'] || definition['url']

  self.response_body = definition['body'] || file_utils.read_file(path: filepath)
end

Instance Attribute Details

#file_utilsObject

Returns the value of attribute file_utils.



2
3
4
# File 'lib/mull/response.rb', line 2

def file_utils
  @file_utils
end

#filepathObject

Returns the value of attribute filepath.



2
3
4
# File 'lib/mull/response.rb', line 2

def filepath
  @filepath
end

#headersObject

Returns the value of attribute headers.



2
3
4
# File 'lib/mull/response.rb', line 2

def headers
  @headers
end

#matcherObject

Returns the value of attribute matcher.



2
3
4
# File 'lib/mull/response.rb', line 2

def matcher
  @matcher
end

#methodObject

Returns the value of attribute method.



2
3
4
# File 'lib/mull/response.rb', line 2

def method
  @method
end

#post_bodyObject

Returns the value of attribute post_body.



2
3
4
# File 'lib/mull/response.rb', line 2

def post_body
  @post_body
end

#response_bodyObject

Returns the value of attribute response_body.



2
3
4
# File 'lib/mull/response.rb', line 2

def response_body
  @response_body
end

#serverObject

Returns the value of attribute server.



2
3
4
# File 'lib/mull/response.rb', line 2

def server
  @server
end

#statusObject

Returns the value of attribute status.



2
3
4
# File 'lib/mull/response.rb', line 2

def status
  @status
end

Instance Method Details

#map_server_routeObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mull/response.rb', line 34

def map_server_route
  this = self
  server.send(method.downcase, Regexp.new(matcher)) do
    this.headers.each do |key, value|
      pass and return unless request.env["HTTP_" + key.gsub(/-/, '_').upcase] == value
    end

    this.post_body.each do |key, value|
      pass and return unless Regexp.new(value.to_s) === params[key]
    end

    status this.status
    this.response_body
  end
end