my_target_api 1.2.1 → 1.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +4 -3
- data/lib/my_target_api.rb +5 -5
- data/lib/my_target_api/request.rb +34 -22
- data/lib/my_target_api/request_error.rb +3 -1
- data/lib/my_target_api/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9f3188adcf480c8c732b25cba9d6ff3ed917e82
|
4
|
+
data.tar.gz: b952a49b44aad9f0c8cf0c581c449e06dbc02262
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0441c39f4b4dd63fc8b4ecf9542024c8575041d2fbee0654323ba5a47e34e298cb346158decb33f76499293a722bf5526cf1fb639d998ba0a9976b47b0868fe
|
7
|
+
data.tar.gz: 110e86dc4f466f507d0ec690d705f32183357ce80eba4d8f57e6f9ed560eacef549a4477bc93aad287c007302ef3a5f458c866367cd9513fbbe6de388ee5475d
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```
|
10
|
-
gem 'my_target_api', '~> 1.2.
|
10
|
+
gem 'my_target_api', '~> 1.2.6'
|
11
11
|
```
|
12
12
|
|
13
13
|
Or install from command line:
|
@@ -43,6 +43,7 @@ Name | Default value | Description
|
|
43
43
|
---|---|---
|
44
44
|
`:v` | 1 | API version
|
45
45
|
`:logger` | | An object to log requests and exceptions. The object must respond to `<<` method
|
46
|
+
`:headers` | | Headers hash to pass with request
|
46
47
|
|
47
48
|
### Create, Read, Update, Delete
|
48
49
|
|
@@ -60,7 +61,7 @@ remarketing_counters_resource.delete(id: 343434) # => [{ 'success' => true }]
|
|
60
61
|
|
61
62
|
#### Options
|
62
63
|
|
63
|
-
Name | Default value | Description
|
64
|
+
Name | Default value | Description
|
64
65
|
---|---|---
|
65
66
|
`:id` | | Resource ID. Optional for Read, required for Update and Delete
|
66
67
|
`:id_param_key` | `:id` | Option key for resource ID
|
@@ -105,7 +106,7 @@ rescue MyTargetApi::RequestError, MyTargetApi::ConnectionError => e
|
|
105
106
|
end
|
106
107
|
```
|
107
108
|
|
108
|
-
Name | Description
|
109
|
+
Name | Description
|
109
110
|
---|---
|
110
111
|
`MyTargetApi::RequestError` | Request didn't succeed
|
111
112
|
`MyTargetApi::ConnectionError` | Connection didn't succeed
|
data/lib/my_target_api.rb
CHANGED
@@ -24,19 +24,19 @@ class MyTargetApi
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def get_request(url, params)
|
27
|
-
request_object.get(url, params
|
27
|
+
request_object.get(url, params)
|
28
28
|
end
|
29
29
|
|
30
30
|
def post_request(url, params)
|
31
|
-
request_object.post(url, params
|
31
|
+
request_object.post(url, params)
|
32
32
|
end
|
33
33
|
|
34
34
|
def delete_request(url, params)
|
35
|
-
request_object.delete(url, params
|
35
|
+
request_object.delete(url, params)
|
36
36
|
end
|
37
37
|
|
38
38
|
def upload_request(url, content, params)
|
39
|
-
request_object.upload(url, content, params
|
39
|
+
request_object.upload(url, content, params)
|
40
40
|
end
|
41
41
|
|
42
42
|
private
|
@@ -44,7 +44,7 @@ class MyTargetApi
|
|
44
44
|
attr_reader :access_token, :options
|
45
45
|
|
46
46
|
def request_object
|
47
|
-
Request.new(logger: options[:logger])
|
47
|
+
Request.new(logger: options[:logger], access_token: access_token, headers: options[:headers])
|
48
48
|
end
|
49
49
|
|
50
50
|
end
|
@@ -14,8 +14,8 @@ class MyTargetApi
|
|
14
14
|
def get(url, params = {})
|
15
15
|
log_hash(method: 'Request#get', url: url, params: params)
|
16
16
|
|
17
|
-
response = with_exception_handling do
|
18
|
-
RestClient.get(url, headers
|
17
|
+
response = with_exception_handling(params) do
|
18
|
+
RestClient.get(url, headers.merge(query(params)))
|
19
19
|
end
|
20
20
|
|
21
21
|
process_response(response)
|
@@ -24,8 +24,8 @@ class MyTargetApi
|
|
24
24
|
def post(url, params = {})
|
25
25
|
log_hash(method: 'Request#post', url: url, params: params)
|
26
26
|
|
27
|
-
response = with_exception_handling do
|
28
|
-
RestClient.post(url, body_parameters(params), headers
|
27
|
+
response = with_exception_handling(params) do
|
28
|
+
RestClient.post(url, body_parameters(params), headers)
|
29
29
|
end
|
30
30
|
|
31
31
|
process_response(response)
|
@@ -34,8 +34,8 @@ class MyTargetApi
|
|
34
34
|
def delete(url, params = {})
|
35
35
|
log_hash(method: 'Request#delete', url: url, params: params)
|
36
36
|
|
37
|
-
response = with_exception_handling do
|
38
|
-
RestClient.delete(url, headers
|
37
|
+
response = with_exception_handling(params) do
|
38
|
+
RestClient.delete(url, headers.merge(query(params)))
|
39
39
|
end
|
40
40
|
|
41
41
|
process_response(response)
|
@@ -44,8 +44,10 @@ class MyTargetApi
|
|
44
44
|
def upload(url, content, params = {})
|
45
45
|
log_hash(method: 'Request#upload', url: url, params: params, content: 'no logging')
|
46
46
|
|
47
|
-
response = with_exception_handling do
|
48
|
-
RestClient.post(
|
47
|
+
response = with_exception_handling(params) do
|
48
|
+
RestClient.post(
|
49
|
+
url, content, headers.merge(query(params)).merge(content_type: 'application/octet-stream')
|
50
|
+
)
|
49
51
|
end
|
50
52
|
|
51
53
|
process_response(response)
|
@@ -57,7 +59,6 @@ class MyTargetApi
|
|
57
59
|
|
58
60
|
def body_parameters(params)
|
59
61
|
result_params = params.dup
|
60
|
-
result_params.delete(:access_token)
|
61
62
|
|
62
63
|
if result_params.values.any? { |param| param.is_a? IO } || result_params[:grant_type]
|
63
64
|
individual_body_parameters(result_params)
|
@@ -72,18 +73,15 @@ class MyTargetApi
|
|
72
73
|
end.to_h
|
73
74
|
end
|
74
75
|
|
75
|
-
def header_parameters(params)
|
76
|
-
result_params = params.dup
|
77
|
-
result_params.delete(:access_token)
|
78
|
-
result_params
|
79
|
-
end
|
80
|
-
|
81
76
|
def query(params)
|
82
|
-
{ params:
|
77
|
+
{ params: params }
|
83
78
|
end
|
84
79
|
|
85
|
-
def headers
|
86
|
-
{
|
80
|
+
def headers
|
81
|
+
{
|
82
|
+
Authorization: "Bearer #{access_token}",
|
83
|
+
**optional_headers
|
84
|
+
}
|
87
85
|
end
|
88
86
|
|
89
87
|
def process_response(response)
|
@@ -92,16 +90,16 @@ class MyTargetApi
|
|
92
90
|
response.body
|
93
91
|
end
|
94
92
|
|
95
|
-
def with_exception_handling
|
93
|
+
def with_exception_handling(params)
|
96
94
|
response = yield
|
97
95
|
log(response)
|
98
96
|
response
|
99
97
|
rescue RestClient::ExceptionWithResponse => e
|
100
98
|
log_rest_client_exception(e)
|
101
|
-
|
99
|
+
raise_with_params(e, params)
|
102
100
|
rescue RestClient::Exception => e
|
103
101
|
log("#{e.class.name} #{e.message}")
|
104
|
-
|
102
|
+
raise_with_params(e, params)
|
105
103
|
rescue SocketError => e
|
106
104
|
raise MyTargetApi::ConnectionError, e
|
107
105
|
end
|
@@ -115,11 +113,17 @@ class MyTargetApi
|
|
115
113
|
HTTP Code: #{exception.http_code}
|
116
114
|
HTTP Body: #{exception.http_body}
|
117
115
|
HTTP headers: #{exception.http_headers}
|
118
|
-
|
116
|
+
LOG
|
119
117
|
|
120
118
|
log(log_message)
|
121
119
|
end
|
122
120
|
|
121
|
+
def raise_with_params(e, params)
|
122
|
+
result = MyTargetApi::RequestError.new(e, params)
|
123
|
+
result.set_backtrace(caller)
|
124
|
+
raise result
|
125
|
+
end
|
126
|
+
|
123
127
|
def log_hash(hash)
|
124
128
|
log(hash.map do |key, value|
|
125
129
|
"#{key}: #{value.is_a?(String) ? value : value.inspect}"
|
@@ -130,5 +134,13 @@ class MyTargetApi
|
|
130
134
|
options[:logger] << message if options[:logger]
|
131
135
|
end
|
132
136
|
|
137
|
+
def access_token
|
138
|
+
options[:access_token]
|
139
|
+
end
|
140
|
+
|
141
|
+
def optional_headers
|
142
|
+
options[:headers] || {}
|
143
|
+
end
|
144
|
+
|
133
145
|
end
|
134
146
|
end
|
@@ -5,9 +5,11 @@ class MyTargetApi
|
|
5
5
|
class RequestError < StandardError
|
6
6
|
|
7
7
|
attr_reader :original_exception
|
8
|
+
attr_reader :params
|
8
9
|
|
9
|
-
def initialize(exception)
|
10
|
+
def initialize(exception, params)
|
10
11
|
@original_exception = exception
|
12
|
+
@params = params
|
11
13
|
super build_message exception
|
12
14
|
end
|
13
15
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: my_target_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OneRetarget.com
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|