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.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +157 -0
- data/Rakefile +1 -0
- data/braze_ruby.gemspec +33 -0
- data/lib/braze_ruby.rb +9 -0
- data/lib/braze_ruby/api.rb +31 -0
- data/lib/braze_ruby/deprecated.rb +19 -0
- data/lib/braze_ruby/endpoints.rb +4 -0
- data/lib/braze_ruby/endpoints/email_status.rb +13 -0
- data/lib/braze_ruby/endpoints/schedule_messages.rb +15 -0
- data/lib/braze_ruby/endpoints/send_messages.rb +15 -0
- data/lib/braze_ruby/endpoints/track_users.rb +29 -0
- data/lib/braze_ruby/http.rb +30 -0
- data/lib/braze_ruby/rest.rb +7 -0
- data/lib/braze_ruby/rest/base.rb +19 -0
- data/lib/braze_ruby/rest/email_status.rb +22 -0
- data/lib/braze_ruby/rest/export_users.rb +27 -0
- data/lib/braze_ruby/rest/list_segments.rb +11 -0
- data/lib/braze_ruby/rest/schedule_messages.rb +28 -0
- data/lib/braze_ruby/rest/send_messages.rb +22 -0
- data/lib/braze_ruby/rest/track_users.rb +14 -0
- data/lib/braze_ruby/version.rb +3 -0
- data/spec/braze_ruby/api_spec.rb +4 -0
- data/spec/braze_ruby/endpoints/track_users_spec.rb +72 -0
- data/spec/braze_ruby/rest/email_status_spec.rb +19 -0
- data/spec/braze_ruby/rest/export_users_spec.rb +19 -0
- data/spec/braze_ruby/rest/schedule_messages_spec.rb +36 -0
- data/spec/braze_ruby/rest/send_messages_spec.rb +33 -0
- data/spec/braze_ruby/rest/track_users_spec.rb +24 -0
- data/spec/factories.rb +36 -0
- data/spec/fixtures/responses/email_status/existing_email/responds_with_created.yml +69 -0
- data/spec/fixtures/responses/email_status/existing_email/responds_with_success_message.yml +69 -0
- data/spec/fixtures/responses/email_status/unknown_email/responds_with_bad_request.yml +69 -0
- data/spec/fixtures/responses/email_status/unknown_email/responds_with_success_message.yml +69 -0
- data/spec/fixtures/responses/export_users/by_ids/with_success/responds_with_created.yml +69 -0
- data/spec/fixtures/responses/export_users/by_segment/with_success/responds_with_created.yml +69 -0
- data/spec/fixtures/responses/list_segments/with_success/responds_with_a_list_of_segments.yml +81 -0
- data/spec/fixtures/responses/list_segments/with_success/responds_with_success.yml +81 -0
- data/spec/fixtures/responses/schedule_messages/unauthorized/responds_with_unauthorize.yml +67 -0
- data/spec/fixtures/responses/schedule_messages/with_success/responds_with_created.yml +70 -0
- data/spec/fixtures/responses/schedule_messages/with_success/responds_with_success_message.yml +70 -0
- data/spec/fixtures/responses/send_messages/unauthorized/responds_with_unauthorized.yml +66 -0
- data/spec/fixtures/responses/send_messages/with_success/responds_with_created.yml +69 -0
- data/spec/fixtures/responses/send_messages/with_success/responds_with_success_message.yml +69 -0
- data/spec/fixtures/responses/track_users/unauthorized/responds_with_unauthorized.yml +68 -0
- data/spec/fixtures/responses/track_users/with_success/responds_with_created.yml +71 -0
- data/spec/fixtures/responses/track_users/with_success/responds_with_success_message.yml +71 -0
- data/spec/integrations/email_status_spec.rb +36 -0
- data/spec/integrations/export_users_spec.rb +27 -0
- data/spec/integrations/list_segments_spec.rb +19 -0
- data/spec/integrations/schedule_messages_spec.rb +31 -0
- data/spec/integrations/send_messages_spec.rb +30 -0
- data/spec/integrations/track_users_spec.rb +35 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/support/factory_bot.rb +10 -0
- data/spec/support/integrations.rb +20 -0
- data/spec/support/vcr.rb +16 -0
- 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,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,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
|
data/spec/factories.rb
ADDED
@@ -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
|