my_target_api 1.0.3 → 1.0.4
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 +13 -1
- data/lib/my_target_api/{errors/connection_error.rb → connection_error.rb} +2 -2
- data/lib/my_target_api/{errors/request_error.rb → request_error.rb} +5 -5
- data/lib/my_target_api/resource.rb +3 -1
- data/lib/my_target_api/version.rb +1 -1
- data/lib/my_target_api.rb +3 -3
- data/spec/request_spec.rb +8 -0
- metadata +4 -6
- data/lib/my_target_api/auth.rb +0 -42
- data/lib/my_target_api/errors/using_error.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29d637416775f256beda271796924a188a577f31
|
4
|
+
data.tar.gz: 0764dacd1b9b341b72730ed142866c7c05b02383
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89d329c205f1792ce8173ce52af161aee3eaa12b780257ec4cf3714a7f2548ca7ac510477453d2cc6f683ff954480529be9dde9a13a81ab877f58de8c3935f47
|
7
|
+
data.tar.gz: cc342e1016adc4a9760b738081cd6b382238b32744e028db90b15156619b6c8a97cd5a2415e3d79c261698d59d69b56be5c4e786e5ce556a97ee6586b56cc79a
|
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.0.
|
10
|
+
gem 'my_target_api', '~> 1.0.4'
|
11
11
|
```
|
12
12
|
|
13
13
|
Or install from command line:
|
@@ -19,12 +19,14 @@ $ gem install my_target_api
|
|
19
19
|
## Usage
|
20
20
|
|
21
21
|
### Initialization
|
22
|
+
|
22
23
|
```ruby
|
23
24
|
# You need an access token to use API
|
24
25
|
my_target_api = MyTargetApi.new(access_token)
|
25
26
|
```
|
26
27
|
|
27
28
|
### Resources
|
29
|
+
|
28
30
|
```ruby
|
29
31
|
# root resources
|
30
32
|
campaigns_resource = my_target_api.resource('campaigns')
|
@@ -36,6 +38,7 @@ remarketing_counters_resource = remarketing_resource.resource('counters')
|
|
36
38
|
```
|
37
39
|
|
38
40
|
### Create, Read, Update, Delete
|
41
|
+
|
39
42
|
```ruby
|
40
43
|
remarketing_counters_resource.create(counter_id: 121212) # => [{ 'id' => 343434 }]
|
41
44
|
|
@@ -48,6 +51,15 @@ campaigns_resource.update(id: 12345, status: 'blocked') # => [{ 'id' => 12345, '
|
|
48
51
|
remarketing_counters_resource.delete(id: 343434) # => [{ 'success' => true }]
|
49
52
|
```
|
50
53
|
|
54
|
+
### File upload
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
static_resource = my_target_api.resource('content/static', v: 2)
|
58
|
+
picture = File.new('path/to/picture.jpg', 'rb')
|
59
|
+
|
60
|
+
static_resource.create(file: picture, data: { width: 1200, height: 800 })
|
61
|
+
```
|
62
|
+
|
51
63
|
## Exceptions
|
52
64
|
|
53
65
|
```ruby
|
@@ -4,21 +4,21 @@ class MyTargetApi
|
|
4
4
|
# Error for request
|
5
5
|
class RequestError < StandardError
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
super build_message
|
7
|
+
def initialize(exception)
|
8
|
+
super build_message exception
|
9
9
|
end
|
10
10
|
|
11
11
|
private
|
12
12
|
|
13
|
-
def build_message(
|
14
|
-
body = JSON.parse
|
13
|
+
def build_message(exception)
|
14
|
+
body = JSON.parse exception.response
|
15
15
|
if body['error']
|
16
16
|
"#{body['error']} : #{body['error_description']}"
|
17
17
|
else
|
18
18
|
body.map { |field, error| "#{field}: #{error}" }.join(', ')
|
19
19
|
end
|
20
20
|
rescue StandardError
|
21
|
-
|
21
|
+
exception.response
|
22
22
|
end
|
23
23
|
|
24
24
|
end
|
@@ -29,6 +29,7 @@ class MyTargetApi
|
|
29
29
|
def update(params = {})
|
30
30
|
params = prepare_params(params)
|
31
31
|
id = params.delete(:id)
|
32
|
+
raise ArgumentError, ':id is required' unless id
|
32
33
|
|
33
34
|
api.post_request("#{path}/#{id}.json", params)
|
34
35
|
end
|
@@ -36,6 +37,7 @@ class MyTargetApi
|
|
36
37
|
def delete(params = {})
|
37
38
|
params = prepare_params(params)
|
38
39
|
id = params.delete(:id)
|
40
|
+
raise ArgumentError, ':id is required' unless id
|
39
41
|
|
40
42
|
api.delete_request("#{path}/#{id}.json", params)
|
41
43
|
end
|
@@ -49,7 +51,7 @@ class MyTargetApi
|
|
49
51
|
attr_reader :api, :path
|
50
52
|
|
51
53
|
def prepare_params(params)
|
52
|
-
raise
|
54
|
+
raise ArgumentError, 'Params must be a Hash' unless params.is_a? Hash
|
53
55
|
|
54
56
|
params.map do |param, value|
|
55
57
|
[param.to_sym, value]
|
data/lib/my_target_api.rb
CHANGED
@@ -7,10 +7,10 @@ class MyTargetApi
|
|
7
7
|
|
8
8
|
autoload :Resource, 'my_target_api/resource'
|
9
9
|
autoload :Request, 'my_target_api/request'
|
10
|
-
autoload :Session, 'my_target_api/session'
|
11
10
|
|
12
|
-
autoload :ConnectionError, 'my_target_api/
|
13
|
-
autoload :RequestError, 'my_target_api/
|
11
|
+
autoload :ConnectionError, 'my_target_api/connection_error'
|
12
|
+
autoload :RequestError, 'my_target_api/request_error'
|
13
|
+
autoload :InterfaceError, 'my_target_api/using_error'
|
14
14
|
|
15
15
|
def initialize(access_token, options = {})
|
16
16
|
@access_token = access_token
|
data/spec/request_spec.rb
CHANGED
@@ -38,5 +38,13 @@ describe MyTargetApi::Request do
|
|
38
38
|
subject.delete('https://target.my.com/api/v1/remarketing_context_phrases/53.json')
|
39
39
|
).to eq([{ 'success' => true }])
|
40
40
|
end
|
41
|
+
|
42
|
+
it 'raises exception on bad statuses' do
|
43
|
+
stub_request(:get, 'https://target.my.com/api/v1/wrong_path.json')
|
44
|
+
.to_return(body: 'Unknown resource', status: 404)
|
45
|
+
|
46
|
+
expect { subject.get('https://target.my.com/api/v1/wrong_path.json') }
|
47
|
+
.to raise_error(MyTargetApi::RequestError, 'Unknown resource')
|
48
|
+
end
|
41
49
|
end
|
42
50
|
end
|
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.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Reshetnikov Ivan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -154,11 +154,9 @@ files:
|
|
154
154
|
- README.md
|
155
155
|
- Rakefile
|
156
156
|
- lib/my_target_api.rb
|
157
|
-
- lib/my_target_api/
|
158
|
-
- lib/my_target_api/errors/connection_error.rb
|
159
|
-
- lib/my_target_api/errors/request_error.rb
|
160
|
-
- lib/my_target_api/errors/using_error.rb
|
157
|
+
- lib/my_target_api/connection_error.rb
|
161
158
|
- lib/my_target_api/request.rb
|
159
|
+
- lib/my_target_api/request_error.rb
|
162
160
|
- lib/my_target_api/resource.rb
|
163
161
|
- lib/my_target_api/version.rb
|
164
162
|
- my_target_api.gemspec
|
data/lib/my_target_api/auth.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# see https://target.my.com/doc/api/oauth2
|
4
|
-
|
5
|
-
class MyTargetApi
|
6
|
-
# authorization
|
7
|
-
class Auth
|
8
|
-
|
9
|
-
class << self
|
10
|
-
|
11
|
-
include MyTargetApi::Request
|
12
|
-
|
13
|
-
def authorize_url
|
14
|
-
state = (0...32).map { rand(65..90).chr }.join.downcase
|
15
|
-
'https://target.my.com/oauth2/authorize?response_type=code' \
|
16
|
-
"&client_id=#{MyTargetApi.client_id}&state=#{state}&scope=#{MyTargetApi.scopes}"
|
17
|
-
end
|
18
|
-
|
19
|
-
# We need new method to receive token using `agency_client_credentials` grant type
|
20
|
-
# @param client_username [String] client user_name in myTarget
|
21
|
-
# @return [Hash] containing requested access_token
|
22
|
-
def get_agency_client_credentials(client_username)
|
23
|
-
params = { grant_type: 'agency_client_credentials',
|
24
|
-
agency_client_name: client_username,
|
25
|
-
v: 2 }
|
26
|
-
request :post, '/oauth2/token', params
|
27
|
-
end
|
28
|
-
|
29
|
-
def get_token(code)
|
30
|
-
params = { grant_type: 'authorization_code', code: code, v: 2 }
|
31
|
-
request :post, '/oauth2/token', params
|
32
|
-
end
|
33
|
-
|
34
|
-
def refresh_token(code)
|
35
|
-
params = { grant_type: 'refresh_token', refresh_token: code, v: 2 }
|
36
|
-
request :post, '/oauth2/token', params
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|