Class: Mull::Request
- Inherits:
-
Object
- Object
- Mull::Request
- Defined in:
- lib/mull/request.rb
Instance Attribute Summary collapse
-
#file_utils ⇒ Object
dependencies.
-
#filepath ⇒ Object
Returns the value of attribute filepath.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#http ⇒ Object
dependencies.
-
#matcher ⇒ Object
Returns the value of attribute matcher.
-
#method ⇒ Object
Returns the value of attribute method.
-
#post_body ⇒ Object
Returns the value of attribute post_body.
-
#root_url ⇒ Object
Returns the value of attribute root_url.
-
#status ⇒ Object
Returns the value of attribute status.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #fully_qualified_url ⇒ Object
-
#initialize(root_url:, definition:, file_utils:, http:) ⇒ Request
constructor
A new instance of Request.
- #pretty_json_if_possible(body) ⇒ Object
- #request_options ⇒ Object
- #result ⇒ Object
- #save_response! ⇒ Object
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_utils ⇒ Object
dependencies
9 10 11 |
# File 'lib/mull/request.rb', line 9 def file_utils @file_utils end |
#filepath ⇒ Object
Returns the value of attribute filepath.
14 15 16 |
# File 'lib/mull/request.rb', line 14 def filepath @filepath end |
#headers ⇒ Object
Returns the value of attribute headers.
14 15 16 |
# File 'lib/mull/request.rb', line 14 def headers @headers end |
#http ⇒ Object
dependencies
9 10 11 |
# File 'lib/mull/request.rb', line 9 def http @http end |
#matcher ⇒ Object
Returns the value of attribute matcher.
14 15 16 |
# File 'lib/mull/request.rb', line 14 def matcher @matcher end |
#method ⇒ Object
Returns the value of attribute method.
14 15 16 |
# File 'lib/mull/request.rb', line 14 def method @method end |
#post_body ⇒ Object
Returns the value of attribute post_body.
14 15 16 |
# File 'lib/mull/request.rb', line 14 def post_body @post_body end |
#root_url ⇒ Object
Returns the value of attribute root_url.
14 15 16 |
# File 'lib/mull/request.rb', line 14 def root_url @root_url end |
#status ⇒ Object
Returns the value of attribute status.
14 15 16 |
# File 'lib/mull/request.rb', line 14 def status @status end |
#url ⇒ Object
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_url ⇒ Object
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_options ⇒ Object
57 58 59 60 61 62 |
# File 'lib/mull/request.rb', line 57 def opts = {} opts[:headers] = headers if headers.present? opts[:body] = post_body if post_body.present? opts end |
#result ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/mull/request.rb', line 64 def result @result ||= \ if .empty? http.send(method.downcase, fully_qualified_url) else http.send(method.downcase, fully_qualified_url, ) 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 |