outbound 0.1.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.
Files changed (2) hide show
  1. data/lib/outbound.rb +183 -0
  2. metadata +64 -0
data/lib/outbound.rb ADDED
@@ -0,0 +1,183 @@
1
+ require 'logger'
2
+
3
+ module Outbound
4
+ VERSION = '0.1.0'
5
+ BASE_URL = 'https://api.outbound.io/v2'
6
+
7
+ ERROR_USER_ID = "User ID must be a string or number."
8
+ ERROR_EVENT_NAME = "Event name must be a string."
9
+ ERROR_CONNECTION = "Outbound connection error"
10
+ ERROR_INIT = "Must call init() before identify() or track()."
11
+
12
+ @ob = nil
13
+ @logger = Logger.new $stdout
14
+ @logger.progname = "Outbound"
15
+ @logger.level = Logger::ERROR
16
+
17
+ module Defaults
18
+ HEADERS = {
19
+ 'Content-type' => 'application/json',
20
+ 'X-Outbound-Client' => 'ruby',
21
+ 'X-Outbound-Client-Version' => Outbound::VERSION,
22
+ }
23
+ end
24
+
25
+ def Outbound.init api_key, log_level=Logger::ERROR
26
+ @logger.level = log_level
27
+ @ob = Outbound::Client.new api_key, @logger
28
+ end
29
+
30
+ def Outbound.identify user_id, info={}, attributes={}
31
+ if @ob == nil
32
+ res = Result.new Outbound::ERROR_INIT, false
33
+ @logger.error res.error
34
+ return res
35
+ end
36
+
37
+ return @ob.identify user_id, info, attributes
38
+ end
39
+
40
+ def Outbound.track user_id, event, properties={}, user_info={}, user_attributes={}
41
+ if @ob == nil
42
+ res = Result.new Outbound::ERROR_INIT, false
43
+ @logger.error res.error
44
+ return res
45
+ end
46
+ return @ob.track user_id, event, properties, user_info, user_attributes
47
+ end
48
+
49
+ class Result
50
+ include Defaults
51
+
52
+ def initialize error, received_call
53
+ @error = error
54
+ @received_call = received_call
55
+ end
56
+
57
+ attr_accessor :error
58
+ attr_accessor :received_call
59
+
60
+ def success?
61
+ return @received_call && @error == nil
62
+ end
63
+
64
+ def user_id_error?
65
+ return @error == Outbound::ERROR_USER_ID
66
+ end
67
+
68
+ def event_name_error?
69
+ return @error == Outbound::ERROR_EVENT_NAME
70
+ end
71
+
72
+ def connection_error?
73
+ return @error == Outbound::ERROR_CONNECTION
74
+ end
75
+
76
+ def init_error?
77
+ return @error == Outbound::ERROR_INIT
78
+ end
79
+ end
80
+
81
+ class Client
82
+ include Defaults
83
+
84
+ def initialize api_key, logger
85
+ @api_key = api_key
86
+ @logger = logger
87
+ end
88
+
89
+ def identify user_id, info={}, attributes={}
90
+ unless user_id.is_a? String or user_id.is_a? Numeric
91
+ res = Result.new Outbound::ERROR_USER_ID, false
92
+ @logger.error res.error
93
+ return res
94
+ end
95
+
96
+ user_data = {:user_id => user_id}
97
+ begin
98
+ user = user(info, attributes)
99
+ user_data = user_data.merge user
100
+ rescue
101
+ @logger.error "Could not use user info (#{info}) and/or user attributes #{attributes} given to identify call."
102
+ end
103
+
104
+ return post(@api_key, '/identify', user_data)
105
+ end
106
+
107
+ def track user_id, event, properties={}, user_info={}, user_attributes={}
108
+ unless user_id.is_a? String or user_id.is_a? Numeric
109
+ res = Result.new Outbound::ERROR_USER_ID, false
110
+ @logger.error res.error
111
+ return res
112
+ end
113
+
114
+ unless event.is_a? String
115
+ res = Result.new Outbound::ERROR_EVENT_NAME, false
116
+ @logger.error res.error
117
+ return res
118
+ end
119
+
120
+ data = {:user_id => user_id, :event => event}
121
+
122
+ begin
123
+ user = user(user_info, user_attributes)
124
+ if user.length > 0
125
+ data[:user] = user
126
+ end
127
+ rescue
128
+ @logger.error "Could not use user info (#{user_info}) and/or user attributes #{user_attributes} given to track call."
129
+ end
130
+
131
+ if properties.is_a? Hash
132
+ if properties.length > 0
133
+ data[:properties] = properties
134
+ end
135
+ else
136
+ @logger.error "Could not use event properties (#{properties}) given to track call."
137
+ end
138
+
139
+ return post(@api_key, '/track', data)
140
+ end
141
+
142
+ private
143
+
144
+ def post api_key, path, data
145
+ begin
146
+ headers = HEADERS
147
+ headers['X-Outbound-Key'] = api_key
148
+ payload = JSON.generate data
149
+ request = Net::HTTP::Post.new("#{BASE_URL}#{path}", headers)
150
+
151
+ res = @http.request(request, payload)
152
+ status = res.code.to_i
153
+ rescue Exception => e
154
+ res = Result.new Outbound::ERROR_CONNECTION, false
155
+ @logger.error res.error
156
+ return res
157
+ end
158
+
159
+ err = nil
160
+ if status < 200 or status >= 400
161
+ err = "#{status} - #{res.body}" if res.response_body_permitted? else nil
162
+ end
163
+ return err, true
164
+ end
165
+
166
+ def user info={}, attributes={}
167
+ unless info.is_a? Hash and attributes.is_a? Hash
168
+ raise
169
+ end
170
+
171
+ user = {
172
+ :first_name => info[:first_name],
173
+ :last_name => info[:last_name],
174
+ :email => info[:email],
175
+ :phone_number => info[:phone_number],
176
+ :apns_tokens => info[:apns_tokens],
177
+ :gcm_tokens => info[:gcm_tokens],
178
+ :attributes => attributes,
179
+ }
180
+ return user.delete_if { |k, v| v.nil? || v.empty? }
181
+ end
182
+ end
183
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: outbound
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Travis Beauvais
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-07 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'Outbound sends automated email, SMS, phone calls and push notifications
15
+ based on the actions users take or do not take in your app. The Outbound API has
16
+ two components:
17
+
18
+
19
+ Identify each of your users and their attributes using an identify API call.
20
+
21
+ Track the actions that each user takes in your app using a track API call.
22
+
23
+ Because every message is associated with a user (identify call) and a specific trigger
24
+ action that a user took or should take (track call), Outbound is able to keep track
25
+ of how each message affects user actions in your app. These calls also allow you
26
+ to target campaigns and customize each message based on user data.
27
+
28
+
29
+ Example: When a user in San Francisco(user attribute) does signup(event) but does
30
+ not upload a picture(event) within 2 weeks, send them an email about how they''ll
31
+ benefit from uploading a picture.'
32
+ email: support@outbound.io
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/outbound.rb
38
+ homepage: https://outbound.io
39
+ licenses:
40
+ - MIT
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.23
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Outbound sends automated email, SMS, phone calls and push notifications based
63
+ on the actions users take (or do not take) in your app.
64
+ test_files: []