Class: Marfa::Helpers::HTTP::Rest

Inherits:
Object
  • Object
show all
Defined in:
lib/marfa/helpers/http/rest.rb

Overview

Helpers to use Rest requests

Class Method Summary collapse

Class Method Details

._log_body(type, code, body) ⇒ Object

logging body response of request

Examples:

_log_body(type, response.code, response.body)

Parameters:

  • type (String)
    • type of request

  • code (String)
    • http code response

  • body (String)
    • response body



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

Examples:

_log_head(type, url, headers, payload)

Parameters:

  • type (String)
    • type of request

  • url (String)
    • url

  • headers (Hash)
    • headers hash

  • payload (Hash) (defaults to: nil)
    • payload



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

Examples:

Rest.delete(url, payload, headers) do |response|

Parameters:

  • url (String)
    • url

  • payload (String) (defaults to: {})
    • request body

  • headers (Hash) (defaults to: {})
    • headers hash

  • block (Proc)
    • block code to run



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

Examples:

response = Rest.get(url, headers)

Parameters:

  • url (String)
    • url

  • headers (Hash) (defaults to: {})
    • headers hash



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

Examples:

response = Rest.head(url)

Parameters:

  • url (String)
    • url

  • headers (Hash) (defaults to: {})
    • headers hash



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

Examples:

response = Rest.post(url, payload)
Rest.post(url, payload) do |response|

Parameters:

  • url (String)
    • url

  • payload (String)
    • request body

  • headers (Hash) (defaults to: {})
    • headers hash

  • block (Proc)
    • block code to run



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

Examples:

response = Rest.put(url, payload)
Rest.put(url, payload) do |response|

Parameters:

  • url (String)
    • url

  • payload (String)
    • request body

  • headers (Hash) (defaults to: {})
    • headers hash

  • block (Proc)
    • block code to run



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