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)
    • request body

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

  • block (Proc)
    • block code to run



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/marfa/helpers/http/rest.rb', line 127

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

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

    block.call(response)
  end
end

.get(url, headers = {}) ⇒ 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
# File 'lib/marfa/helpers/http/rest.rb', line 16

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

  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

  response
end

.head(url, headers = {}) ⇒ Object

HEAD request

Examples:

response = Rest.head(url)

Parameters:

  • url (String)
    • url

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



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/marfa/helpers/http/rest.rb', line 34

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

  response = RestClient.head(url, headers)

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

  response
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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/marfa/helpers/http/rest.rb', line 55

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/marfa/helpers/http/rest.rb', line 92

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