Class: Mull::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_url:, definition:, file_utils:, http:) ⇒ Request

Returns a new instance of Request.



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

def initialize(root_url:, definition:, file_utils:, http:)
  self.file_utils = file_utils
  self.http       = http
  self.root_url   = root_url

  defaults = {
    'method' => 'GET',
    'post_body' => {},
    'headers' => {},
    'status' => 200,
  }

  definition = defaults.merge(definition)

  self.filepath  = definition['file']
  self.headers   = definition['headers']
  self.method    = definition['method']
  self.post_body = definition['post_body']
  self.url       = definition['url']
  self.status    = definition['status']
  self.matcher   = definition['matcher'] || url
end

Instance Attribute Details

#file_utilsObject

dependencies



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

def file_utils
  @file_utils
end

#filepathObject

Returns the value of attribute filepath.



14
15
16
# File 'lib/mull/request.rb', line 14

def filepath
  @filepath
end

#headersObject

Returns the value of attribute headers.



14
15
16
# File 'lib/mull/request.rb', line 14

def headers
  @headers
end

#httpObject

dependencies



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

def http
  @http
end

#matcherObject

Returns the value of attribute matcher.



14
15
16
# File 'lib/mull/request.rb', line 14

def matcher
  @matcher
end

#methodObject

Returns the value of attribute method.



14
15
16
# File 'lib/mull/request.rb', line 14

def method
  @method
end

#post_bodyObject

Returns the value of attribute post_body.



14
15
16
# File 'lib/mull/request.rb', line 14

def post_body
  @post_body
end

#root_urlObject

Returns the value of attribute root_url.



14
15
16
# File 'lib/mull/request.rb', line 14

def root_url
  @root_url
end

#statusObject

Returns the value of attribute status.



14
15
16
# File 'lib/mull/request.rb', line 14

def status
  @status
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/mull/request.rb', line 14

def url
  @url
end

Instance Method Details

#fully_qualified_urlObject



53
54
55
# File 'lib/mull/request.rb', line 53

def fully_qualified_url
  @_fully_qualified_url ||= file_utils.join(root_url, url)
end

#pretty_json_if_possible(body) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/mull/request.rb', line 73

def pretty_json_if_possible(body)
  begin
    parsedJson = JSON.parse(body)
    return JSON.pretty_generate(parsedJson)
  rescue Exception => e
    return body
  end
end

#request_optionsObject



57
58
59
60
61
62
# File 'lib/mull/request.rb', line 57

def request_options
  opts = {}
  opts[:headers] = headers if headers.present?
  opts[:body]    = post_body if post_body.present?
  opts
end

#resultObject



64
65
66
67
68
69
70
71
# File 'lib/mull/request.rb', line 64

def result
  @result ||= \
  if request_options.empty?
    http.send(method.downcase, fully_qualified_url)
  else
    http.send(method.downcase, fully_qualified_url, request_options)
  end
end

#save_response!Object



82
83
84
85
86
87
# File 'lib/mull/request.rb', line 82

def save_response!
  return unless filepath
  return unless url
  
  file_utils.write_file(path: filepath, content: pretty_json_if_possible(result.body))
end