Class: Magnews::Contact
Class Method Summary
collapse
auth_header, common_headers
Methods included from UrlHelper
url_for
logger
Class Method Details
.create!(values, options = {}) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/magnews/contact.rb', line 8
def create!(values, options={})
normalize_values!(values)
payload = { options: options.reverse_merge({ iddatabase: Magnews.iddatabase }), values: values }.to_json
RestClient.post(url_for("contacts/subscribe"), payload, ) do |response, request, result, &block|
logger.info { response.body }
if (200..207).include? response.code
respond_to_200(response)
elsif Magnews::EXCEPTIONS_MAP[response.code].present?
raise Magnews::EXCEPTIONS_MAP[response.code].new(response, response.code)
else
response.return!(request, result, &block)
end
end
end
|
.normalize_values!(values) ⇒ Object
34
35
36
37
38
|
# File 'lib/magnews/contact.rb', line 34
def normalize_values!(values)
values.each do |key, value|
values[key] = value.join(",") if value.is_a? Array
end
end
|
.respond_to_200(response) ⇒ Object
24
25
26
27
28
29
30
31
32
|
# File 'lib/magnews/contact.rb', line 24
def respond_to_200(response)
body = JSON.parse(response.body).deep_symbolize_keys!
if body[:ok]
return true
else
response.code = 422
raise Magnews::EXCEPTIONS_MAP[response.code].new(response, response.code)
end
end
|