txbr 1.0.1 → 1.1.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
  SHA256:
3
- metadata.gz: 31315acfac2024dccf07c712f3146c897d85f1932e3cf9d832dd5d0eeef3fadf
4
- data.tar.gz: d7f450603dad38710b0415e8fa5a76d38d99fcc3af48c90a756b75b3d2cc7e47
3
+ metadata.gz: 5715c1621b00c110e167d8116399482d7bad3622a7fcd205ac64bfac194f654e
4
+ data.tar.gz: 016b5d0f0455b11a0b73986f9aac04a704ca74d1168a2d417bee9479873b33e0
5
5
  SHA512:
6
- metadata.gz: 9e52a042f6b550f9616d8f06454691172befa00dc9933defaecfccd88cd0033996f140f11cac3d18c492c01fe8c2a667b38a4828f9ae87c0abec4a17152aa349
7
- data.tar.gz: 12987f0c5e313fadb81e256d1c5ebad38aea68a5bb67d8406fc52dd88aa53789ec12f52684124473386eb117750a164e080bc7037034160b2593d86f32a74bfb
6
+ metadata.gz: 9e902060a7bc2a89399f2217188bc05c0069319e1177068bca3522ed86f75c451eca5dc375a348bf1f45c37a6332f12f0d0bff7ba60052c64cf5492c31ff95fe
7
+ data.tar.gz: ea742212714f4d9bd8227275867266729925f7cc9d69071222e8db7e7b8f13d400da080ada51ddfc0f4f3e14111fe984b34e22742c93a94483abd8f6ec56cbbd
data/README.md CHANGED
@@ -47,16 +47,14 @@ Configuration
47
47
  Txbr requires several configuration options in order to communicate with Braze and Transifex. Configuration is done in the YAML file format. Here's an example:
48
48
 
49
49
  ```yaml
50
+ transifex_api_username: tx_username
51
+ transifex_api_password: tx_password
50
52
  projects:
51
53
  - handler_id: email-templates
52
- transifex_api_username: tx_username
53
- transifex_api_password: tx_password
54
54
  strings_format: YML
55
55
  source_lang: en
56
- braze_email_address: login@yourcompany.com
57
- braze_password: your_braze_password
58
- braze_app_group_id: your_braze_app_group_id
59
- braze_api_url: https://dashboard-03.braze.com/
56
+ braze_api_url: https://rest.iad-01.braze.com/ # or whatever
57
+ braze_api_key: abc123
60
58
 
61
59
  ```
62
60
 
@@ -3,21 +3,38 @@ require 'faraday_middleware'
3
3
 
4
4
  module Txbr
5
5
  class BrazeApi
6
+ TEMPLATE_BATCH_SIZE = 35
7
+ TEMPLATE_LIST_PATH = 'templates/email/list'.freeze
8
+ TEMPLATE_INFO_PATH = 'templates/email/info'.freeze
9
+
6
10
  include RequestMethods
7
11
 
8
12
  attr_reader :api_key, :api_url
9
13
 
10
- def initialize(api_key, api_url)
14
+ def initialize(api_key, api_url, connection: nil)
11
15
  @api_key = api_key
12
16
  @api_url = api_url
17
+ @connection = connection
13
18
  end
14
19
 
15
- def each_email_template
16
- raise NotImplementedError, 'Braze does not support this operation yet'
20
+ def each_email_template(offset: 1, &block)
21
+ return to_enum(__method__, offset: offset) unless block_given?
22
+
23
+ loop do
24
+ templates = get_json(
25
+ TEMPLATE_LIST_PATH,
26
+ offset: offset,
27
+ limit: TEMPLATE_BATCH_SIZE
28
+ )
29
+
30
+ templates['templates'].each(&block)
31
+ offset += templates['templates'].size
32
+ break if templates['templates'].size < TEMPLATE_BATCH_SIZE
33
+ end
17
34
  end
18
35
 
19
36
  def get_email_template_details(email_template_id:)
20
- raise NotImplementedError, 'Braze does not support this operation yet'
37
+ get_json(TEMPLATE_INFO_PATH, email_template_id: email_template_id)
21
38
  end
22
39
 
23
40
  private
@@ -40,7 +40,7 @@ module Txbr
40
40
  private
41
41
 
42
42
  def strings_map
43
- @strings_map ||= %w(template subject preheader).each_with_object({}) do |name, ret|
43
+ @strings_map ||= %w(body subject preheader).each_with_object({}) do |name, ret|
44
44
  component = EmailTemplateComponent.new(
45
45
  Liquid::Template.parse(details[name])
46
46
  )
@@ -58,7 +58,7 @@ module Txbr
58
58
  end
59
59
 
60
60
  def template_name
61
- details['name']
61
+ details['template_name']
62
62
  end
63
63
 
64
64
  def details
@@ -17,7 +17,7 @@ module Txbr
17
17
  return to_enum(__method__) unless block_given?
18
18
 
19
19
  project.braze_api.each_email_template do |tmpl|
20
- yield EmailTemplate.new(project, tmpl['id'])
20
+ yield EmailTemplate.new(project, tmpl['email_template_id'])
21
21
  end
22
22
  end
23
23
  end
data/lib/txbr/project.rb CHANGED
@@ -5,9 +5,6 @@ module Txbr
5
5
  attr_reader :braze_api_url, :braze_api_key, :handler_id
6
6
  attr_reader :strings_format, :source_lang
7
7
 
8
- # @TODO: remove these when Braze gives us the endpoints we asked for
9
- attr_reader :braze_app_group_id, :braze_email_address, :braze_password
10
-
11
8
  def initialize(options = {})
12
9
  @braze_api_url = options.fetch(:braze_api_url)
13
10
  @braze_api_key = options.fetch(:braze_api_key)
@@ -15,25 +12,12 @@ module Txbr
15
12
  @strings_format = options.fetch(:strings_format)
16
13
  @source_lang = options.fetch(:source_lang)
17
14
 
18
- # @TODO: remove these when Braze gives us the endpoints we asked for
19
- @braze_app_group_id = options.fetch(:braze_app_group_id)
20
- @braze_email_address = options.fetch(:braze_email_address)
21
- @braze_password = options.fetch(:braze_password)
22
-
23
15
  @braze_api = options[:braze_api]
24
16
  @transifex_api = options[:transifex_api]
25
17
  end
26
18
 
27
- def braze_session
28
- @@braze_session ||= Txbr::BrazeSession.new(
29
- braze_api_url, braze_email_address, braze_password
30
- )
31
- end
32
-
33
19
  def braze_api
34
- # @TODO: use BrazeApi when Braze gives us the endpoints we asked for
35
- # @braze_api ||= Txbr::BrazeApi.new(braze_api_key, braze_api_url)
36
- @braze_api ||= Txbr::BrazeSessionApi.new(braze_session, braze_app_group_id)
20
+ @braze_api ||= Txbr::BrazeApi.new(braze_api_key, braze_api_url)
37
21
  end
38
22
 
39
23
  def transifex_api
data/lib/txbr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Txbr
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -1,14 +1,11 @@
1
1
  require 'spec_helper'
2
- require 'support/fake_braze_session'
3
2
  require 'support/fake_connection'
4
3
 
5
- describe Txbr::BrazeSessionApi do
6
- let(:session_id) { 'session_id' }
7
- let(:app_group_id) { 'app_group_id' }
4
+ describe Txbr::BrazeApi do
5
+ let(:api_key) { 'abc123' }
8
6
  let(:api_url) { 'https://somewhere.braze.com' }
9
- let(:session) { FakeBrazeSession.new(api_url, session_id) }
10
7
  let(:connection) { FakeConnection.new(interactions) }
11
- let(:client) { described_class.new(session, app_group_id, connection: connection) }
8
+ let(:client) { described_class.new(api_key, api_url, connection: connection) }
12
9
 
13
10
  shared_examples 'a client request that handles errors' do
14
11
  context 'when the resource is not found' do
@@ -18,7 +15,7 @@ describe Txbr::BrazeSessionApi do
18
15
  end
19
16
  end
20
17
 
21
- it 'raises an error' do
18
+ it 'raises a not found error' do
22
19
  expect { subject }.to raise_error(Txbr::BrazeNotFoundError)
23
20
  end
24
21
  end
@@ -33,9 +30,8 @@ describe Txbr::BrazeSessionApi do
33
30
  end
34
31
  end
35
32
 
36
- it 'resets the session and tries again' do
37
- expect { subject }.to_not raise_error
38
- expect(session).to be_reset
33
+ it 'raises an unauthorized error' do
34
+ expect { subject }.to raise_error(Txbr::BrazeUnauthorizedError)
39
35
  end
40
36
  end
41
37
 
@@ -46,34 +42,37 @@ describe Txbr::BrazeSessionApi do
46
42
  end
47
43
  end
48
44
 
49
- it 'raises an error' do
45
+ it 'raises a generic error' do
50
46
  expect { subject }.to raise_error(Txbr::BrazeApiError)
51
47
  end
52
48
  end
53
49
  end
54
50
 
55
51
  describe '#each_email_template' do
52
+ subject { client.each_email_template.to_a }
53
+
56
54
  before do
57
- stub_const("#{described_class.name}::EMAIL_TEMPLATE_BATCH_SIZE", 1)
55
+ stub_const("#{described_class.name}::TEMPLATE_BATCH_SIZE", 1)
58
56
  end
59
57
 
60
- subject { client.each_email_template.to_a }
61
-
62
58
  let(:interactions) do
63
59
  [{
64
- request: { verb: 'get', url: 'engagement/email_templates', start: 0, length: 1 },
65
- response: { status: 200, body: { results: [{ id: '123abc' }] }.to_json }
60
+ request: { verb: 'get', url: described_class::TEMPLATE_LIST_PATH, params: { offset: 1, limit: 1 } },
61
+ response: { status: 200, body: { templates: [{ email_template_id: '123abc' }] }.to_json }
66
62
  }, {
67
- request: { verb: 'get', url: 'engagement/email_templates', start: 1, length: 1 },
68
- response: { status: 200, body: { results: [{ id: '456def' }] }.to_json }
63
+ request: { verb: 'get', url: described_class::TEMPLATE_LIST_PATH, params: { offset: 2, limit: 1 } },
64
+ response: { status: 200, body: { templates: [{ email_template_id: '456def' }] }.to_json }
69
65
  }, {
70
- request: { verb: 'get', url: 'engagement/email_templates', start: 2, length: 0 },
71
- response: { status: 200, body: { results: [] }.to_json }
66
+ request: { verb: 'get', url: described_class::TEMPLATE_LIST_PATH, params: { offset: 3, limit: 1 } },
67
+ response: { status: 200, body: { templates: [] }.to_json }
72
68
  }]
73
69
  end
74
70
 
75
71
  it 'yields each template' do
76
- expect(subject).to eq([{ 'id' => '123abc' }, { 'id' => '456def' }])
72
+ expect(subject).to eq([
73
+ { 'email_template_id' => '123abc' },
74
+ { 'email_template_id' => '456def' }
75
+ ])
77
76
  end
78
77
 
79
78
  it_behaves_like 'a client request that handles errors'
@@ -85,16 +84,16 @@ describe Txbr::BrazeSessionApi do
85
84
 
86
85
  let(:details) do
87
86
  {
88
- 'name' => 'Foo Template',
89
- 'template' => "<html><body>I'm a little teapot</body</html>",
87
+ 'template_name' => 'Foo Template',
88
+ 'body' => "<html><body>I'm a little teapot</body</html>",
90
89
  'subject' => 'Subject subject',
91
- 'preheader' => 'Preheader preheader',
90
+ 'preheader' => 'Preheader preheader'
92
91
  }
93
92
  end
94
93
 
95
94
  let(:interactions) do
96
95
  [{
97
- request: { verb: 'get', url: "engagement/email_templates/#{email_template_id}" },
96
+ request: { verb: 'get', url: described_class::TEMPLATE_INFO_PATH, params: { email_template_id: email_template_id } },
98
97
  response: { status: 200, body: details.to_json }
99
98
  }]
100
99
  end
data/spec/config_spec.rb CHANGED
@@ -25,11 +25,6 @@ describe Txbr::Config do
25
25
  expect(project.braze_api_url).to eq('https://somewhere.braze.com')
26
26
  expect(project.strings_format).to eq('YML')
27
27
  expect(project.source_lang).to eq('en')
28
-
29
- # @TODO: remove once Braze implements the endpoints we asked for
30
- expect(project.braze_email_address).to eq('braze@email.com')
31
- expect(project.braze_password).to eq('braze_password')
32
- expect(project.braze_app_group_id).to eq('5551212')
33
28
  end
34
29
  end
35
30
 
@@ -7,7 +7,7 @@ describe Txbr::EmailTemplate do
7
7
  let(:email_template_id) { 'abc123' }
8
8
  let(:email_template) { described_class.new(project, email_template_id) }
9
9
 
10
- let(:template_html) do
10
+ let(:body_html) do
11
11
  <<~HTML
12
12
  <html>
13
13
  <head>
@@ -51,12 +51,16 @@ describe Txbr::EmailTemplate do
51
51
  describe '#each_resource' do
52
52
  let(:braze_interactions) do
53
53
  [{
54
- request: { verb: 'get', url: "engagement/email_templates/#{email_template_id}" },
54
+ request: {
55
+ verb: 'get',
56
+ url: Txbr::BrazeApi::TEMPLATE_INFO_PATH,
57
+ params: { email_template_id: email_template_id }
58
+ },
55
59
  response: {
56
60
  status: 200,
57
61
  body: {
58
- name: 'Super Slick Awesome',
59
- template: template_html,
62
+ template_name: 'Super Slick Awesome',
63
+ body: body_html,
60
64
  subject: subject_html,
61
65
  preheader: preheader_html
62
66
  }.to_json
@@ -1,17 +1,15 @@
1
1
  require 'txgh'
2
- require 'support/fake_braze_session'
3
2
  require 'support/fake_connection'
4
3
  require 'support/test_config'
5
4
 
6
5
  shared_context 'standard setup' do
7
- let(:session_id) { 'session_id' }
8
- let(:app_group_id) { config[:braze_app_group_id] }
6
+ let(:config) { TestConfig.config[:projects].first }
7
+ let(:api_url) { config[:braze_api_url] }
8
+ let(:api_key) { config[:braze_api_key] }
9
9
  let(:braze_connection) { FakeConnection.new(braze_interactions) }
10
10
  let(:transifex_connection) { FakeConnection.new(transifex_interactions) }
11
- let(:braze_session) { FakeBrazeSession.new(config[:braze_api_url], session_id) }
12
- let(:braze_api) { Txbr::BrazeSessionApi.new(braze_session, app_group_id, connection: braze_connection) }
11
+ let(:braze_api) { Txbr::BrazeApi.new(api_key, api_url, connection: braze_connection) }
13
12
  let(:transifex_api) { Txgh::TransifexApi.create_from_connection(transifex_connection) }
14
- let(:config) { TestConfig.config[:projects].first }
15
13
  let(:project) { Txbr::Project.new(config.merge(braze_api: braze_api, transifex_api: transifex_api)) }
16
14
 
17
15
  # override these
@@ -9,11 +9,6 @@ class TestConfig
9
9
  braze_api_url: 'https://somewhere.braze.com',
10
10
  strings_format: 'YML',
11
11
  source_lang: 'en',
12
-
13
- # @TODO: remove once Braze implements the endpoints we asked for
14
- braze_email_address: 'braze@email.com',
15
- braze_password: 'braze_password',
16
- braze_app_group_id: '5551212'
17
12
  }]
18
13
  }
19
14
  end
@@ -8,15 +8,22 @@ describe Txbr::Uploader do
8
8
 
9
9
  let(:braze_interactions) do
10
10
  [{
11
- request: { verb: 'get', url: 'engagement/email_templates', start: 0, length: 1 },
12
- response: { status: 200, body: { results: [{ id: email_template_id }] }.to_json }
11
+ request: {
12
+ verb: 'get',
13
+ url: Txbr::BrazeApi::TEMPLATE_LIST_PATH,
14
+ params: { offset: 1, limit: Txbr::BrazeApi::TEMPLATE_BATCH_SIZE }
15
+ },
16
+ response: {
17
+ status: 200,
18
+ body: { templates: [{ email_template_id: email_template_id }] }.to_json
19
+ }
13
20
  }, {
14
- request: { verb: 'get', url: "engagement/email_templates/#{email_template_id}" },
21
+ request: { verb: 'get', url: Txbr::BrazeApi::TEMPLATE_INFO_PATH, params: { email_template_id: email_template_id } },
15
22
  response: {
16
23
  status: 200,
17
24
  body: {
18
- name: 'Super Slick Awesome',
19
- template: template_html,
25
+ template_name: 'Super Slick Awesome',
26
+ body: body_html,
20
27
  subject: '',
21
28
  preheader: ''
22
29
  }.to_json
@@ -35,7 +42,7 @@ describe Txbr::Uploader do
35
42
  }]
36
43
  end
37
44
 
38
- let(:template_html) do
45
+ let(:body_html) do
39
46
  <<~HTML
40
47
  <html>
41
48
  <head>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: txbr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-30 00:00:00.000000000 Z
11
+ date: 2018-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: abroad
@@ -135,7 +135,6 @@ files:
135
135
  - lib/txbr/application.rb
136
136
  - lib/txbr/braze_api.rb
137
137
  - lib/txbr/braze_session.rb
138
- - lib/txbr/braze_session_api.rb
139
138
  - lib/txbr/commands.rb
140
139
  - lib/txbr/config.rb
141
140
  - lib/txbr/email_template.rb
@@ -149,7 +148,7 @@ files:
149
148
  - lib/txbr/utils.rb
150
149
  - lib/txbr/version.rb
151
150
  - spec/application_spec.rb
152
- - spec/braze_session_api_spec.rb
151
+ - spec/braze_api_spec.rb
153
152
  - spec/braze_session_spec.rb
154
153
  - spec/config_spec.rb
155
154
  - spec/email_template_spec.rb
@@ -157,7 +156,6 @@ files:
157
156
  - spec/spec_helper.rb
158
157
  - spec/strings_manifest_spec.rb
159
158
  - spec/support/env_helpers.rb
160
- - spec/support/fake_braze_session.rb
161
159
  - spec/support/fake_connection.rb
162
160
  - spec/support/standard_setup.rb
163
161
  - spec/support/test_config.rb
@@ -1,73 +0,0 @@
1
- require 'faraday'
2
- require 'faraday_middleware'
3
-
4
- module Txbr
5
- class BrazeSessionApi
6
- EMAIL_TEMPLATE_BATCH_SIZE = 35
7
-
8
- include RequestMethods
9
-
10
- attr_reader :session, :app_group_id
11
-
12
- def initialize(session, app_group_id, connection: nil)
13
- @session = session
14
- @app_group_id = app_group_id
15
- @connection = connection
16
- end
17
-
18
- def each_email_template(start: 0, &block)
19
- return to_enum(__method__, start: start) unless block_given?
20
-
21
- loop do
22
- templates = get_json(
23
- 'engagement/email_templates',
24
- start: start,
25
- limit: EMAIL_TEMPLATE_BATCH_SIZE
26
- )
27
-
28
- templates['results'].each(&block)
29
- start += templates['results'].size
30
- break if templates['results'].size < EMAIL_TEMPLATE_BATCH_SIZE
31
- end
32
- end
33
-
34
- def get_email_template_details(email_template_id:)
35
- get_json("engagement/email_templates/#{email_template_id}")
36
- end
37
-
38
- private
39
-
40
- def act(*args)
41
- retried = false
42
-
43
- begin
44
- super(*args, { cookie: "_session_id=#{session.session_id}" })
45
- rescue BrazeUnauthorizedError => e
46
- raise e if retried
47
- reset!
48
- retried = true
49
- retry
50
- end
51
- end
52
-
53
- def reset!
54
- session.reset!
55
- end
56
-
57
- def connection
58
- @connection ||= begin
59
- options = {
60
- url: session.api_url,
61
- params: { app_group_id: app_group_id }
62
- }
63
-
64
- Faraday.new(options) do |faraday|
65
- faraday.request(:json)
66
- faraday.response(:logger)
67
- faraday.use(FaradayMiddleware::FollowRedirects)
68
- faraday.adapter(:net_http)
69
- end
70
- end
71
- end
72
- end
73
- end
@@ -1,14 +0,0 @@
1
- class FakeBrazeSession
2
- attr_reader :api_url, :session_id, :was_reset
3
- alias reset? was_reset
4
-
5
- def initialize(api_url, session_id)
6
- @api_url = api_url
7
- @session_id = session_id
8
- @was_reset = false
9
- end
10
-
11
- def reset!
12
- @was_reset = true
13
- end
14
- end