blurb 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 040ae071f23800ce24f8c7fe8602d690b6c97983
4
- data.tar.gz: f2b702bd101e5a4f9343ceba783e801cefe07599
3
+ metadata.gz: f418adffdec8cb6dad94905f36907bbb783afc52
4
+ data.tar.gz: 275ea7f93bc0ca704db76c4476c10cd4ecc1ea8a
5
5
  SHA512:
6
- metadata.gz: b1e46b85afb82c8351b7cd30880e4bd923dd56e18cad37772a352fb85712493591f3192b07fd1428de7d86b945745fe000680edbe57b647f2587b497416980dd
7
- data.tar.gz: b6d4fa9ec4ef124eca1e6ed9038f028e5c0cf9dd223a93188dcba1f90852c2649ae5193d5742e30714327b12e24fb2440d42826bbecc43666c0e87486cf28128
6
+ metadata.gz: 5bb60ed1768e2c2bb618518741768fd31759b9a10cae29210ab7ab3c1a5f7ee9d8d05f0059f8d3cd139653b2f6789d8fcaade68fbc0d96f4e4a81a043c2b59b8
7
+ data.tar.gz: be5d2d3a75f03c76493b1ff560f5041a87fc3bb402566d5fa63d96ec9e5e7a4749354d34fbc145ed595abf7c3ab87fc3683aa63306da2f2bbb26cb2405ab20d3
@@ -10,33 +10,17 @@ require "blurb/profile"
10
10
  require "blurb/report"
11
11
  require "blurb/snapshot"
12
12
  require "blurb/suggested_keyword"
13
- require "blurb/token"
14
13
 
15
14
  module Blurb
16
- TOKEN_URL = "https://api.amazon.com"
17
- API_URL = "https://advertising-api.amazon.com"
18
- TEST_API_URL = "https://advertising-api-test.amazon.com"
19
- EU_API_URL = "https://advertising-api-eu.amazon.com"
20
15
 
21
- def self.client
22
- return OAuth2::Client.new(
23
- "",
24
- "",
25
- :site => TOKEN_URL
26
- )
27
- end
28
-
29
- # By default this gem will use the production API url unless the test_env module
30
- # variable is set to true. Then the test API url will be used
31
- def self.active_api_url
32
- if test_env
33
- return TEST_API_URL
34
- end
35
- if eu_env
36
- return EU_API_URL
37
- end
38
-
39
- return API_URL
16
+ def self.default_account
17
+ {
18
+ client_secret: self.client_secret,
19
+ client_id: self.client_id,
20
+ refresh_token: self.refresh_token,
21
+ profile_id: self.profile_id,
22
+ eu_env: self.eu_env
23
+ }
40
24
  end
41
25
 
42
26
  class << self
@@ -1,15 +1,80 @@
1
1
  module Blurb
2
2
  class BaseResource
3
- def self.profile_request(api_path)
4
- access_token = Blurb::Token.retrieve()
3
+ attr_accessor :client_secret, :client_id, :refresh_token, :profile_id, :test_env, :eu_env
4
+
5
+ TOKEN_URL = "https://api.amazon.com"
6
+ API_URL = "https://advertising-api.amazon.com"
7
+ TEST_API_URL = "https://advertising-api-test.amazon.com"
8
+ EU_API_URL = "https://advertising-api-eu.amazon.com"
9
+
10
+ def initialize(account=Blurb.default_account)
11
+ @client_secret = account[:client_secret]
12
+ @client_id = account[:client_id]
13
+ @refresh_token = account[:refresh_token]
14
+ @profile_id = account[:profile_id]
15
+ @test_env = Blurb.test_env
16
+ @eu_env = account[:eu_env]
17
+ end
18
+
19
+ def active_api_url
20
+ if @test_env
21
+ return TEST_API_URL
22
+ end
23
+ if @eu_env
24
+ return EU_API_URL
25
+ end
26
+
27
+ return API_URL
28
+ end
29
+
30
+ def client
31
+ return OAuth2::Client.new(
32
+ "",
33
+ "",
34
+ :site => TOKEN_URL
35
+ )
36
+ end
37
+
38
+ def token_code(auth_code)
39
+ response = client.request(:post, "/auth/o2/token",
40
+ {
41
+ body: {
42
+ grant_type: "authorization_code",
43
+ client_id: @client_id,
44
+ code: auth_code,
45
+ client_secret: @client_secret
46
+ }
47
+ }
48
+ )
49
+
50
+ return JSON.parse(response.body)
51
+ end
52
+
53
+ def retrieve_token(params = {}, opts = {})
54
+ response = client.request(:post, "/auth/o2/token",
55
+ {
56
+ body: {
57
+ grant_type: "refresh_token",
58
+ client_id: @client_id,
59
+ refresh_token: @refresh_token,
60
+ client_secret: @client_secret
61
+ }
62
+ }
63
+ )
64
+
65
+ return JSON.parse(response.body)
66
+ end
67
+
68
+ def profile_request(api_path)
69
+ access_token = retrieve_token()
5
70
 
6
71
  request_config = {
7
72
  method: :get,
8
- url: "#{Blurb.active_api_url}#{api_path}",
73
+ url: "#{active_api_url}#{api_path}",
9
74
  headers: {
10
75
  "Authorization" => "Bearer #{access_token['access_token']}",
11
76
  "Content-Type" => "application/json",
12
- "Amazon-Advertising-API-ClientId" => Blurb.client_id
77
+ "Amazon-Advertising-API-ClientId" => @client_id
13
78
  }
14
79
  }
15
80
 
@@ -17,17 +82,17 @@ module Blurb
17
82
  return JSON.parse(resp)
18
83
  end
19
84
 
20
- def self.get_request(api_path, opts = {})
21
- access_token = Blurb::Token.retrieve()
85
+ def get_request(api_path, opts = {})
86
+ access_token = retrieve_token()
22
87
 
23
- url = "#{Blurb.active_api_url}#{api_path}"
88
+ url = "#{active_api_url}#{api_path}"
24
89
  url = api_path if opts[:full_path]
25
90
 
26
91
  headers_hash = {
27
92
  "Authorization" => "Bearer #{access_token['access_token']}",
28
93
  "Content-Type" => "application/json",
29
- "Amazon-Advertising-API-Scope" => Blurb.profile_id,
30
- "Amazon-Advertising-API-ClientId" => Blurb.client_id
94
+ "Amazon-Advertising-API-Scope" => @profile_id,
95
+ "Amazon-Advertising-API-ClientId" => @client_id
31
96
  }
32
97
 
33
98
  headers_hash["Content-Encoding"] = "gzip" if opts[:gzip]
@@ -43,37 +108,37 @@ module Blurb
43
108
  return make_request(request_config)
44
109
  end
45
110
 
46
- def self.post_request(api_path, payload)
47
- access_token = Blurb::Token.retrieve()
111
+ def post_request(api_path, payload)
112
+ access_token = retrieve_token()
48
113
 
49
114
  request_config = {
50
115
  method: :post,
51
- url: "#{Blurb.active_api_url}#{api_path}",
116
+ url: "#{active_api_url}#{api_path}",
52
117
  payload: payload.to_json,
53
118
  headers: {
54
119
  "Authorization" => "Bearer #{access_token['access_token']}",
55
120
  "Content-Type" => "application/json",
56
- "Amazon-Advertising-API-Scope" => Blurb.profile_id.to_i,
57
- "Amazon-Advertising-API-ClientId" => Blurb.client_id
121
+ "Amazon-Advertising-API-Scope" => @profile_id.to_i,
122
+ "Amazon-Advertising-API-ClientId" => @client_id
58
123
  }
59
124
  }
60
125
 
61
126
  return make_request(request_config)
62
127
  end
63
128
 
64
- def self.delete_request(api_path)
65
- access_token = Blurb::Token.retrieve()
129
+ def delete_request(api_path)
130
+ access_token = retrieve_token()
66
131
 
67
132
  headers_hash = {
68
133
  "Authorization" => "Bearer #{access_token['access_token']}",
69
134
  "Content-Type" => "application/json",
70
- "Amazon-Advertising-API-Scope" => Blurb.profile_id,
71
- "Amazon-Advertising-API-ClientId" => Blurb.client_id
135
+ "Amazon-Advertising-API-Scope" => @profile_id,
136
+ "Amazon-Advertising-API-ClientId" => @client_id
72
137
  }
73
138
 
74
139
  request_config = {
75
140
  method: :delete,
76
- url: "#{Blurb.active_api_url}#{api_path}",
141
+ url: "#{active_api_url}#{api_path}",
77
142
  headers: headers_hash,
78
143
  }
79
144
 
@@ -82,7 +147,7 @@ module Blurb
82
147
 
83
148
  private
84
149
 
85
- def self.make_request(request_config)
150
+ def make_request(request_config)
86
151
  begin
87
152
  resp = RestClient::Request.execute(request_config)
88
153
  rescue RestClient::ExceptionWithResponse => err
@@ -1,20 +1,20 @@
1
1
  module Blurb
2
2
  class BidRecommendation < BaseResource
3
- def self.ad_group_recommendations(params = {}, opts = {})
3
+ def ad_group_recommendations(params = {}, opts = {})
4
4
  # required argument checks
5
5
  raise ArgumentError.new("params hash must contain an adGroupId") unless params["adGroupId"]
6
6
 
7
7
  get_request("/v2/adGroups/#{params["adGroupId"]}/bidRecommendations")
8
8
  end
9
9
 
10
- def self.keyword_recommendations(params = {}, opts = {})
10
+ def keyword_recommendations(params = {}, opts = {})
11
11
  # required argument checks
12
12
  raise ArgumentError.new("params hash must contain an keywordId") unless params["keywordId"]
13
13
 
14
14
  get_request("/v2/keywords/#{params["keywordId"]}/bidRecommendations")
15
15
  end
16
16
 
17
- def self.bulk_keyword_recommendations(params = {}, opts = {})
17
+ def bulk_keyword_recommendations(params = {}, opts = {})
18
18
  # required argument checks
19
19
  raise ArgumentError.new("params hash must contain an array of keywordIds") unless params["keywordIds"]
20
20
 
@@ -3,25 +3,25 @@ module Blurb
3
3
  SPONSORED_PRODUCTS = "sp"
4
4
  SPONSORED_BRANDS = "hsa"
5
5
 
6
- def self.retrieve(campaign_id, campaign_type)
6
+ def retrieve(campaign_id, campaign_type)
7
7
  get_request("/v2/#{campaign_type}/campaigns/#{campaign_id}")
8
8
  end
9
9
 
10
- def self.retrieve_extended(campaign_id, campaign_type)
10
+ def retrieve_extended(campaign_id, campaign_type)
11
11
  raise ArgumentError.new("Extended campaigns interface is only supported for Sponsored Products") unless campaign_type == SPONSORED_PRODUCTS
12
12
  get_request("/v2/#{campaign_type}/campaigns/extended/#{campaign_id}")
13
13
  end
14
14
 
15
- def self.list(campaign_type, params = {}, opts = {})
15
+ def list(campaign_type, params = {}, opts = {})
16
16
  get_request("/v2/#{campaign_type}/campaigns?#{setup_url_params(params)}")
17
17
  end
18
18
 
19
- def self.list_extended(campaign_type, params = {}, opts = {})
19
+ def list_extended(campaign_type, params = {}, opts = {})
20
20
  raise ArgumentError.new("Extended campaigns interface is only supported for Sponsored Products") unless campaign_type == SPONSORED_PRODUCTS
21
21
  get_request("/v2/#{campaign_type}/campaigns/extended?#{setup_url_params(params)}")
22
22
  end
23
23
 
24
- def self.create(campaign_type, params = {}, opts = {})
24
+ def create(campaign_type, params = {}, opts = {})
25
25
  # required argument checks
26
26
  if !params["name"] && !params["targetingType"] && !params["state"] && !params["dailyBudget"] && !params["startDate"]
27
27
  raise ArgumentError.new("params hash must contain name, targetingType, state, dailyBudget and startDate")
@@ -31,13 +31,13 @@ module Blurb
31
31
  post_request("/v2/#{campaign_type}/campaigns", [params])
32
32
  end
33
33
 
34
- def self.delete(campaign_id)
34
+ def delete(campaign_id)
35
35
  delete_request("/v2/campaigns/#{campaign_id}")
36
36
  end
37
37
 
38
38
  private
39
39
 
40
- def self.setup_url_params(params)
40
+ def setup_url_params(params)
41
41
  url_params = ""
42
42
  url_params = "startIndex=#{params['startIndex']}" if params['startIndex']
43
43
 
@@ -1,11 +1,11 @@
1
1
  module Blurb
2
2
  class Profile < BaseResource
3
3
 
4
- def self.list()
4
+ def list()
5
5
  profile_request("/v2/profiles")
6
6
  end
7
7
 
8
- def self.retrieve(profile_id)
8
+ def retrieve(profile_id)
9
9
  profile_request("/v2/profiles/#{profile_id}")
10
10
  end
11
11
  end
@@ -9,7 +9,7 @@ module Blurb
9
9
  SPONSORED_PRODUCTS = "sp"
10
10
  SPONSORED_BRANDS = "hsa"
11
11
 
12
- def self.create(params = {}, opts = {})
12
+ def create(params = {}, opts = {})
13
13
  # required argument checks
14
14
  raise ArgumentError.new("params hash must contain a recordType") unless params["recordType"]
15
15
 
@@ -34,18 +34,16 @@ module Blurb
34
34
  post_request(request_url, api_params)
35
35
  end
36
36
 
37
- def self.status(report_id, opts = {})
37
+ def status(report_id, opts = {})
38
38
  get_request("/v2/reports/#{report_id}")
39
39
  end
40
40
 
41
- def self.download(location, opts = {})
41
+ def download(location, opts = {})
42
42
  opts.merge!({:full_path => true, :gzip => true, :no_token => true})
43
43
  get_request(location, opts)
44
44
  end
45
45
 
46
- private
47
-
48
- def self.get_default_metrics(record_type, campaign_type)
46
+ def get_default_metrics(record_type, campaign_type)
49
47
  if campaign_type == SPONSORED_BRANDS
50
48
  return [
51
49
  "campaignName",
@@ -118,7 +116,6 @@ module Blurb
118
116
  "attributedSales30dOtherSKU"
119
117
  ].join(",") if record_type == ASINS
120
118
  return [
121
- "bidPlus",
122
119
  "campaignName",
123
120
  "campaignId",
124
121
  "campaignStatus",
@@ -10,7 +10,7 @@ module Blurb
10
10
  SPONSORED_PRODUCTS = "sp"
11
11
  SPONSORED_BRANDS = "hsa"
12
12
 
13
- def self.create(params = {}, opts = {})
13
+ def create(params = {}, opts = {})
14
14
  # required argument checks
15
15
  raise ArgumentError.new("params hash must contain a recordType") unless params["recordType"]
16
16
 
@@ -25,11 +25,11 @@ module Blurb
25
25
  })
26
26
  end
27
27
 
28
- def self.status(snapshot_id, opts = {})
28
+ def status(snapshot_id, opts = {})
29
29
  get_request("/v2/snapshots/#{snapshot_id}")
30
30
  end
31
31
 
32
- def self.download(location, opts = {})
32
+ def download(location, opts = {})
33
33
  opts.merge!({:full_path => true, :gzip => true, :no_token => true})
34
34
  get_request(location, opts)
35
35
  end
@@ -1,27 +1,27 @@
1
1
  module Blurb
2
2
  class SuggestedKeyword < BaseResource
3
- def self.ad_group_suggestions(params = {}, opts = {})
3
+ def ad_group_suggestions(params = {}, opts = {})
4
4
  # required argument checks
5
5
  raise ArgumentError.new("params hash must contain an adGroupId") unless params["adGroupId"]
6
6
 
7
7
  get_request("/v2/adGroups/#{params["adGroupId"]}/suggested/keywords?#{setup_url_params(params)}")
8
8
  end
9
9
 
10
- def self.ad_group_extended_suggestions(params = {}, opts = {})
10
+ def ad_group_extended_suggestions(params = {}, opts = {})
11
11
  # required argument checks
12
12
  raise ArgumentError.new("params hash must contain an adGroupId") unless params["adGroupId"]
13
13
 
14
14
  get_request("/v2/adGroups/#{params["adGroupId"]}/suggested/keywords/extended?#{setup_url_params(params)}")
15
15
  end
16
16
 
17
- def self.asin_suggestions(params = {}, opts = {})
17
+ def asin_suggestions(params = {}, opts = {})
18
18
  # required argument checks
19
19
  raise ArgumentError.new("params hash must contain an asinValue") unless params["asinValue"]
20
20
 
21
21
  get_request("/v2/asins/#{params["asinValue"]}/suggested/keywords?#{setup_url_params(params)}")
22
22
  end
23
23
 
24
- def self.bulk_asin_suggestions(params = {}, opts = {})
24
+ def bulk_asin_suggestions(params = {}, opts = {})
25
25
  # required argument checks
26
26
  raise ArgumentError.new("params hash must contain an asins array") unless params["asins"]
27
27
 
@@ -36,7 +36,7 @@ module Blurb
36
36
 
37
37
  private
38
38
 
39
- def self.setup_url_params(params)
39
+ def setup_url_params(params)
40
40
  url_params = ""
41
41
  url_params = "maxNumSuggestions=#{params['maxNumSuggestions']}" if params['maxNumSuggestions']
42
42
 
@@ -1,3 +1,3 @@
1
1
  module Blurb
2
- VERSION = "0.2.3".freeze
2
+ VERSION = "0.3.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blurb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - dlbunker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-18 00:00:00.000000000 Z
11
+ date: 2019-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,7 +122,6 @@ files:
122
122
  - lib/blurb/report.rb
123
123
  - lib/blurb/snapshot.rb
124
124
  - lib/blurb/suggested_keyword.rb
125
- - lib/blurb/token.rb
126
125
  - lib/blurb/version.rb
127
126
  homepage: https://github.com/iserve-products/blurb
128
127
  licenses:
@@ -1,34 +0,0 @@
1
- module Blurb
2
- class Token
3
-
4
- def self.code(auth_code)
5
- response = Blurb::client.request(:post, "/auth/o2/token",
6
- {
7
- body: {
8
- grant_type: "authorization_code",
9
- client_id: Blurb.client_id,
10
- code: auth_code,
11
- client_secret: Blurb.client_secret
12
- }
13
- }
14
- )
15
-
16
- return JSON.parse(response.body)
17
- end
18
-
19
- def self.retrieve(params = {}, opts = {})
20
- response = Blurb::client.request(:post, "/auth/o2/token",
21
- {
22
- body: {
23
- grant_type: "refresh_token",
24
- client_id: Blurb.client_id,
25
- refresh_token: Blurb.refresh_token,
26
- client_secret: Blurb.client_secret
27
- }
28
- }
29
- )
30
-
31
- return JSON.parse(response.body)
32
- end
33
- end
34
- end