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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4c0406b29e745be8bd5e98d2353edb96e5753c12
4
+ data.tar.gz: ff30b73feb4b87a7bc02ad38a8b4b359490c2427
5
+ SHA512:
6
+ metadata.gz: e22f67115caaa99d694cae39b5e2c930c352d037939313ce7ba82426c4197c094588305d4f3fa51329d3aefbc2177f1addcd07ea1dbd0a3403c43e97e995b931
7
+ data.tar.gz: 54ae4986cfe73d3dddff37450bf947187aaf4b36ebbb7585b9050c6ecc34d753e6e439eaeac1c62ef78ef9bf02f376d169d564836656bd192bb1d091c0aaeefb
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .ruby-version
7
+ .env
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in braze_ruby.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Josh Nussbaum
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,157 @@
1
+ # BrazeRuby
2
+
3
+ A wrapper for the Braze REST API. Forked from https://github.com/DynamoMTL/appboy
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'braze_ruby'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install braze_ruby
18
+
19
+ ## Examples
20
+
21
+ ### Initializing API
22
+
23
+ ```ruby
24
+ api = BrazeRuby::API.new('<braze-rest-api-key>', '<braze-rest-api-url')
25
+ ```
26
+
27
+ ### Track User Attributes
28
+
29
+ ```ruby
30
+ api.track_users(attributes: [{
31
+ external_id: 123,
32
+ first_name: 'John',
33
+ last_name: 'Smith',
34
+ gender: 'male',
35
+ email: 'jsmith@example.com'
36
+ }])
37
+ ```
38
+
39
+ ##### Track Attributes for Single User
40
+
41
+ ```ruby
42
+ api.track_attribute(external_id: 123, first_name: 'John', ...)
43
+ ```
44
+
45
+ ### Track Event
46
+
47
+ ```ruby
48
+ api.track_users(events: [{
49
+ external_id: 123,
50
+ name: 'add-to-cart',
51
+ time: Time.now
52
+ }])
53
+ ```
54
+
55
+ ##### Track Events for Single User
56
+
57
+ ```ruby
58
+ api.track_event(external_id: 123, name: 'Event', ...)
59
+ ```
60
+
61
+ ### Track Purchase
62
+
63
+ ```ruby
64
+ api.track_users(purchases: [{
65
+ external_id: 123,
66
+ product_id: 456,
67
+ currency: 'CAD',
68
+ price: 1.99,
69
+ time: Time.now
70
+ }])
71
+ ```
72
+
73
+ ##### Track Purchases for Single User
74
+
75
+ ```ruby
76
+ api.track_purchase(external_id: 123, product_id: 456, ...)
77
+ ```
78
+
79
+ ### Track Everything for Everyone All at Once
80
+
81
+ ```ruby
82
+ api.track_users(purchases: purchases, events: events, attributes: attributes)
83
+ ```
84
+
85
+ ### Send Message
86
+
87
+ ##### Messages Payload
88
+
89
+ ```ruby
90
+ messages = {
91
+ android_push: { alert: 'Hello Android' },
92
+ apple_push: { alert: "Hello iOS" }
93
+ }
94
+ ```
95
+
96
+ ##### Option A, Using External User IDs
97
+
98
+ ```ruby
99
+ api.send_messages(messages: messages, external_user_ids: [123, 456])
100
+ ```
101
+
102
+ ##### Option B, Using Segment ID
103
+
104
+ ```ruby
105
+ api.send_messages(messages: messages, segment_id: '<segment-id>')
106
+ ```
107
+
108
+
109
+ ### Schedule Message
110
+
111
+ ```ruby
112
+ api.schedule_messages(send_at: 1.hour.since, messages: messages, external_user_ids: [123, 456])
113
+ ```
114
+
115
+ ### Changing Email Subscription
116
+
117
+ ```ruby
118
+ api.email_status(email: 'john@example.com', status: :opted_in)
119
+ ```
120
+
121
+ ### List Segments
122
+
123
+ ```ruby
124
+ api.list_segments
125
+ ```
126
+
127
+ ### Export Users
128
+
129
+ #### By IDs
130
+
131
+ ```ruby
132
+ api.export_users(external_ids: [1])
133
+ ```
134
+
135
+ #### By Segment
136
+
137
+ ```ruby
138
+ api.export_users(segment_id: segment_id, callback_endpoint: 'https://example.com')
139
+ ```
140
+
141
+ ## Debugging
142
+
143
+ The BRAZE_RUBY_DEBUG environment variable will trigger full printouts of the Faraday gem's HTTP requests and responses.
144
+
145
+ ```bash
146
+ cd /my/app
147
+ export BRAZE_RUBY_DEBUG=true
148
+ bundle exec rails whatever
149
+ ```
150
+
151
+ ## Contributing
152
+
153
+ 1. Fork it
154
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
155
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
156
+ 4. Push to the branch (`git push origin my-new-feature`)
157
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'braze_ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'braze_ruby'
8
+ spec.version = BrazeRuby::VERSION
9
+ spec.authors = ['Josh Nussbaum', 'Hugo Bastien', 'Justin Boltz']
10
+ spec.email = %w(josh@godynamo.com hugo@godynamo.com justin.boltz@takl.com)
11
+ spec.description = %q{Braze wrapper}
12
+ spec.summary = %q{A wrapper for the Braze REST API forked from the appboy gem, track users/events/purchases, send & schedule messages}
13
+ spec.homepage = 'http://braze.com'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'virtus'
22
+ spec.add_dependency 'faraday'
23
+ spec.add_dependency 'faraday_middleware'
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rspec'
27
+ spec.add_development_dependency 'dotenv'
28
+ spec.add_development_dependency 'vcr'
29
+ spec.add_development_dependency 'webmock'
30
+ spec.add_development_dependency 'pry'
31
+ spec.add_development_dependency 'activesupport'
32
+ spec.add_development_dependency 'factory_bot'
33
+ end
@@ -0,0 +1,9 @@
1
+ require 'json'
2
+ require 'virtus'
3
+ require 'braze_ruby/version'
4
+
5
+ module BrazeRuby
6
+ autoload :API, 'braze_ruby/api'
7
+ autoload :REST, 'braze_ruby/rest'
8
+ autoload :Endpoints, 'braze_ruby/endpoints'
9
+ end
@@ -0,0 +1,31 @@
1
+ require 'braze_ruby/deprecated'
2
+ require 'braze_ruby/endpoints/track_users'
3
+ require 'braze_ruby/endpoints/send_messages'
4
+ require 'braze_ruby/endpoints/schedule_messages'
5
+ require 'braze_ruby/endpoints/email_status'
6
+
7
+ module BrazeRuby
8
+ class API
9
+ include BrazeRuby::Deprecated
10
+
11
+ include BrazeRuby::Endpoints::TrackUsers
12
+ include BrazeRuby::Endpoints::SendMessages
13
+ include BrazeRuby::Endpoints::ScheduleMessages
14
+ include BrazeRuby::Endpoints::EmailStatus
15
+
16
+ def export_users(**payload)
17
+ BrazeRuby::REST::ExportUsers.new(braze_url).perform(api_key, payload)
18
+ end
19
+
20
+ def list_segments
21
+ BrazeRuby::REST::ListSegments.new(braze_url).perform(api_key)
22
+ end
23
+
24
+ attr_reader :api_key, :braze_url
25
+
26
+ def initialize(api_key, braze_url)
27
+ @api_key = api_key
28
+ @braze_url = braze_url
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module BrazeRuby
2
+ module Deprecated
3
+ def track(attribute, events = [], purchases = [])
4
+ warn('[BrazeRuby] `track` is deprecated. Please use `track_users` instead.')
5
+ track_users(attribute: attribute, events: events, purchases: purchases)
6
+ end
7
+
8
+ def send_message(message, user_ids, segment_ids = nil)
9
+ warn('[BrazeRuby] `send_message` is deprecated. Please use `send_messages` instead.')
10
+ send_messages(messages: message, user_ids: user_ids, segment_ids: segment_ids)
11
+ end
12
+
13
+ def schedule_message(date, message, segment_id, local_timezone = false)
14
+ warn('[BrazeRuby] `schedule_message` is deprecated. Please use `schedule_messages` instead.')
15
+ schedule_messages(send_at: date, messages: message,
16
+ segment_id: segment_id, local_timezone: local_timezone)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ require 'braze_ruby/endpoints/email_status'
2
+ require 'braze_ruby/endpoints/schedule_messages'
3
+ require 'braze_ruby/endpoints/send_messages'
4
+ require 'braze_ruby/endpoints/track_users'
@@ -0,0 +1,13 @@
1
+ module BrazeRuby
2
+ module Endpoints
3
+ module EmailStatus
4
+ def email_status(**payload)
5
+ email_status_service.new(api_key, braze_url, payload).perform
6
+ end
7
+
8
+ def email_status_service
9
+ BrazeRuby::REST::EmailStatus
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module BrazeRuby
2
+ module Endpoints
3
+ module ScheduleMessages
4
+ def schedule_messages(**payload)
5
+ schedule_messages_service.new(api_key, braze_url, payload).perform
6
+ end
7
+
8
+ private
9
+
10
+ def schedule_messages_service
11
+ BrazeRuby::REST::ScheduleMessages
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module BrazeRuby
2
+ module Endpoints
3
+ module SendMessages
4
+ def send_messages(**payload)
5
+ send_messages_service.new(api_key, braze_url, payload).perform
6
+ end
7
+
8
+ private
9
+
10
+ def send_messages_service
11
+ BrazeRuby::REST::SendMessages
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ module BrazeRuby
2
+ module Endpoints
3
+ module TrackUsers
4
+ attr_writer :track_users_service
5
+
6
+ def track_users(**payload)
7
+ track_users_service.perform(api_key, payload)
8
+ end
9
+
10
+ def track_purchase(payload)
11
+ track_users(purchases: [payload])
12
+ end
13
+
14
+ def track_event(payload)
15
+ track_users(events: [payload])
16
+ end
17
+
18
+ def track_attribute(payload)
19
+ track_users(attributes: [payload])
20
+ end
21
+
22
+ private
23
+
24
+ def track_users_service
25
+ @track_users_service ||= BrazeRuby::REST::TrackUsers.new(braze_url)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module BrazeRuby
5
+ class HTTP
6
+ def initialize(braze_url)
7
+ @braze_url = braze_url
8
+ end
9
+
10
+ def post(path, payload)
11
+ connection.post path do |request|
12
+ request.body = payload
13
+ end
14
+ end
15
+
16
+ def get(path, query)
17
+ connection.get path, query
18
+ end
19
+
20
+ def connection
21
+ @connection ||= Faraday.new(url: @braze_url) do |connection|
22
+ connection.request :json
23
+
24
+ connection.response :logger if ENV['BRAZE_RUBY_DEBUG']
25
+
26
+ connection.adapter Faraday.default_adapter
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ require 'braze_ruby/rest/base'
2
+ require 'braze_ruby/rest/track_users'
3
+ require 'braze_ruby/rest/send_messages'
4
+ require 'braze_ruby/rest/schedule_messages'
5
+ require 'braze_ruby/rest/email_status'
6
+ require 'braze_ruby/rest/export_users'
7
+ require 'braze_ruby/rest/list_segments'
@@ -0,0 +1,19 @@
1
+ require 'braze_ruby/http'
2
+
3
+ module BrazeRuby
4
+ module REST
5
+ class Base
6
+ attr_writer :http
7
+
8
+ def initialize(braze_url)
9
+ @braze_url = braze_url
10
+ end
11
+
12
+ private
13
+
14
+ def http
15
+ @http ||= BrazeRuby::HTTP.new(@braze_url)
16
+ end
17
+ end
18
+ end
19
+ end