Class: Marfa::Helpers::HTTP::Rest
- Defined in:
- lib/marfa/helpers/http/rest.rb
Overview
Helpers to use Rest requests
Class Method Summary collapse
-
._log_body(type, code, body) ⇒ Object
logging body response of request.
-
._log_head(type, url, headers, payload = nil) ⇒ Object
logging head of request.
-
.delete(url, payload = {}, headers = {}, &block) ⇒ Object
DELETE request.
-
.get(url, headers = {}, &block) ⇒ Object
GET request.
-
.head(url, headers = {}, &block) ⇒ Object
HEAD request.
-
.post(url, payload, headers = {}, &block) ⇒ Object
POST request.
-
.put(url, payload, headers = {}, &block) ⇒ Object
PUT request.
Class Method Details
._log_body(type, code, body) ⇒ Object
logging body response of request
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/marfa/helpers/http/rest.rb', line 159 def self._log_body(type, code, body) return if $logger.nil? if code == 200 $logger.info("REST #{type} code = #{code}") $logger.debug("REST #{type} body = #{body}") else $logger.error("REST #{type} code = #{code}") $logger.error("REST #{type} body = #{body}") end end |
._log_head(type, url, headers, payload = nil) ⇒ Object
logging head of request
146 147 148 149 150 151 |
# File 'lib/marfa/helpers/http/rest.rb', line 146 def self._log_head(type, url, headers, payload = nil) return if $logger.nil? $logger.info("REST #{type} url = #{url}") $logger.info("REST #{type} headers = #{headers}") $logger.info("REST #{type} payload = #{payload}") unless payload.nil? end |
.delete(url, payload = {}, headers = {}, &block) ⇒ Object
DELETE request
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/marfa/helpers/http/rest.rb', line 119 def self.delete(url, payload = {}, headers = {}, &block) _log_head('DEL', url, headers, payload) if block.nil? response = RestClient.delete(url, headers) _log_body('DEL', response.code, response.body) response else RestClient::Request.execute( method: :delete, url: url, payload: payload, headers: headers ) do |response| _log_body('DEL', response.code, response.body) block.call(response) end end end |
.get(url, headers = {}, &block) ⇒ Object
GET request
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/marfa/helpers/http/rest.rb', line 16 def self.get(url, headers = {}, &block) _log_head('GET', url, headers) if block.nil? response = RestClient.get(url, headers) _log_body('GET', response.code, response.body) response else RestClient.get(url, headers) do |response| _log_body('GET', response.code, response.body) block.call(response) end end end |
.head(url, headers = {}, &block) ⇒ Object
HEAD request
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/marfa/helpers/http/rest.rb', line 37 def self.head(url, headers = {}, &block) _log_head('HEA', url, headers) if block.nil? response = RestClient.head(url, headers) _log_body('HEA', response.code, response.body) response else RestClient.head(url, headers) do |response| _log_body('HEA', response.code, response.body) block.call(response) end end end |
.post(url, payload, headers = {}, &block) ⇒ Object
POST request
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/marfa/helpers/http/rest.rb', line 61 def self.post (url, payload, headers={}, &block) _log_head('POS', url, headers, payload) if block.nil? response = RestClient.post(url, payload, headers) _log_body('POS', response.code, response.body) response else RestClient::Request.execute( method: :post, url: url, payload: payload, headers: headers ) do |response| _log_body('POS', response.code, response.body) block.call(response) end end end |
.put(url, payload, headers = {}, &block) ⇒ Object
PUT request
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/marfa/helpers/http/rest.rb', line 91 def self.put(url, payload, headers = {}, &block) _log_head('PUT', url, headers, payload) if block.nil? response = RestClient.put(url, payload, headers) _log_body('PUT', response.code, response.body) response else RestClient::Request.execute( method: :put, url: url, payload: payload, headers: headers ) do |response| _log_body('PUT', response.code, response.body) block.call(response) end end end |