braze_ruby 0.0.1

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.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +157 -0
  7. data/Rakefile +1 -0
  8. data/braze_ruby.gemspec +33 -0
  9. data/lib/braze_ruby.rb +9 -0
  10. data/lib/braze_ruby/api.rb +31 -0
  11. data/lib/braze_ruby/deprecated.rb +19 -0
  12. data/lib/braze_ruby/endpoints.rb +4 -0
  13. data/lib/braze_ruby/endpoints/email_status.rb +13 -0
  14. data/lib/braze_ruby/endpoints/schedule_messages.rb +15 -0
  15. data/lib/braze_ruby/endpoints/send_messages.rb +15 -0
  16. data/lib/braze_ruby/endpoints/track_users.rb +29 -0
  17. data/lib/braze_ruby/http.rb +30 -0
  18. data/lib/braze_ruby/rest.rb +7 -0
  19. data/lib/braze_ruby/rest/base.rb +19 -0
  20. data/lib/braze_ruby/rest/email_status.rb +22 -0
  21. data/lib/braze_ruby/rest/export_users.rb +27 -0
  22. data/lib/braze_ruby/rest/list_segments.rb +11 -0
  23. data/lib/braze_ruby/rest/schedule_messages.rb +28 -0
  24. data/lib/braze_ruby/rest/send_messages.rb +22 -0
  25. data/lib/braze_ruby/rest/track_users.rb +14 -0
  26. data/lib/braze_ruby/version.rb +3 -0
  27. data/spec/braze_ruby/api_spec.rb +4 -0
  28. data/spec/braze_ruby/endpoints/track_users_spec.rb +72 -0
  29. data/spec/braze_ruby/rest/email_status_spec.rb +19 -0
  30. data/spec/braze_ruby/rest/export_users_spec.rb +19 -0
  31. data/spec/braze_ruby/rest/schedule_messages_spec.rb +36 -0
  32. data/spec/braze_ruby/rest/send_messages_spec.rb +33 -0
  33. data/spec/braze_ruby/rest/track_users_spec.rb +24 -0
  34. data/spec/factories.rb +36 -0
  35. data/spec/fixtures/responses/email_status/existing_email/responds_with_created.yml +69 -0
  36. data/spec/fixtures/responses/email_status/existing_email/responds_with_success_message.yml +69 -0
  37. data/spec/fixtures/responses/email_status/unknown_email/responds_with_bad_request.yml +69 -0
  38. data/spec/fixtures/responses/email_status/unknown_email/responds_with_success_message.yml +69 -0
  39. data/spec/fixtures/responses/export_users/by_ids/with_success/responds_with_created.yml +69 -0
  40. data/spec/fixtures/responses/export_users/by_segment/with_success/responds_with_created.yml +69 -0
  41. data/spec/fixtures/responses/list_segments/with_success/responds_with_a_list_of_segments.yml +81 -0
  42. data/spec/fixtures/responses/list_segments/with_success/responds_with_success.yml +81 -0
  43. data/spec/fixtures/responses/schedule_messages/unauthorized/responds_with_unauthorize.yml +67 -0
  44. data/spec/fixtures/responses/schedule_messages/with_success/responds_with_created.yml +70 -0
  45. data/spec/fixtures/responses/schedule_messages/with_success/responds_with_success_message.yml +70 -0
  46. data/spec/fixtures/responses/send_messages/unauthorized/responds_with_unauthorized.yml +66 -0
  47. data/spec/fixtures/responses/send_messages/with_success/responds_with_created.yml +69 -0
  48. data/spec/fixtures/responses/send_messages/with_success/responds_with_success_message.yml +69 -0
  49. data/spec/fixtures/responses/track_users/unauthorized/responds_with_unauthorized.yml +68 -0
  50. data/spec/fixtures/responses/track_users/with_success/responds_with_created.yml +71 -0
  51. data/spec/fixtures/responses/track_users/with_success/responds_with_success_message.yml +71 -0
  52. data/spec/integrations/email_status_spec.rb +36 -0
  53. data/spec/integrations/export_users_spec.rb +27 -0
  54. data/spec/integrations/list_segments_spec.rb +19 -0
  55. data/spec/integrations/schedule_messages_spec.rb +31 -0
  56. data/spec/integrations/send_messages_spec.rb +30 -0
  57. data/spec/integrations/track_users_spec.rb +35 -0
  58. data/spec/spec_helper.rb +29 -0
  59. data/spec/support/factory_bot.rb +10 -0
  60. data/spec/support/integrations.rb +20 -0
  61. data/spec/support/vcr.rb +16 -0
  62. metadata +312 -0
@@ -0,0 +1,22 @@
1
+ module BrazeRuby
2
+ module REST
3
+ class EmailStatus < Base
4
+ attr_reader :api_key, :email, :status
5
+
6
+ def initialize(api_key, braze_url, email: nil, status: nil)
7
+ @api_key = api_key
8
+ @email = email
9
+ @status = status
10
+ super braze_url
11
+ end
12
+
13
+ def perform
14
+ http.post '/email/status', {
15
+ 'api_key': api_key,
16
+ 'email': email,
17
+ 'subscription_state': status
18
+ }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ module BrazeRuby
2
+ module REST
3
+ class ExportUsers < Base
4
+ def perform(api_key, external_ids: nil, segment_id: nil, **options)
5
+ return export_users_by_ids(api_key, external_ids) if external_ids
6
+
7
+ export_users_by_segment(api_key, segment_id, options) if segment_id
8
+ end
9
+
10
+ private
11
+
12
+ def export_users_by_ids(api_key, external_ids)
13
+ http.post '/users/export/ids', {
14
+ 'api_key': api_key,
15
+ 'external_ids': external_ids
16
+ }
17
+ end
18
+
19
+ def export_users_by_segment(api_key, segment_id, options)
20
+ http.post '/users/export/segment', {
21
+ 'api_key': api_key,
22
+ 'segment_id': segment_id
23
+ }.merge(options)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ module BrazeRuby
2
+ module REST
3
+ class ListSegments < Base
4
+ def perform(api_key)
5
+ http.get '/segments/list', {
6
+ 'api_key': api_key
7
+ }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ module BrazeRuby
2
+ module REST
3
+ class ScheduleMessages < Base
4
+ attr_reader :api_key, :time, :messages, :in_local_time, :external_user_ids
5
+
6
+ def initialize(api_key, braze_url, time: nil, messages: [], external_user_ids: [], in_local_time: false)
7
+ @api_key = api_key
8
+ @messages = messages
9
+ @time = time
10
+ @external_user_ids = external_user_ids
11
+ @in_local_time = in_local_time
12
+ super braze_url
13
+ end
14
+
15
+ def perform
16
+ http.post '/messages/schedule/create', {
17
+ 'api_key': api_key,
18
+ 'external_user_ids': external_user_ids,
19
+ 'schedule': {
20
+ 'time': time,
21
+ 'in_local_time': in_local_time
22
+ },
23
+ 'messages': messages
24
+ }
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ module BrazeRuby
2
+ module REST
3
+ class SendMessages < Base
4
+ attr_reader :api_key, :messages, :external_user_ids
5
+
6
+ def initialize(api_key, braze_url, messages: [], external_user_ids: [])
7
+ @api_key = api_key
8
+ @messages = messages
9
+ @external_user_ids = external_user_ids
10
+ super braze_url
11
+ end
12
+
13
+ def perform
14
+ http.post '/messages/send', {
15
+ 'api_key': api_key,
16
+ 'messages': messages,
17
+ 'external_user_ids': external_user_ids
18
+ }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module BrazeRuby
2
+ module REST
3
+ class TrackUsers < Base
4
+ def perform(api_key, attributes: [], events: [], purchases: [])
5
+ http.post '/users/track', {
6
+ 'api_key': api_key,
7
+ 'attributes': attributes,
8
+ 'events': events,
9
+ 'purchases': purchases
10
+ }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module BrazeRuby
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe BrazeRuby::API do
4
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ class API
4
+ include BrazeRuby::Endpoints::TrackUsers
5
+
6
+ def api_key
7
+ :api_key
8
+ end
9
+ end
10
+
11
+ describe BrazeRuby::Endpoints::TrackUsers do
12
+ let(:api) { API.new }
13
+ let(:track_users_service) { double(:track_users_service) }
14
+
15
+ before { api.track_users_service = track_users_service }
16
+
17
+ describe '#track_users' do
18
+ let(:payload) {{
19
+ attributes: [build(:attribute)],
20
+ events: [build(:event)],
21
+ purchases: [build(:purchase)]
22
+ }}
23
+
24
+ subject(:track_users!) { api.track_users(payload) }
25
+
26
+ it 'tracks attributes, events and purchases' do
27
+ expect(track_users_service).to receive(:perform)
28
+ .with(:api_key, payload)
29
+
30
+ track_users!
31
+ end
32
+ end
33
+
34
+ describe '#track_purchase' do
35
+ let(:payload) { build(:purchase) }
36
+
37
+ subject(:track_purchase!) { api.track_purchase(payload) }
38
+
39
+ it 'tracks a single purchase' do
40
+ expect(track_users_service).to receive(:perform)
41
+ .with(:api_key, purchases: [payload])
42
+
43
+ track_purchase!
44
+ end
45
+ end
46
+
47
+ describe '#track_event' do
48
+ let(:payload) { build(:event) }
49
+
50
+ subject(:track_event!) { api.track_event(payload) }
51
+
52
+ it 'tracks a single purchase' do
53
+ expect(track_users_service).to receive(:perform)
54
+ .with(:api_key, events: [payload])
55
+
56
+ track_event!
57
+ end
58
+ end
59
+
60
+ describe '#track_attribute' do
61
+ let(:payload) { build(:attribute) }
62
+
63
+ subject(:track_attribute!) { api.track_attribute(payload) }
64
+
65
+ it 'tracks a single purchase' do
66
+ expect(track_users_service).to receive(:perform)
67
+ .with(:api_key, attributes: [payload])
68
+
69
+ track_attribute!
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe BrazeRuby::REST::EmailStatus do
4
+ let(:http) { double(:http) }
5
+
6
+ before { subject.http = http }
7
+
8
+ subject { described_class.new(:api_key, :rest_url, email: :email, status: :status) }
9
+
10
+ it 'makes an http call to the email status endpoint' do
11
+ expect(http).to receive(:post).with '/email/status', {
12
+ api_key: :api_key,
13
+ email: :email,
14
+ subscription_state: :status
15
+ }
16
+
17
+ subject.perform
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe BrazeRuby::REST::ExportUsers do
4
+ let(:http) { double(:http) }
5
+
6
+ let(:payload) {{ external_ids: external_ids }}
7
+ let(:external_ids) { [1] }
8
+
9
+ subject { described_class.new :rest_url }
10
+
11
+ before { subject.http = http }
12
+
13
+ it 'makes an http call to the track user endpoint' do
14
+ expect(http).to receive(:post).with '/users/export/ids',
15
+ payload.merge({ api_key: :api_key })
16
+
17
+ subject.perform(:api_key, payload)
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe BrazeRuby::REST::ScheduleMessages do
4
+ let(:http) { double(:http) }
5
+
6
+ let(:payload) {{
7
+ external_user_ids: :external_user_ids,
8
+ time: :time,
9
+ in_local_time: :in_local_time,
10
+ messages: :messages
11
+ }}
12
+
13
+ let(:api_key) { :api_key }
14
+
15
+ subject { described_class.new(api_key, :rest_url, payload) }
16
+
17
+ before { subject.http = http }
18
+
19
+ it 'makes an http call to the schedule messages endpoint' do
20
+ expect_schedule_messages_http_call
21
+
22
+ subject.perform
23
+ end
24
+
25
+ def expect_schedule_messages_http_call
26
+ expect(http).to receive(:post).with '/messages/schedule/create', {
27
+ api_key: api_key,
28
+ external_user_ids: :external_user_ids,
29
+ schedule: {
30
+ time: :time,
31
+ in_local_time: :in_local_time
32
+ },
33
+ messages: :messages
34
+ }
35
+ end
36
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe BrazeRuby::REST::SendMessages do
4
+ let(:http) { double(:http) }
5
+
6
+ let(:payload) {{
7
+ messages: :messages,
8
+ external_user_ids: :external_user_ids,
9
+ }}
10
+
11
+ let(:api_key) { :api_key }
12
+
13
+ subject { described_class.new(api_key,
14
+ messages: :messages,
15
+ external_user_ids: :external_user_ids
16
+ ) }
17
+
18
+ before { subject.http = http }
19
+
20
+ it 'makes an http call to the send messages endpoint' do
21
+ expect_send_messages_http_call
22
+
23
+ subject.perform
24
+ end
25
+
26
+ def expect_send_messages_http_call
27
+ expect(http).to receive(:post).with '/messages/send', {
28
+ api_key: :api_key,
29
+ messages: [],
30
+ external_user_ids: [],
31
+ }
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe BrazeRuby::REST::TrackUsers do
4
+ let(:http) { double(:http) }
5
+
6
+ let(:payload) {{
7
+ attributes: :attributes,
8
+ events: :events,
9
+ purchases: :purchases
10
+ }}
11
+
12
+ let(:api_key) { :api_key }
13
+
14
+ subject { described_class.new :rest_url}
15
+
16
+ before { subject.http = http }
17
+
18
+ it 'makes an http call to the track user endpoint' do
19
+ expect(http).to receive(:post).with '/users/track',
20
+ payload.merge({ api_key: :api_key })
21
+
22
+ subject.perform(api_key, payload)
23
+ end
24
+ end
@@ -0,0 +1,36 @@
1
+ FactoryBot.define do
2
+ factory :attribute, class: Hash do
3
+ skip_create
4
+ external_id 1
5
+ foo :bar
6
+
7
+ initialize_with { attributes }
8
+ end
9
+
10
+ factory :event, class: Hash do
11
+ skip_create
12
+ external_id 1
13
+ name :baz
14
+ time Time.now
15
+
16
+ initialize_with { attributes }
17
+ end
18
+
19
+ factory :purchase, class: Hash do
20
+ skip_create
21
+ external_id 1
22
+ product_id 1
23
+ time Time.now
24
+ currency 'CAD'
25
+ price 1.0
26
+
27
+ initialize_with { attributes }
28
+ end
29
+
30
+ factory :messages, class: Hash do
31
+ skip_create
32
+ apple_push({ alert: :hello })
33
+
34
+ initialize_with { attributes }
35
+ end
36
+ end
@@ -0,0 +1,69 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: "<BRAZE_REST_URL>/email/status"
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"api_key":"<BRAZE_REST_API_KEY>","email":"john@example.com","subscription_state":"unsubscribed"}'
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.14.0
12
+ Content-Type:
13
+ - application/json
14
+ Accept-Encoding:
15
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
16
+ Accept:
17
+ - "*/*"
18
+ response:
19
+ status:
20
+ code: 201
21
+ message: Created
22
+ headers:
23
+ Cache-Control:
24
+ - max-age=0, private, must-revalidate
25
+ Content-Type:
26
+ - application/json
27
+ Etag:
28
+ - W/"838a7c62adda8d131d694ae13ba2c5b7"
29
+ Server:
30
+ - nginx
31
+ Strict-Transport-Security:
32
+ - max-age=0; includeSubDomains
33
+ - max-age=31536000; includeSubDomains
34
+ X-Ratelimit-Limit:
35
+ - '50000'
36
+ X-Ratelimit-Remaining:
37
+ - '49999'
38
+ X-Ratelimit-Reset:
39
+ - '1524002400'
40
+ X-Request-Id:
41
+ - 24433bbb-89d6-4bd3-918f-46e7865d383e
42
+ X-Runtime:
43
+ - '0.034502'
44
+ Content-Length:
45
+ - '46'
46
+ Accept-Ranges:
47
+ - bytes
48
+ Date:
49
+ - Tue, 17 Apr 2018 21:08:19 GMT
50
+ Via:
51
+ - 1.1 varnish
52
+ Connection:
53
+ - keep-alive
54
+ X-Served-By:
55
+ - cache-pdk17830-PDK
56
+ X-Cache:
57
+ - MISS
58
+ X-Cache-Hits:
59
+ - '0'
60
+ X-Timer:
61
+ - S1523999299.770713,VS0,VE230
62
+ Vary:
63
+ - Accept-Encoding
64
+ body:
65
+ encoding: ASCII-8BIT
66
+ string: '{"message":"success"}'
67
+ http_version:
68
+ recorded_at: Tue, 17 Apr 2018 21:08:19 GMT
69
+ recorded_with: VCR 4.0.0