my_target_api 1.1.1 → 1.2.0
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 +81 -0
- data/README.md +30 -3
- data/lib/my_target_api/connection_error.rb +3 -0
- data/lib/my_target_api/request.rb +10 -12
- data/lib/my_target_api/request_error.rb +3 -0
- data/lib/my_target_api/resource.rb +7 -8
- data/lib/my_target_api/version.rb +1 -1
- data/spec/resource_spec.rb +10 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d50d6a3938e19d3ecea148ee213ceb7c2a98503
|
4
|
+
data.tar.gz: 2949b7a73006c2f56b86f37b8389ff99d84300cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa92d11653187e0c86863d8744d99e47fdbe6a65f5bbca65007c1436dff1ad6a9f1b8db4e59599952f4c23542e58a285d0633c8b485e8c72df6f5fa33ab4aa3f
|
7
|
+
data.tar.gz: b075284f16d5d546b7e7e8b389f2e4d60c08dcba81ef7a8901681f589f0fea7f81f44601ac5bc37ec6c6ce119e46cde578c10f061430e12dee92222d1fb1bcc6
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
my_target_api (1.2.0)
|
5
|
+
json (~> 2.0, >= 2.0.0)
|
6
|
+
rest-client (~> 2.0, >= 2.0.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
addressable (2.6.0)
|
12
|
+
public_suffix (>= 2.0.2, < 4.0)
|
13
|
+
ast (2.4.0)
|
14
|
+
crack (0.4.3)
|
15
|
+
safe_yaml (~> 1.0.0)
|
16
|
+
diff-lcs (1.3)
|
17
|
+
domain_name (0.5.20180417)
|
18
|
+
unf (>= 0.0.5, < 1.0.0)
|
19
|
+
hashdiff (0.3.8)
|
20
|
+
http-cookie (1.0.3)
|
21
|
+
domain_name (~> 0.5)
|
22
|
+
json (2.1.0)
|
23
|
+
mime-types (3.2.2)
|
24
|
+
mime-types-data (~> 3.2015)
|
25
|
+
mime-types-data (3.2018.0812)
|
26
|
+
netrc (0.11.0)
|
27
|
+
parallel (1.13.0)
|
28
|
+
parser (2.6.0.0)
|
29
|
+
ast (~> 2.4.0)
|
30
|
+
powerpack (0.1.2)
|
31
|
+
public_suffix (3.0.3)
|
32
|
+
rainbow (3.0.0)
|
33
|
+
rake (12.3.2)
|
34
|
+
rest-client (2.0.2)
|
35
|
+
http-cookie (>= 1.0.2, < 2.0)
|
36
|
+
mime-types (>= 1.16, < 4.0)
|
37
|
+
netrc (~> 0.8)
|
38
|
+
rspec (3.7.0)
|
39
|
+
rspec-core (~> 3.7.0)
|
40
|
+
rspec-expectations (~> 3.7.0)
|
41
|
+
rspec-mocks (~> 3.7.0)
|
42
|
+
rspec-core (3.7.1)
|
43
|
+
rspec-support (~> 3.7.0)
|
44
|
+
rspec-expectations (3.7.0)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.7.0)
|
47
|
+
rspec-mocks (3.7.0)
|
48
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
49
|
+
rspec-support (~> 3.7.0)
|
50
|
+
rspec-support (3.7.1)
|
51
|
+
rubocop (0.54.0)
|
52
|
+
parallel (~> 1.10)
|
53
|
+
parser (>= 2.5)
|
54
|
+
powerpack (~> 0.1)
|
55
|
+
rainbow (>= 2.2.2, < 4.0)
|
56
|
+
ruby-progressbar (~> 1.7)
|
57
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
58
|
+
ruby-progressbar (1.10.0)
|
59
|
+
safe_yaml (1.0.4)
|
60
|
+
unf (0.1.4)
|
61
|
+
unf_ext
|
62
|
+
unf_ext (0.0.7.5)
|
63
|
+
unicode-display_width (1.4.1)
|
64
|
+
webmock (2.3.2)
|
65
|
+
addressable (>= 2.3.6)
|
66
|
+
crack (>= 0.3.2)
|
67
|
+
hashdiff
|
68
|
+
|
69
|
+
PLATFORMS
|
70
|
+
ruby
|
71
|
+
|
72
|
+
DEPENDENCIES
|
73
|
+
bundler (~> 1.6)
|
74
|
+
my_target_api!
|
75
|
+
rake (~> 12.3.0, >= 12.3.0)
|
76
|
+
rspec (~> 3.7.0, >= 3.7.0)
|
77
|
+
rubocop (~> 0.54.0)
|
78
|
+
webmock (~> 2.3.2, >= 2.3.2)
|
79
|
+
|
80
|
+
BUNDLED WITH
|
81
|
+
1.16.1
|
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.
|
10
|
+
gem 'my_target_api', '~> 1.2.0'
|
11
11
|
```
|
12
12
|
|
13
13
|
Or install from command line:
|
@@ -28,15 +28,22 @@ my_target_api = MyTargetApi.new(access_token)
|
|
28
28
|
### Resources
|
29
29
|
|
30
30
|
```ruby
|
31
|
-
#
|
31
|
+
# Root resources
|
32
32
|
campaigns_resource = my_target_api.resource('campaigns')
|
33
33
|
remarketing_resource = my_target_api.resource('remarketing', v: 2)
|
34
34
|
|
35
|
-
#
|
35
|
+
# Nested resources
|
36
36
|
remarketing_counters_resource = my_target_api.resource('remarketing/counters', v: 2)
|
37
37
|
remarketing_counters_resource = remarketing_resource.resource('counters')
|
38
38
|
```
|
39
39
|
|
40
|
+
#### Options
|
41
|
+
|
42
|
+
Name | Default value | Description
|
43
|
+
---|---|---
|
44
|
+
`:v` | 1 | API version
|
45
|
+
`:logger` | | An object to log requests and exceptions. The object must respond to `<<` method
|
46
|
+
|
40
47
|
### Create, Read, Update, Delete
|
41
48
|
|
42
49
|
```ruby
|
@@ -51,6 +58,19 @@ campaigns_resource.update(id: 12345, status: 'blocked') # => [{ 'id' => 12345, '
|
|
51
58
|
remarketing_counters_resource.delete(id: 343434) # => [{ 'success' => true }]
|
52
59
|
```
|
53
60
|
|
61
|
+
#### Options
|
62
|
+
|
63
|
+
Name | Default value | Description
|
64
|
+
---|---|---
|
65
|
+
`:id` | | Resource ID. Optional for Read, required for Update and Delete
|
66
|
+
`:id_param_key` | `:id` | Option key for resource ID
|
67
|
+
|
68
|
+
### Changing default ID param name
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
campaigns_resource.read(id_key_param: :campaign_id, campaign_id: 12345) # => [{ 'id' => 12345, ... }]
|
72
|
+
```
|
73
|
+
|
54
74
|
### File upload
|
55
75
|
|
56
76
|
```ruby
|
@@ -79,10 +99,17 @@ def read_active_campaigns
|
|
79
99
|
rescue MyTargetApi::RequestError, MyTargetApi::ConnectionError => e
|
80
100
|
|
81
101
|
puts e.message, e.backtrace
|
102
|
+
# You can access the original exception
|
103
|
+
puts e.original_exception.message, e.original_exception.backtrace
|
82
104
|
|
83
105
|
end
|
84
106
|
```
|
85
107
|
|
108
|
+
Name | Description
|
109
|
+
---|---
|
110
|
+
`MyTargetApi::RequestError` | Request didn't succeed
|
111
|
+
`MyTargetApi::ConnectionError` | Connection didn't succeed
|
112
|
+
|
86
113
|
## Testing
|
87
114
|
|
88
115
|
```
|
@@ -94,14 +94,14 @@ class MyTargetApi
|
|
94
94
|
|
95
95
|
def with_exception_handling
|
96
96
|
response = yield
|
97
|
-
|
97
|
+
log(response)
|
98
98
|
response
|
99
|
-
rescue RestClient::
|
100
|
-
RestClient::BadRequest, RestClient::RequestFailed,
|
101
|
-
RestClient::ResourceNotFound => e
|
102
|
-
|
99
|
+
rescue RestClient::ExceptionWithResponse => e
|
103
100
|
log_rest_client_exception(e)
|
104
101
|
raise MyTargetApi::RequestError, e
|
102
|
+
rescue RestClient::Exception => e
|
103
|
+
log("#{e.class.name} #{e.message}")
|
104
|
+
raise MyTargetApi::RequestError, e
|
105
105
|
rescue SocketError => e
|
106
106
|
raise MyTargetApi::ConnectionError, e
|
107
107
|
end
|
@@ -117,19 +117,17 @@ class MyTargetApi
|
|
117
117
|
HTTP headers: #{exception.http_headers}
|
118
118
|
LOG
|
119
119
|
|
120
|
-
|
120
|
+
log(log_message)
|
121
121
|
end
|
122
122
|
|
123
123
|
def log_hash(hash)
|
124
|
-
|
125
|
-
|
126
|
-
logger << hash.map do |key, value|
|
124
|
+
log(hash.map do |key, value|
|
127
125
|
"#{key}: #{value.is_a?(String) ? value : value.inspect}"
|
128
|
-
end.join("\n")
|
126
|
+
end.join("\n"))
|
129
127
|
end
|
130
128
|
|
131
|
-
def
|
132
|
-
options[:logger]
|
129
|
+
def log(message)
|
130
|
+
options[:logger] << message if options[:logger]
|
133
131
|
end
|
134
132
|
|
135
133
|
end
|
@@ -16,9 +16,7 @@ class MyTargetApi
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def read(params = {})
|
19
|
-
|
20
|
-
id = prepared.delete(:id)
|
21
|
-
|
19
|
+
with_id_and_prepared_params(params) do |id, prepared|
|
22
20
|
if id
|
23
21
|
api.get_request("#{path}/#{id}.json", prepared)
|
24
22
|
else
|
@@ -28,13 +26,13 @@ class MyTargetApi
|
|
28
26
|
end
|
29
27
|
|
30
28
|
def update(params = {})
|
31
|
-
with_id_and_prepared_params(params) do |id, prepared|
|
29
|
+
with_id_and_prepared_params(params, assert_id_existence: true) do |id, prepared|
|
32
30
|
api.post_request("#{path}/#{id}.json", prepared)
|
33
31
|
end
|
34
32
|
end
|
35
33
|
|
36
34
|
def delete(params = {})
|
37
|
-
with_id_and_prepared_params(params) do |id, prepared|
|
35
|
+
with_id_and_prepared_params(params, assert_id_existence: true) do |id, prepared|
|
38
36
|
api.delete_request("#{path}/#{id}.json", prepared)
|
39
37
|
end
|
40
38
|
end
|
@@ -63,10 +61,11 @@ class MyTargetApi
|
|
63
61
|
yield prepared
|
64
62
|
end
|
65
63
|
|
66
|
-
def with_id_and_prepared_params(params)
|
64
|
+
def with_id_and_prepared_params(params, assert_id_existence: false)
|
67
65
|
with_prepared_params(params) do |prepared|
|
68
|
-
|
69
|
-
|
66
|
+
id_key_param = prepared.delete(:id_param_key) || :id
|
67
|
+
id = prepared.delete(id_key_param.to_sym)
|
68
|
+
raise ArgumentError, ':id is required' if assert_id_existence && id.nil?
|
70
69
|
|
71
70
|
yield id, prepared.dup
|
72
71
|
end
|
data/spec/resource_spec.rb
CHANGED
@@ -29,9 +29,16 @@ describe MyTargetApi::Resource do
|
|
29
29
|
resource.read(param: 'param')
|
30
30
|
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
describe 'request path to a single object' do
|
33
|
+
it 'request path to a single object with default id key' do
|
34
|
+
expect(api).to receive(:get_request).with('api_path/7.json', param: 'param')
|
35
|
+
resource.read(id: 7, param: 'param')
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'request path to a single object with custom id key' do
|
39
|
+
expect(api).to receive(:get_request).with('api_path/8.json', param: 'param')
|
40
|
+
resource.read(id_param_key: :custom_id, custom_id: 8, param: 'param')
|
41
|
+
end
|
35
42
|
end
|
36
43
|
end
|
37
44
|
|
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.
|
4
|
+
version: 1.2.0
|
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: 2019-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -149,6 +149,7 @@ extensions: []
|
|
149
149
|
extra_rdoc_files: []
|
150
150
|
files:
|
151
151
|
- Gemfile
|
152
|
+
- Gemfile.lock
|
152
153
|
- LICENSE.txt
|
153
154
|
- README.md
|
154
155
|
- lib/my_target_api.rb
|