buzzrb 0.1.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77debe2be42e0aaef64398c094531219d7a7bebaa5be3855035f8047f7ff5807
4
- data.tar.gz: 4d34d129f1d710eb5f5f87a849aefe0853a91c4be6659f2161610feb32b44656
3
+ metadata.gz: 57b591611ae054f922120874732de27cb8262e9e69106bafe45b12f2ffc45375
4
+ data.tar.gz: 5942a746b729b99c7e3df9372b9262451c09681cb2256fea779042869c2420a7
5
5
  SHA512:
6
- metadata.gz: 88998110007260bf612ece0a21f5674f2d6ecb87db008ea183ec0ecba4c32a0d42f49038b1552ed97d03ae5b4155ff2b37ebe6def2521f7fe3beeda82ae777c8
7
- data.tar.gz: 81d7ef463ce8eda9c599932037c5bd7236e9907d2d38831baadcc4ddec7aecaa581163c0a928d4b309309d1cc84b8f9fef6704f36b05fb76a29b1e4bde2e07b5
6
+ metadata.gz: 7850c71454ce6e1eb2fa378613c8cf8a94909f895d52c5b9f14685294e5345f746e8838c5114b8e8eb5eb157cfb2ed4e0b9f7dc8d537f08ab4a6372497db871e
7
+ data.tar.gz: 2c4815101a08f218fda17b81fede7f38dcf85e025d0f6635522a36b11a06ddb76d2378e3425583bc0f443cd75f064426764f5a962a12ba609913e98e465cbac9
data/lib/buzz/client.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "net/http"
4
4
  require "uri"
5
5
  require "json"
6
+ require "tempfile"
6
7
 
7
8
  module Buzz
8
9
  class Client
@@ -19,6 +20,7 @@ module Buzz
19
20
  else
20
21
  Buzz.configuration.dup
21
22
  end
23
+
22
24
  @config.validate!
23
25
  @cookie_jar = CookieJar.new
24
26
  @http = nil
@@ -44,19 +46,22 @@ module Buzz
44
46
 
45
47
  def authenticate
46
48
  uri = build_uri("/rest/v2/authenticate")
47
- body = { email: config.email, password: config.password }
49
+ body = {email: config.email, password: config.password}
48
50
  body[:account_id] = config.account_id if config.account_id
49
51
 
50
52
  req = build_request(:post, uri)
53
+ req["Content-Type"] = "application/json"
51
54
  req.body = JSON.generate(body)
52
55
 
53
56
  response = execute(uri, req)
54
57
 
55
58
  unless response.success?
56
- raise AuthenticationError.new(
57
- "Authentication failed: #{response.body}",
58
- status: response.status,
59
- body: response.data
59
+ raise(
60
+ AuthenticationError.new(
61
+ "Authentication failed: #{response.body}",
62
+ status: response.status,
63
+ body: response.data
64
+ )
60
65
  )
61
66
  end
62
67
 
@@ -78,6 +83,10 @@ module Buzz
78
83
  Resources::Advertiser.new(self)
79
84
  end
80
85
 
86
+ def advertiser_categories
87
+ Resources::AdvertiserCategory.new(self)
88
+ end
89
+
81
90
  def campaigns
82
91
  Resources::Campaign.new(self)
83
92
  end
@@ -111,7 +120,7 @@ module Buzz
111
120
  end
112
121
 
113
122
  def search(query:, types: nil)
114
- params = { q: query }
123
+ params = {q: query}
115
124
  params[:entity_types] = Array(types).map(&:to_s).join(",") if types
116
125
  get("/rest/v2/search", params)
117
126
  end
@@ -121,6 +130,7 @@ module Buzz
121
130
  def keep_logged_in
122
131
  uri = build_uri("/rest/v2/authenticate/keep_logged_in")
123
132
  req = build_request(:post, uri)
133
+ req["Content-Type"] = "application/json"
124
134
  req.body = JSON.generate({})
125
135
  execute(uri, req)
126
136
  end
@@ -130,7 +140,15 @@ module Buzz
130
140
 
131
141
  uri = build_uri(path, params)
132
142
  req = build_request(method, uri)
133
- req.body = JSON.generate(body) if body
143
+
144
+ if body
145
+ if multipart?(body)
146
+ req.set_form(format_multipart(body), "multipart/form-data")
147
+ else
148
+ req["Content-Type"] = "application/json"
149
+ req.body = JSON.generate(body)
150
+ end
151
+ end
134
152
 
135
153
  response = execute(uri, req)
136
154
 
@@ -139,7 +157,16 @@ module Buzz
139
157
  authenticate
140
158
  uri = build_uri(path, params)
141
159
  req = build_request(method, uri)
142
- req.body = JSON.generate(body) if body
160
+
161
+ if body
162
+ if multipart?(body)
163
+ req.set_form(format_multipart(body), "multipart/form-data")
164
+ else
165
+ req["Content-Type"] = "application/json"
166
+ req.body = JSON.generate(body)
167
+ end
168
+ end
169
+
143
170
  response = execute(uri, req)
144
171
  end
145
172
 
@@ -157,19 +184,23 @@ module Buzz
157
184
  query = params.map { |k, v| "#{URI.encode_www_form_component(k)}=#{URI.encode_www_form_component(v)}" }
158
185
  uri.query = query.join("&")
159
186
  end
187
+
160
188
  uri
161
189
  end
162
190
 
163
191
  def build_request(method, uri)
164
192
  klass = case method
165
- when :get then Net::HTTP::Get
166
- when :post then Net::HTTP::Post
167
- when :put then Net::HTTP::Put
168
- when :delete then Net::HTTP::Delete
169
- end
193
+ when :get
194
+ Net::HTTP::Get
195
+ when :post
196
+ Net::HTTP::Post
197
+ when :put
198
+ Net::HTTP::Put
199
+ when :delete
200
+ Net::HTTP::Delete
201
+ end
170
202
 
171
203
  req = klass.new(uri)
172
- req["Content-Type"] = "application/json"
173
204
  req["Accept"] = "application/json"
174
205
  req["X-Timezone"] = config.timezone if config.timezone
175
206
 
@@ -203,6 +234,7 @@ module Buzz
203
234
  @http.keep_alive_timeout = 30
204
235
  @http.start
205
236
  end
237
+
206
238
  @http
207
239
  end
208
240
  end
@@ -227,6 +259,28 @@ module Buzz
227
259
  end
228
260
  end
229
261
 
262
+ def multipart?(body)
263
+ return false unless body.is_a?(Hash) || body.is_a?(Array)
264
+
265
+ if body.is_a?(Hash)
266
+ body.values.any? { |v| v.is_a?(IO) || (defined?(Tempfile) && v.is_a?(Tempfile)) }
267
+ else
268
+ body.any? { |v| v.is_a?(IO) || (defined?(Tempfile) && v.is_a?(Tempfile)) }
269
+ end
270
+ end
271
+
272
+ def format_multipart(body)
273
+ return body unless body.is_a?(Hash)
274
+
275
+ body.map do |k, v|
276
+ if v.is_a?(IO) || (defined?(Tempfile) && v.is_a?(Tempfile))
277
+ [k.to_s, v]
278
+ else
279
+ [k.to_s, v.to_s]
280
+ end
281
+ end
282
+ end
283
+
230
284
  def error_message(response)
231
285
  if response.data.is_a?(Hash) && response.data["message"]
232
286
  response.data["message"]
@@ -2,8 +2,16 @@
2
2
 
3
3
  module Buzz
4
4
  class Configuration
5
- attr_accessor :buzz_key, :email, :password, :keep_logged_in,
6
- :account_id, :timezone, :open_timeout, :read_timeout
5
+ attr_accessor(
6
+ :buzz_key,
7
+ :email,
8
+ :password,
9
+ :keep_logged_in,
10
+ :account_id,
11
+ :timezone,
12
+ :open_timeout,
13
+ :read_timeout
14
+ )
7
15
  attr_writer :base_url
8
16
 
9
17
  def initialize
@@ -68,7 +68,7 @@ module Buzz
68
68
  value = name_value[(eq_index + 1)..].strip
69
69
  return nil if name.empty?
70
70
 
71
- attrs = { name: name, value: value, domain: uri.host, path: "/", secure: false, httponly: false }
71
+ attrs = {name: name, value: value, domain: uri.host, path: "/", secure: false, httponly: false}
72
72
 
73
73
  parts.each do |part|
74
74
  key, val = part.split("=", 2).map(&:strip)
data/lib/buzz/error.rb CHANGED
@@ -11,7 +11,9 @@ module Buzz
11
11
  end
12
12
  end
13
13
 
14
- class AuthenticationError < Error; end
14
+ class AuthenticationError < Error
15
+ end
16
+
15
17
  class RateLimitError < Error
16
18
  attr_reader :retry_after
17
19
 
@@ -20,7 +22,13 @@ module Buzz
20
22
  super(message, status: status, body: body)
21
23
  end
22
24
  end
23
- class NotFoundError < Error; end
24
- class ValidationError < Error; end
25
- class ServerError < Error; end
25
+
26
+ class NotFoundError < Error
27
+ end
28
+
29
+ class ValidationError < Error
30
+ end
31
+
32
+ class ServerError < Error
33
+ end
26
34
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buzz
4
+ module Resources
5
+ class AdvertiserCategory < Resource
6
+ private
7
+
8
+ def resource_path
9
+ "/rest/v2/ref/advertiser-categories"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -11,7 +11,7 @@ module Buzz
11
11
 
12
12
  # Query performance data (spend, impressions, clicks, etc.)
13
13
  def query(dimensions: [], metrics: [], **params)
14
- body = { dimensions: dimensions, metrics: metrics }.merge(params)
14
+ body = {dimensions: dimensions, metrics: metrics}.merge(params)
15
15
  response = client.post("/rest/v2/report-data", body)
16
16
  response.data["results"]
17
17
  end
data/lib/buzz/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Buzz
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/buzz.rb CHANGED
@@ -8,6 +8,7 @@ require_relative "buzz/response"
8
8
  require_relative "buzz/paginator"
9
9
  require_relative "buzz/resource"
10
10
  require_relative "buzz/resources/advertiser"
11
+ require_relative "buzz/resources/advertiser_category"
11
12
  require_relative "buzz/resources/campaign"
12
13
  require_relative "buzz/resources/line_item"
13
14
  require_relative "buzz/resources/creative"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buzzrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Letterpress
@@ -65,6 +65,7 @@ files:
65
65
  - lib/buzz/paginator.rb
66
66
  - lib/buzz/resource.rb
67
67
  - lib/buzz/resources/advertiser.rb
68
+ - lib/buzz/resources/advertiser_category.rb
68
69
  - lib/buzz/resources/campaign.rb
69
70
  - lib/buzz/resources/creative.rb
70
71
  - lib/buzz/resources/creative_asset.rb