Class: Epages::REST::Request
- Inherits:
-
Object
- Object
- Epages::REST::Request
show all
- Includes:
- Utils
- Defined in:
- lib/epages/rest/request.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
build_shop_from, camelize_keys, camelize_words, options_to_multipart_request, options_to_patch_request, parse_attribute_as, parse_attribute_as_array_of, parse_attributes, symbolize_keys!, to_query_options, underscorize_keys
Constructor Details
#initialize(object, request_method, path, options = {}) ⇒ Request
Returns a new instance of Request.
11
12
13
14
15
16
|
# File 'lib/epages/rest/request.rb', line 11
def initialize(object, request_method, path, options = {})
@shop = build_shop_from(object)
@uri = URI.parse("#{@shop.protocol}://#{@shop.host}/rs/shops/#{@shop.name.to_s + path}")
@path = uri.path
set_request_options(request_method, options)
end
|
Instance Attribute Details
Returns the value of attribute headers.
9
10
11
|
# File 'lib/epages/rest/request.rb', line 9
def
@headers
end
|
#options ⇒ Object
Returns the value of attribute options.
9
10
11
|
# File 'lib/epages/rest/request.rb', line 9
def options
@options
end
|
#path ⇒ Object
Returns the value of attribute path.
9
10
11
|
# File 'lib/epages/rest/request.rb', line 9
def path
@path
end
|
#request_method ⇒ Object
Returns the value of attribute request_method.
9
10
11
|
# File 'lib/epages/rest/request.rb', line 9
def request_method
@request_method
end
|
#shop ⇒ Object
Returns the value of attribute shop.
9
10
11
|
# File 'lib/epages/rest/request.rb', line 9
def shop
@shop
end
|
#uri ⇒ Object
Returns the value of attribute uri.
9
10
11
|
# File 'lib/epages/rest/request.rb', line 9
def uri
@uri
end
|
Instance Method Details
#auth_token ⇒ Object
26
27
28
|
# File 'lib/epages/rest/request.rb', line 26
def auth_token
"Bearer #{@shop.token}"
end
|
#content_type_options ⇒ Object
59
60
61
62
|
# File 'lib/epages/rest/request.rb', line 59
def content_type_options
return 'application/x-www-form-urlencoded' if @status_webhook
@request_method == :patch ? 'application/json-patch+json' : 'application/json'
end
|
#edit_webhook?(path) ⇒ Boolean
95
96
97
|
# File 'lib/epages/rest/request.rb', line 95
def edit_webhook?(path)
(path.to_s.include? "/webhooks/") && (@request_method == :post)
end
|
#fail_or_return_response_body(response) ⇒ Object
82
83
84
85
86
87
88
89
|
# File 'lib/epages/rest/request.rb', line 82
def fail_or_return_response_body(response)
return if response.code == 204
if response.code.between?(200, 206)
symbolize_keys!(response.parse)
else
fail Epages::Error::ERRORS[response.code], response.body.to_s
end
end
|
64
65
66
67
68
69
70
71
|
# File 'lib/epages/rest/request.rb', line 64
def format_options(options)
case @request_method
when :multipart_post then options_to_multipart_request(options)
when :patch then options_to_patch_request(options).to_json
else options.is_a?(Hash) ? camelize_keys(options) : options
end
end
|
#mime_type(basename) ⇒ Object
73
74
75
76
77
78
79
80
|
# File 'lib/epages/rest/request.rb', line 73
def mime_type(basename)
case basename
when /.gif/ then 'image/gif'
when /.jpe?g/ then 'image/jpeg'
when /.png/ then 'image/png'
else 'application/octet-stream'
end
end
|
#new_webhook?(path) ⇒ Boolean
91
92
93
|
# File 'lib/epages/rest/request.rb', line 91
def new_webhook?(path)
(path.to_s == "/webhooks") && (@request_method == :post)
end
|
#options_passed_by ⇒ Object
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/epages/rest/request.rb', line 48
def options_passed_by
return :body if @is_a_new_webhook
return :form if @status_webhook
return :form if (@request_method == :post && @options.key?(:image))
case @request_method
when :get then :params
when :patch then :body
else :json
end
end
|
31
32
33
34
35
36
37
|
# File 'lib/epages/rest/request.rb', line 31
def perform
response = HTTP.(accept: @headers['Accept'],
authorization: @headers['Authorization'],
content_type: @headers['Content-Type'],
user_agent: @headers['User-Agent']).public_send(@request_method, @uri.to_s, options_passed_by => @options)
fail_or_return_response_body(response)
end
|
39
40
41
42
43
44
45
46
|
# File 'lib/epages/rest/request.rb', line 39
def
= {}
['Content-Type'] = content_type_options unless @request_method == :multipart_post
['Accept'] = '*/*'
['Authorization'] = auth_token if @shop.token?
['User-Agent'] = 'ruby gem'
end
|
#set_request_options(method, options) ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/epages/rest/request.rb', line 18
def set_request_options(method, options)
@request_method = method
@is_a_new_webhook = new_webhook?(path)
@status_webhook = edit_webhook?(path)
@headers =
@options = format_options(options)
end
|