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

.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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/marfa/helpers/http/rest.rb', line 145

def self.delete(url, payload = {}, headers = {}, &block)
  p "REST DEL url    = #{url}" if Marfa.config.logging_level > 0
  if Marfa.config.logging_level > 0
    p "REST DEL headers= #{headers}"
    p "REST DEL payload= #{payload}"
  end

  if block.nil?
    response = RestClient.delete(url, headers)

    p "REST DEL code  = #{response.code}" if Marfa.config.logging_level > 0
    p response.body if Marfa.config.logging_level > 1

    return response
  else
    RestClient::Request.execute(
        method: :delete,
        url: url,
        payload: payload,
        headers: headers
    ) do |response|
      p "REST DEL code   = #{response.code}" if Marfa.config.logging_level > 0
      p response.body if Marfa.config.logging_level > 1

      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
30
31
32
33
34
35
# File 'lib/marfa/helpers/http/rest.rb', line 16

def self.get(url, headers = {}, &block)
  p "REST GET url = #{url}" if Marfa.config.logging_level > 0
  p "REST GET headers = #{headers}" if Marfa.config.logging_level > 0

  if block.nil?
    response = RestClient.get(url, headers)

    p "REST GET code = #{response.code}" if Marfa.config.logging_level > 0
    p response.body if Marfa.config.logging_level > 1

    return response
  else
    RestClient.get(url, headers) do |response|
      p "REST GET code = #{response.code}" if Marfa.config.logging_level > 0
      p response.body if Marfa.config.logging_level > 1

      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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/marfa/helpers/http/rest.rb', line 43

def self.head(url, headers = {}, &block)
  p "REST HEA url = #{url}" if Marfa.config.logging_level > 0
  p "REST HEA headers = #{headers}" if Marfa.config.logging_level > 0

  if block.nil?
    response = RestClient.head(url, headers)

    p "REST HEA code  = #{response.code}" if Marfa.config.logging_level > 0
    p response.body if Marfa.config.logging_level > 1

    return response
  else
    RestClient.head(url, headers) do |response|
      p "REST HEA code  = #{response.code}" if Marfa.config.logging_level > 0
      p response.body if Marfa.config.logging_level > 1

      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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/marfa/helpers/http/rest.rb', line 73

def self.post (url, payload, headers={}, &block)
  p "REST POST url = #{url}" if Marfa.config.logging_level > 0

  if Marfa.config.logging_level > 0
    p "REST POST headers= #{headers}"
    p "REST POST payload= #{payload}"
  end

  if block.nil?
    response = RestClient.post(url, payload, headers)
    p "REST POST code = #{response.code}" if Marfa.config.logging_level > 0
    p response.body if Marfa.config.logging_level > 1
    return response
  else
    RestClient::Request.execute(
      method: :post,
      url: url,
      payload: payload,
      headers: headers
    ) do |response|
      p "REST POST code  = #{response.code}" if Marfa.config.logging_level > 0
      p response.body if Marfa.config.logging_level > 1

      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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/marfa/helpers/http/rest.rb', line 110

def self.put(url, payload, headers = {}, &block)
  p "REST PUT url  = #{url}" if Marfa.config.logging_level > 0
  if Marfa.config.logging_level > 0
    p "REST PUT headers= #{headers}"
    p "REST PUT payload= #{payload}"
  end

  if block.nil?
    response = RestClient.put(url, payload, headers)
    p "REST PUT code   = #{response.code}" if Marfa.config.logging_level > 0
    p response.body if Marfa.config.logging_level > 1
    return response
  else
    RestClient::Request.execute(
      method: :put,
      url: url,
      payload: payload,
      headers: headers
    ) do |response|
      p "REST PUT code   = #{response.code}" if Marfa.config.logging_level > 0
      p response.body if Marfa.config.logging_level > 1

      block.call(response)
    end
  end
end