comufy 0.0.2.pre2 → 0.0.3pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -2
- data/example/comufy_example.rb +4 -6
- data/lib/comufy/config.rb +18 -31
- data/lib/comufy/constants.rb +15 -0
- data/lib/comufy/version.rb +2 -2
- data/lib/comufy.rb +776 -11
- data/spec/comufy_spec.rb +18 -8
- data/yaml/config.yaml +2 -0
- metadata +20 -9
- data/lib/comufy/connector.rb +0 -774
data/lib/comufy.rb
CHANGED
@@ -1,7 +1,3 @@
|
|
1
|
-
require "comufy/version"
|
2
|
-
require "comufy/config"
|
3
|
-
require "comufy/connector"
|
4
|
-
|
5
1
|
require 'yaml'
|
6
2
|
require 'json'
|
7
3
|
require 'net/http'
|
@@ -9,15 +5,784 @@ require 'net/https'
|
|
9
5
|
require 'cgi'
|
10
6
|
require 'logger'
|
11
7
|
|
12
|
-
|
8
|
+
class Comufy
|
13
9
|
|
14
|
-
|
15
|
-
|
16
|
-
|
10
|
+
attr_reader :config, :logger
|
11
|
+
|
12
|
+
autoload :Constants, "comufy/constants"
|
13
|
+
autoload :Version, "comufy/version"
|
14
|
+
autoload :Config, "comufy/config"
|
17
15
|
|
18
16
|
# Based on Rails implementation, ensures all strings are converted
|
19
|
-
# into symbols.
|
20
|
-
def symbolize_keys hash
|
21
|
-
hash.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
|
17
|
+
# into symbols. (Non-recursive!)
|
18
|
+
def self.symbolize_keys hash
|
19
|
+
hash.each_with_object( {} ) { |(k,v), h| h[k.to_sym] = v }
|
22
20
|
end
|
21
|
+
|
22
|
+
# There are a number of options you can pass to change default settings.
|
23
|
+
#
|
24
|
+
# With regards to the server to connect to, if you pass <tt>staging: true</tt> it'll use the Comufy
|
25
|
+
# staging server, otherwise it'll default to using the social server.
|
26
|
+
#
|
27
|
+
# Concerning authentication, there are a number of ways to connect to the Comufy servers. You can
|
28
|
+
# pass in a user name and password in the parameters, provide the location of a YAML file where these are
|
29
|
+
# specified or read in the <tt>access_token</tt> from your environment path. Please note that you can stop
|
30
|
+
# the connector from reading your environment path by setting <tt>no_env</tt> to true. When doing this please
|
31
|
+
# ensure your user name and password are correct, otherwise you'll not be able to connect to the Comufy servers.
|
32
|
+
#
|
33
|
+
# The YAML file provided should appear as:
|
34
|
+
# user: user
|
35
|
+
# password: password
|
36
|
+
#
|
37
|
+
# OR
|
38
|
+
#
|
39
|
+
# token: token
|
40
|
+
# time: expiry_time
|
41
|
+
#
|
42
|
+
# = Example
|
43
|
+
#
|
44
|
+
# # set the user and password yourself
|
45
|
+
# Comufy.new(user: YOUR_USERNAME, password: YOUR_PASSWORD)
|
46
|
+
#
|
47
|
+
# # set the token and expiry time
|
48
|
+
# Comufy.new(token: YOUR_TOKEN, time: YOUR_EXPIRY_TIME)
|
49
|
+
#
|
50
|
+
# # Or you can read in from a YAML file!
|
51
|
+
#
|
52
|
+
# # tell the connector where to find the yaml file to read
|
53
|
+
# Comufy.new(yaml: PATH_TO_YAML_FILE)
|
54
|
+
def initialize opts = {}
|
55
|
+
opts = Comufy.symbolize_keys(opts)
|
56
|
+
|
57
|
+
@logger = Logger.new(STDOUT)
|
58
|
+
@logger.level = case opts[:logger]
|
59
|
+
when "info" then
|
60
|
+
Logger::INFO
|
61
|
+
when "warn" then
|
62
|
+
Logger::WARN
|
63
|
+
when "debug" then
|
64
|
+
Logger::DEBUG
|
65
|
+
else
|
66
|
+
Logger::WARN
|
67
|
+
end
|
68
|
+
|
69
|
+
# sanitize all output
|
70
|
+
original_formatter = Logger::Formatter.new
|
71
|
+
@logger.formatter = proc { |severity, datetime, progname, msg|
|
72
|
+
original_formatter.call(severity, datetime, progname, msg.dump)
|
73
|
+
}
|
74
|
+
|
75
|
+
@config = Config.new(opts)
|
76
|
+
end
|
77
|
+
|
78
|
+
# This API call allows you to register a Facebook user of your application into Comufy’s social CRM.
|
79
|
+
# If this user was already registered with Comufy, their information will be updated.
|
80
|
+
#
|
81
|
+
# * (String) +app_name+ - The application you'll be adding the user to.
|
82
|
+
# * (String) +uid+ - The Facebook ID of the user you'll be adding.
|
83
|
+
# * (Hash) +tags+ - The tags you'll setting for this user.
|
84
|
+
# * (String) +tag_name+ - Must correspond to one of the tag names of the application.
|
85
|
+
# * (String) +value+ - Must be the correct value type for that tag.
|
86
|
+
#
|
87
|
+
# = Example
|
88
|
+
#
|
89
|
+
# Comufy.store_users(YOUR_APPLICATION_NAME, USER_FACEBOOK_ID, { 'dob' => '1978-10-01 19:50:48' })
|
90
|
+
def store_user app_name, uid, tags
|
91
|
+
return false unless get_access_token
|
92
|
+
if app_name.nil? or app_name.empty?
|
93
|
+
@logger.warn(progname = 'Comufy.store_user') {
|
94
|
+
'First parameter must be set to your application name.'
|
95
|
+
}
|
96
|
+
return false
|
97
|
+
end
|
98
|
+
if uid.nil? or uid.empty?
|
99
|
+
@logger.warn(progname = 'Comufy.store_user') {
|
100
|
+
'Second parameter must be a valid Facebook user ID.'
|
101
|
+
}
|
102
|
+
return false
|
103
|
+
end
|
104
|
+
if tags.nil? or not tags.is_a?(Hash)
|
105
|
+
@logger.warn(progname = 'Comufy.store_user') {
|
106
|
+
'Third parameter must be a hash of tag information for this uid.'
|
107
|
+
}
|
108
|
+
return false
|
109
|
+
end
|
110
|
+
|
111
|
+
data = {
|
112
|
+
#token: @config.access_token,
|
113
|
+
cd: '88',
|
114
|
+
applicationName: app_name,
|
115
|
+
accounts: [{
|
116
|
+
account: { fbId: uid },
|
117
|
+
tags: tags
|
118
|
+
}]
|
119
|
+
}
|
120
|
+
|
121
|
+
message = call_api(data)
|
122
|
+
if message
|
123
|
+
case message['cd']
|
124
|
+
when 388 then
|
125
|
+
@logger.debug(progname = 'Comufy.store_user') {
|
126
|
+
"388 - Success! - data = #{data} - message = #{message}."
|
127
|
+
}
|
128
|
+
return true
|
129
|
+
when 475 then
|
130
|
+
@logger.debug(progname = 'Comufy.store_user') {
|
131
|
+
"475 - Invalid parameter provided. - data = #{data} - message = #{message}."
|
132
|
+
}
|
133
|
+
@logger.warn(progname = 'Comufy.store_user') {
|
134
|
+
'475 - Invalid parameter provided.'
|
135
|
+
}
|
136
|
+
when 617 then
|
137
|
+
@logger.debug(progname = 'Comufy.store_user') {
|
138
|
+
"617 - Some of the tags passed are not registered. - data = #{data} - message = #{message}."
|
139
|
+
}
|
140
|
+
@logger.warn(progname = 'Comufy.store_user') {
|
141
|
+
'617 - Some of the tags passed are not registered.'
|
142
|
+
}
|
143
|
+
when 632 then
|
144
|
+
@logger.debug(progname = 'Comufy.store_user') {
|
145
|
+
"632 - _ERROR_FACEBOOK_PAGE_NOT_FOUND - data = #{data} - message = #{message}."
|
146
|
+
}
|
147
|
+
@logger.warn(progname = 'Comufy.store_user') {
|
148
|
+
'632 - _ERROR_FACEBOOK_PAGE_NOT_FOUND'
|
149
|
+
}
|
150
|
+
else
|
151
|
+
@logger.debug(progname = 'Comufy.store_user') {
|
152
|
+
"UNKNOWN RESPONSE - data = #{data} - message = #{message}."
|
153
|
+
}
|
154
|
+
@logger.warn(progname = 'Comufy.store_user') {
|
155
|
+
"An error occurred when sending #{data}. Comufy returned #{message}. Please get in touch with Comufy if you cannot resolve the problem."
|
156
|
+
}
|
157
|
+
end
|
158
|
+
else
|
159
|
+
@logger.debug(progname = 'Comufy.store_user') {
|
160
|
+
"Authentication failed - data = #{data}."
|
161
|
+
}
|
162
|
+
@logger.warn(progname = 'Comufy.store_user') {
|
163
|
+
"Authentication failed when sending #{data}. Please get in touch with Comufy if you cannot resolve the problem."
|
164
|
+
}
|
165
|
+
end
|
166
|
+
false
|
167
|
+
end
|
168
|
+
|
169
|
+
# See store_user for details
|
170
|
+
def add_user app_name, uid, tags
|
171
|
+
store_user app_name, uid, tags
|
172
|
+
end
|
173
|
+
|
174
|
+
# See store_user for details
|
175
|
+
def update_user app_name, uid, tags
|
176
|
+
store_user app_name, uid, tags
|
177
|
+
end
|
178
|
+
|
179
|
+
## Method not implemented
|
180
|
+
#def remove_user app_name, uid # :nodoc:
|
181
|
+
# return false unless get_access_token
|
182
|
+
# @logger.debug(progname = 'Comufy.remove_user') {
|
183
|
+
# 'METHOD_NOT_IMPLEMENTED'
|
184
|
+
# }
|
185
|
+
# @logger.warn(progname = 'Comufy.remove_user') {
|
186
|
+
# 'METHOD_NOT_IMPLEMENTED'
|
187
|
+
# }
|
188
|
+
# false
|
189
|
+
#end
|
190
|
+
|
191
|
+
#
|
192
|
+
# This API call allows you to register multiple Facebook users of your application into Comufy’s social CRM.
|
193
|
+
# If these users were already registered into Comufy, their information will be updated.
|
194
|
+
#
|
195
|
+
# * (String) +app_name+ - The application you wish to store these users with.
|
196
|
+
# * (Hash) +uid_tags+ - The users you wish to add with their corresponding tag data.
|
197
|
+
# * (String) +uid+ - The key is the Facebook ID of the user.
|
198
|
+
# * (Hash) +tags+ - The value is the tags of data to apply for that user.
|
199
|
+
#
|
200
|
+
# = Example
|
201
|
+
#
|
202
|
+
# Comufy.store_users(
|
203
|
+
# YOUR_APPLICATION_NAME,
|
204
|
+
# { USER_ID => { 'dob' => '1978-10-01 19:50:48' }, OTHER_USER_ID => { 'dob' => '1978-10-01 19:50:48'}}
|
205
|
+
# )
|
206
|
+
def store_users app_name, uid_tags
|
207
|
+
return false unless get_access_token
|
208
|
+
if app_name.nil? or app_name.empty?
|
209
|
+
@logger.warn(progname = 'Comufy.store_users') {
|
210
|
+
'First parameter must be set to your application name.'
|
211
|
+
}
|
212
|
+
return false
|
213
|
+
end
|
214
|
+
if uid_tags.nil? or not uid_tags.is_a?(Hash)
|
215
|
+
@logger.warn(progname = 'Comufy.store_users') {
|
216
|
+
'Second parameter must be a hash where a key is a Facebook user ID and its value a hash of tags.'
|
217
|
+
}
|
218
|
+
return false
|
219
|
+
end
|
220
|
+
|
221
|
+
data = {
|
222
|
+
#token: @config.access_token,
|
223
|
+
cd: '88',
|
224
|
+
applicationName: app_name,
|
225
|
+
accounts: uid_tags.map { |uid, tags| Hash[:account, { fbId: uid }, :tags, tags] }
|
226
|
+
}
|
227
|
+
|
228
|
+
message = call_api(data)
|
229
|
+
if message
|
230
|
+
case message['cd']
|
231
|
+
when 388 then
|
232
|
+
@logger.debug(progname = 'Comufy.store_users') {
|
233
|
+
"388 - Success! - data = #{data} - message = #{message}."
|
234
|
+
}
|
235
|
+
return true
|
236
|
+
when 475 then
|
237
|
+
@logger.debug(progname = 'Comufy.store_users') {
|
238
|
+
"603 - Invalid parameter provided. - data = #{data} - message = #{message}."
|
239
|
+
}
|
240
|
+
@logger.warn(progname = 'Comufy.store_users') {
|
241
|
+
'475 - Invalid parameter provided.'
|
242
|
+
}
|
243
|
+
when 617 then
|
244
|
+
@logger.debug(progname = 'Comufy.store_users') {
|
245
|
+
"617 - Some of the tags passed are not registered. - data = #{data} - message = #{message}."
|
246
|
+
}
|
247
|
+
@logger.warn(progname = 'Comufy.store_users') {
|
248
|
+
'617 - Some of the tags passed are not registered.'
|
249
|
+
}
|
250
|
+
when 632 then
|
251
|
+
@logger.debug(progname = 'Comufy.store_users') {
|
252
|
+
"632 - _ERROR_FACEBOOK_PAGE_NOT_FOUND - data = #{data} - message = #{message}."
|
253
|
+
}
|
254
|
+
@logger.warn(progname = 'Comufy.store_users') {
|
255
|
+
'632 - _ERROR_FACEBOOK_PAGE_NOT_FOUND'
|
256
|
+
}
|
257
|
+
else
|
258
|
+
@logger.debug(progname = 'Comufy.store_users') {
|
259
|
+
"UNKNOWN RESPONSE - data = #{data} - message = #{message}."
|
260
|
+
}
|
261
|
+
@logger.warn(progname = 'Comufy.store_users') {
|
262
|
+
"An error occurred when sending #{data}. Comufy returned #{message}. Please get in touch with Comufy if you cannot resolve the problem."
|
263
|
+
}
|
264
|
+
end
|
265
|
+
else
|
266
|
+
@logger.debug(progname = 'Comufy.store_users') {
|
267
|
+
"Authentication failed - data = #{data}."
|
268
|
+
}
|
269
|
+
@logger.warn(progname = 'Comufy.store_users') {
|
270
|
+
"Authentication failed when sending #{data}. Please get in touch with Comufy if you cannot resolve the problem."
|
271
|
+
}
|
272
|
+
end
|
273
|
+
false
|
274
|
+
end
|
275
|
+
|
276
|
+
|
277
|
+
# Registering a Facebook application tag allows you to store data-fields about each one of your customers.
|
278
|
+
#
|
279
|
+
# * (String) +app_name+ - The application you wish to register the tags with.
|
280
|
+
# * (Hash) +tag+ - The tag you wish to register
|
281
|
+
# * (String) +name+ - The name for the tag.
|
282
|
+
# * (String) +type+ - Must be one of the following: STRING, DATE, GENDER, INT, FLOAT.
|
283
|
+
#
|
284
|
+
# = Example
|
285
|
+
# Comufy.register_tag(YOUR_APPLICATION_NAME, name: 'dob', type: Connector::DATE_TYPE)
|
286
|
+
def register_tag app_name, tag = {}
|
287
|
+
register_tags(app_name, Array[tag])
|
288
|
+
end
|
289
|
+
|
290
|
+
# Registering a Facebook application tag allows you to store data-fields about each one of your customers.
|
291
|
+
#
|
292
|
+
# * (String) +app_name+ - The application you wish to register the tags with.
|
293
|
+
# * (Array) +tags+ - The tags you wish to register, each of which must be a (Hash) containing two keys.
|
294
|
+
# * (String) +name+ - The name for the tag.
|
295
|
+
# * (String) +type+ - Must be one of the following: STRING, DATE, GENDER, INT, FLOAT.
|
296
|
+
#
|
297
|
+
# = Example
|
298
|
+
# Comufy.register_tags(
|
299
|
+
# YOUR_APPLICATION_NAME,
|
300
|
+
# [{
|
301
|
+
# Connector::NAME_TAG: 'dob',
|
302
|
+
# Connector::TYPE_TAG: Connector::DATE_TYPE
|
303
|
+
# },
|
304
|
+
# {
|
305
|
+
# Connector::NAME_TAG: 'height',
|
306
|
+
# Connector::TYPE_TAG: Connector::FLOAT_TYPE
|
307
|
+
# }]
|
308
|
+
# )
|
309
|
+
def register_tags app_name, tags
|
310
|
+
return false unless get_access_token
|
311
|
+
if app_name.nil? or app_name.empty?
|
312
|
+
@logger.warn(progname = 'Comufy.register_tags') {
|
313
|
+
'First parameter must be set to your application name.'
|
314
|
+
}
|
315
|
+
return false
|
316
|
+
end
|
317
|
+
if tags.nil? or not tags.is_a?(Array)
|
318
|
+
@logger.warn(progname = 'Comufy.register_tags') {
|
319
|
+
'Second parameter must be an array containing hashes.'
|
320
|
+
}
|
321
|
+
return false
|
322
|
+
end
|
323
|
+
tags.each do |tag|
|
324
|
+
tag.each do |key, value|
|
325
|
+
unless Constants::LEGAL_TAGS.include?(key)
|
326
|
+
@logger.warn(progname = 'Comufy.register_tags') {
|
327
|
+
"You must have only two keys called #{Constants::NAME_TAG} and #{Constants::TYPE_TAG}."
|
328
|
+
}
|
329
|
+
return false
|
330
|
+
end
|
331
|
+
if (key == "type" or key == :type) and not Constants::LEGAL_TYPES.include?(value)
|
332
|
+
@logger.warn(progname = 'Comufy.register_tags') {
|
333
|
+
"Your type must be one of these values: #{Constants::LEGAL_TYPES.join(',')}."
|
334
|
+
}
|
335
|
+
return false
|
336
|
+
end
|
337
|
+
end
|
338
|
+
end
|
339
|
+
|
340
|
+
data = {
|
341
|
+
#token: @config.access_token,
|
342
|
+
tags: tags,
|
343
|
+
cd: 86,
|
344
|
+
applicationName: app_name
|
345
|
+
}
|
346
|
+
|
347
|
+
message = call_api(data)
|
348
|
+
if message
|
349
|
+
case message['cd']
|
350
|
+
when 386 then
|
351
|
+
@logger.debug(progname = 'Comufy.register_tags') {
|
352
|
+
"386 - Success! - data = #{data} - message = #{message}."
|
353
|
+
}
|
354
|
+
return true
|
355
|
+
when 475 then
|
356
|
+
@logger.debug(progname = 'Comufy.register_tags') {
|
357
|
+
"475 - Invalid parameters provided - data = #{data} - message = #{message}."
|
358
|
+
}
|
359
|
+
@logger.warn(progname = 'Comufy.register_tags') {
|
360
|
+
'475 - Invalid parameters provided'
|
361
|
+
}
|
362
|
+
when 603 then
|
363
|
+
@logger.debug(progname = 'Comufy.register_tags') {
|
364
|
+
"603 - _ERROR_DOMAIN_APPLICATION_NAME_NOT_FOUND - data = #{data} - message = #{message}."
|
365
|
+
}
|
366
|
+
@logger.warn(progname = 'Comufy.register_tags') {
|
367
|
+
'603 - _ERROR_DOMAIN_APPLICATION_NAME_NOT_FOUND'
|
368
|
+
}
|
369
|
+
when 607 then
|
370
|
+
@logger.debug(progname = 'Comufy.register_tags') {
|
371
|
+
"607 - _ERROR_UNAUTHORISED_ACTION - data = #{data} - message = #{message}."
|
372
|
+
}
|
373
|
+
@logger.warn(progname = 'Comufy.register_tags') {
|
374
|
+
'607 - _ERROR_UNAUTHORISED_ACTION'
|
375
|
+
}
|
376
|
+
when 618 then
|
377
|
+
@logger.debug(progname = 'Comufy.register_tags') {
|
378
|
+
"618 - _ERROR_DOMAIN_APPLICATION_TAG_ALREADY_REGISTERED - data = #{data} - message = #{message}."
|
379
|
+
}
|
380
|
+
@logger.warn(progname = 'Comufy.register_tags') {
|
381
|
+
'618 - _ERROR_DOMAIN_APPLICATION_TAG_ALREADY_REGISTERED'
|
382
|
+
}
|
383
|
+
else
|
384
|
+
@logger.debug(progname = 'Comufy.register_tags') {
|
385
|
+
"UNKNOWN RESPONSE - data = #{data} - message = #{message}."
|
386
|
+
}
|
387
|
+
@logger.warn(progname = 'Comufy.register_tags') {
|
388
|
+
"An error occurred when sending #{data}. Comufy returned #{message}. Please get in touch with Comufy if you cannot resolve the problem."
|
389
|
+
}
|
390
|
+
end
|
391
|
+
else
|
392
|
+
@logger.debug(progname = 'Comufy.register_tags') {
|
393
|
+
"Authentication failed - data = #{data}."
|
394
|
+
}
|
395
|
+
@logger.warn(progname = 'Comufy.register_tags') {
|
396
|
+
"Authentication failed when sending #{data}. Please get in touch with Comufy if you cannot resolve the problem."
|
397
|
+
}
|
398
|
+
end
|
399
|
+
false
|
400
|
+
end
|
401
|
+
|
402
|
+
#
|
403
|
+
# This API call will unregister an existing application tag. All data associated with the tag will be lost.
|
404
|
+
#
|
405
|
+
# * (String) +app_name+ - The application on which to remove the tag.
|
406
|
+
# * (String) +tag+ - The tag to remove from the user.
|
407
|
+
#
|
408
|
+
# = Example
|
409
|
+
# Comufy.unregister_tag(YOUR_APPLICATION_NAME, 'dob')
|
410
|
+
def unregister_tag app_name, tag
|
411
|
+
return false unless get_access_token
|
412
|
+
if app_name.nil? or app_name.empty?
|
413
|
+
@logger.warn(progname = 'Comufy.unregister_tag') {
|
414
|
+
'First parameter must be set to your application name.'
|
415
|
+
}
|
416
|
+
return false
|
417
|
+
end
|
418
|
+
if tag.nil? or tag.empty?
|
419
|
+
@logger.warn(progname = 'Comufy.unregister_tag') {
|
420
|
+
'Second parameter must be set to the tag.'
|
421
|
+
}
|
422
|
+
return false
|
423
|
+
end
|
424
|
+
|
425
|
+
data = {
|
426
|
+
#token: @config.access_token,
|
427
|
+
tag: tag,
|
428
|
+
cd: 85,
|
429
|
+
applicationName: app_name
|
430
|
+
}
|
431
|
+
|
432
|
+
message = call_api(data)
|
433
|
+
if message
|
434
|
+
case message['cd']
|
435
|
+
when 385 then
|
436
|
+
@logger.debug(progname = 'Comufy.unregister_tag') {
|
437
|
+
"385 - Success! - data = #{data} - message = #{message}."
|
438
|
+
}
|
439
|
+
return true
|
440
|
+
when 475 then
|
441
|
+
@logger.debug(progname = 'Comufy.unregister_tag') {
|
442
|
+
"475 - Invalid parameters provided - data = #{data} - message = #{message}."
|
443
|
+
}
|
444
|
+
@logger.warn(progname = 'Comufy.unregister_tag') {
|
445
|
+
'475 - Invalid parameters provided'
|
446
|
+
}
|
447
|
+
when 603 then
|
448
|
+
@logger.debug(progname = 'Comufy.unregister_tag') {
|
449
|
+
"603 - _ERROR_DOMAIN_APPLICATION_NAME_NOT_FOUND - data = #{data} - message = #{message}."
|
450
|
+
}
|
451
|
+
@logger.warn(progname = 'Comufy.unregister_tag') {
|
452
|
+
'603 - _ERROR_DOMAIN_APPLICATION_NAME_NOT_FOUND'
|
453
|
+
}
|
454
|
+
when 607 then
|
455
|
+
@logger.debug(progname = 'Comufy.unregister_tag') {
|
456
|
+
"607 - _ERROR_UNAUTHORISED_ACTION - data = #{data} - message = #{message}."
|
457
|
+
}
|
458
|
+
@logger.warn(progname = 'Comufy.unregister_tag') {
|
459
|
+
'607 - _ERROR_UNAUTHORISED_ACTION'
|
460
|
+
}
|
461
|
+
when 617 then
|
462
|
+
@logger.debug(progname = 'Comufy.unregister_tag') {
|
463
|
+
"617 - _ERROR_DOMAIN_APPLICATION_TAG_NOT_FOUND - data = #{data} - message = #{message}."
|
464
|
+
}
|
465
|
+
@logger.warn(progname = 'Comufy.unregister_tag') {
|
466
|
+
'617 - _ERROR_DOMAIN_APPLICATION_TAG_NOT_FOUND'
|
467
|
+
}
|
468
|
+
else
|
469
|
+
@logger.debug(progname = 'Comufy.unregister_tag') {
|
470
|
+
"UNKNOWN RESPONSE - data = #{data} - message = #{message}."
|
471
|
+
}
|
472
|
+
@logger.warn(progname = 'Comufy.unregister_tag') {
|
473
|
+
"An error occurred when sending #{data}. Comufy returned #{message}. Please get in touch with Comufy if you cannot resolve the problem."
|
474
|
+
}
|
475
|
+
end
|
476
|
+
else
|
477
|
+
@logger.debug(progname = 'Comufy.unregister_tag') {
|
478
|
+
"Authentication failed - data = #{data}."
|
479
|
+
}
|
480
|
+
@logger.warn(progname = 'Comufy.unregister_tag') {
|
481
|
+
"Authentication failed when sending #{data}. Please get in touch with Comufy if you cannot resolve the problem."
|
482
|
+
}
|
483
|
+
end
|
484
|
+
false
|
485
|
+
end
|
486
|
+
|
487
|
+
# Sends a message with the description and content to the facebook id or id's specified, allowing multiple
|
488
|
+
# options to be set concerning the privacy, and content of the message.
|
489
|
+
#
|
490
|
+
# * (String) +app_name+ - The application through the message is sent.
|
491
|
+
# * (String) +description+ - Description of the message. Useful to aggregate data in the Comufy dashboard. e.g. "Welcome".
|
492
|
+
# * (String) +content+ - The text message content.
|
493
|
+
# * (Array) +uids+ - The Facebook IDs of the users to send the message to.
|
494
|
+
# * (Hash) +opts+ - Optional settings you can pass.
|
495
|
+
# * (Integer) +delivery_time+ - The scheduled time of delivery defaults to now. (Unix millisecond timestamps)
|
496
|
+
# * (Boolean) +shorten_urls+ - UNTRACKED if false, otherwise defaults to Comufy TRACKED
|
497
|
+
# * (String) +filter+ - filtering condition in CFL.
|
498
|
+
# * (Hash) +message_options+ - options to set for the message especially.
|
499
|
+
# * (String) +name+ - facebook message name.
|
500
|
+
# * (String) +link+ - Facebook message link.
|
501
|
+
# * (String) +caption+ - facebook message caption.
|
502
|
+
# * (String) +description+ - description of the message.
|
503
|
+
# * (String) +picture+ - URL of the image that should appear on the image section of the message.
|
504
|
+
# * (Boolean) +privacy+ - whether the message should be sent private or not.
|
505
|
+
#
|
506
|
+
# = Example
|
507
|
+
# Comufy.send_facebook_message(
|
508
|
+
# YOUR_APPLICATION_NAME, DESCRIPTION, CONTENT_GOES_HERE, %w(ID_ONE ID_TWO),
|
509
|
+
# message_options: {
|
510
|
+
# private: true, link: 'www.example.com', name: 'test', description: 'description'
|
511
|
+
# }
|
512
|
+
# )
|
513
|
+
def send_facebook_message app_name, description, content, uids, opts = {}
|
514
|
+
return false unless get_access_token
|
515
|
+
if app_name.nil? or app_name.empty? or not content.is_a?(String)
|
516
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
517
|
+
'First parameter must be set to your application name, as a String.'
|
518
|
+
}
|
519
|
+
return false
|
520
|
+
end
|
521
|
+
if description.nil? or description.empty? or not content.is_a?(String)
|
522
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
523
|
+
'Second parameter must be set to your facebook description, as a String.'
|
524
|
+
}
|
525
|
+
return false
|
526
|
+
end
|
527
|
+
if content.nil? or content.empty? or not content.is_a?(String)
|
528
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
529
|
+
'Third parameter must be sent to your facebook content, as a String.'
|
530
|
+
}
|
531
|
+
return false
|
532
|
+
end
|
533
|
+
if uids.nil? or uids.empty? or not uids.is_a?(Array)
|
534
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
535
|
+
'Fourth parameter must be sent to your facebook uids, as an Array of Strings.'
|
536
|
+
}
|
537
|
+
return false
|
538
|
+
end
|
539
|
+
|
540
|
+
# symbolize the keys!
|
541
|
+
opts = Comufy.symbolize_keys(opts)
|
542
|
+
|
543
|
+
# optional checks
|
544
|
+
if opts.has_key?(:filter) and not opts[:filter].is_a?(String)
|
545
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
546
|
+
'When including "filter", it must be a String.'
|
547
|
+
}
|
548
|
+
return false
|
549
|
+
end
|
550
|
+
if opts.has_key?(:delivery_time) and not opts[:delivery_time].is_a?(Integer)
|
551
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
552
|
+
'When including "delivery_time", it must be an Integer, of unix time in milliseconds.'
|
553
|
+
}
|
554
|
+
return false
|
555
|
+
end
|
556
|
+
if opts.has_key?(:shorten_urls) and not %w[ true, false ].include?(opts[:shorten_urls])
|
557
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
558
|
+
'When including "shorten_urls", it must be an boolean value.'
|
559
|
+
}
|
560
|
+
return false
|
561
|
+
end
|
562
|
+
if opts.has_key?(:message_options) and not opts[:message_options].is_a?(Hash)
|
563
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
564
|
+
'When including "message_options", it must be a Hash.'
|
565
|
+
}
|
566
|
+
return false
|
567
|
+
end
|
568
|
+
|
569
|
+
facebook_ids = "FACEBOOK_ID=\"#{uids.join('\" OR FACEBOOK_ID=\"')}\""
|
570
|
+
|
571
|
+
filter = opts[:filter] || String.new()
|
572
|
+
delivery_time = opts[:delivery_time]
|
573
|
+
shorten_urls = opts.has_key?(:shorten_urls) ? opts[:shorten_urls] : true
|
574
|
+
options = opts[:message_options]
|
575
|
+
|
576
|
+
data = {
|
577
|
+
cd: 83,
|
578
|
+
applicationName: app_name,
|
579
|
+
description: description,
|
580
|
+
content: content,
|
581
|
+
filter: "#{facebook_ids} #{filter}"
|
582
|
+
}
|
583
|
+
|
584
|
+
data[:deliveryTime] = delivery_time if delivery_time
|
585
|
+
data[:trackingMode] = "UNTRACKED" unless shorten_urls
|
586
|
+
data[:facebookTargetingMode] = "NOTIFICATION"
|
587
|
+
|
588
|
+
if options
|
589
|
+
data[:fbMessagePrivacyMode] = options[:private] ? "PRIVATE" : "PUBLIC" if options.has_key?(:private)
|
590
|
+
data[:fbMessageCaption] = options[:caption] if options.has_key?(:caption)
|
591
|
+
data[:fbMessageLink] = options[:link] if options.has_key?(:link)
|
592
|
+
data[:fbMessageName] = options[:name] if options.has_key?(:name)
|
593
|
+
data[:fbMessageDescription] = options[:description] if options.has_key?(:description)
|
594
|
+
data[:fbMessagePictureUrl] = options[:picture] if options.has_key?(:picture)
|
595
|
+
end
|
596
|
+
|
597
|
+
message = call_api(data)
|
598
|
+
if message
|
599
|
+
case message['cd']
|
600
|
+
when 383 then
|
601
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
602
|
+
"383 - Success! - data = #{data} - message = #{message}."
|
603
|
+
}
|
604
|
+
return true
|
605
|
+
when 416 then
|
606
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
607
|
+
"416 - _ERROR_MSG_SEND_FAILED - data = #{data} - message = #{message}."
|
608
|
+
}
|
609
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
610
|
+
'416 - _ERROR_MSG_SEND_FAILED'
|
611
|
+
}
|
612
|
+
when 475 then
|
613
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
614
|
+
"475 - Invalid parameters provided - data = #{data} - message = #{message}."
|
615
|
+
}
|
616
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
617
|
+
'475 - Invalid parameters provided'
|
618
|
+
}
|
619
|
+
when 551 then
|
620
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
621
|
+
"551 _ERROR_TAG_VALUE_NOT_FOUND - data = #{data} - message = #{message}."
|
622
|
+
}
|
623
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
624
|
+
'551 - _ERROR_TAG_VALUE_NOT_FOUND'
|
625
|
+
}
|
626
|
+
when 603 then
|
627
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
628
|
+
"603 - _ERROR_DOMAIN_APPLICATION_NAME_NOT_FOUND - data = #{data} - message = #{message}."
|
629
|
+
}
|
630
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
631
|
+
'603 - _ERROR_DOMAIN_APPLICATION_NAME_NOT_FOUND'
|
632
|
+
}
|
633
|
+
when 607 then
|
634
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
635
|
+
"607 - _ERROR_UNAUTHORISED_ACTION - data = #{data} - message = #{message}."
|
636
|
+
}
|
637
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
638
|
+
'607 - _ERROR_UNAUTHORISED_ACTION'
|
639
|
+
}
|
640
|
+
when 617 then
|
641
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
642
|
+
"617 - _ERROR_DOMAIN_APPLICATION_TAG_NOT_FOUND - data = #{data} - message = #{message}."
|
643
|
+
}
|
644
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
645
|
+
'617 - _ERROR_DOMAIN_APPLICATION_TAG_NOT_FOUND'
|
646
|
+
}
|
647
|
+
when 648 then
|
648
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
649
|
+
"648 - _ERROR_FACEBOOK_APPLICATION_USER_NOT_FOUND - data = #{data} - message = #{message}."
|
650
|
+
}
|
651
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
652
|
+
'648 - _ERROR_FACEBOOK_APPLICATION_USER_NOT_FOUND'
|
653
|
+
}
|
654
|
+
when 673 then
|
655
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
656
|
+
"673 - Invalid time exception - data = #{data} - message = #{message}."
|
657
|
+
}
|
658
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
659
|
+
'673 - Invalid time exception'
|
660
|
+
}
|
661
|
+
when 679 then
|
662
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
663
|
+
"679 - _ERROR_MALFORMED_TARGETING_EXPRESSION - data = #{data} - message = #{message}."
|
664
|
+
}
|
665
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
666
|
+
'679 - _ERROR_MALFORMED_TARGETING_EXPRESSION'
|
667
|
+
}
|
668
|
+
else
|
669
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
670
|
+
"UNKNOWN RESPONSE - data = #{data} - message = #{message}."
|
671
|
+
}
|
672
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
673
|
+
"An error occurred when sending #{data}. Comufy returned #{message}. Please get in touch with Comufy if you cannot resolve the problem."
|
674
|
+
}
|
675
|
+
end
|
676
|
+
else
|
677
|
+
@logger.debug(progname = 'Comufy.send_facebook_message') {
|
678
|
+
"Authentication failed - data = #{data}."
|
679
|
+
}
|
680
|
+
@logger.warn(progname = 'Comufy.send_facebook_message') {
|
681
|
+
"Authentication failed when sending #{data}. Please get in touch with Comufy if you cannot resolve the problem."
|
682
|
+
}
|
683
|
+
end
|
684
|
+
false
|
685
|
+
end
|
686
|
+
|
687
|
+
private
|
688
|
+
|
689
|
+
# Use the configured user name and password to get and set the access token and expiry time, if it fails,
|
690
|
+
# it means the user likely has their user name/password wrong.
|
691
|
+
def authenticate
|
692
|
+
data = {
|
693
|
+
cd: 131,
|
694
|
+
user: @config.user,
|
695
|
+
password: @config.password
|
696
|
+
}
|
697
|
+
|
698
|
+
message = call_api(data, false)
|
699
|
+
case message['cd']
|
700
|
+
when 235 then
|
701
|
+
@logger.debug(progname = 'Comufy.authenticate') {
|
702
|
+
"235 - Success! - data = #{data} - message = #{message}."
|
703
|
+
}
|
704
|
+
@config.access_token = message['tokenInfo']['token']
|
705
|
+
@config.expiry_time = message['tokenInfo']['expiryTime']
|
706
|
+
return true
|
707
|
+
when 475 then
|
708
|
+
@logger.debug(progname = 'Comufy.authenticate') {
|
709
|
+
"475 - Invalid parameters provided. - data = #{data} - message = #{message}."
|
710
|
+
}
|
711
|
+
@logger.warn(progname = 'Comufy.authenticate') {
|
712
|
+
'475 - Invalid parameters provided.'
|
713
|
+
}
|
714
|
+
when 504 then
|
715
|
+
@logger.debug(progname = 'Comufy.authenticate') {
|
716
|
+
"504 - FIX. - data = #{data} - message = #{message}."
|
717
|
+
}
|
718
|
+
@logger.warn(progname = 'Comufy.authenticate') {
|
719
|
+
'504 - FIX.'
|
720
|
+
}
|
721
|
+
when 651 then
|
722
|
+
@logger.debug(progname = 'Comufy.authenticate') {
|
723
|
+
"651 - Invalid user name exception. Check that you are login in using the format user@domain. - data = #{data} - message = #{message}."
|
724
|
+
}
|
725
|
+
@logger.warn(progname = 'Comufy.authenticate') {
|
726
|
+
'651 - Invalid user name exception. Check that you are login in using the format user@domain.'
|
727
|
+
}
|
728
|
+
when 652 then
|
729
|
+
@logger.debug(progname = 'Comufy.authenticate') {
|
730
|
+
"652 - Invalid password exception. - data = #{data} - message = #{message}."
|
731
|
+
}
|
732
|
+
@logger.warn(progname = 'Comufy.authenticate') {
|
733
|
+
'652 - Invalid password exception.'
|
734
|
+
}
|
735
|
+
when 682 then
|
736
|
+
@logger.debug(progname = 'Comufy.authenticate') {
|
737
|
+
"682 - This user is blocked. - data = #{data} - message = #{message}."
|
738
|
+
}
|
739
|
+
@logger.warn(progname = 'Comufy.authenticate') {
|
740
|
+
'682 - This user is blocked.'
|
741
|
+
}
|
742
|
+
else
|
743
|
+
@logger.debug(progname = 'Comufy.authenticate') {
|
744
|
+
"UNKNOWN RESPONSE - data = #{data} - message = #{message}."
|
745
|
+
}
|
746
|
+
@logger.warn(progname = 'Comufy.authenticate') {
|
747
|
+
"An error occurred when sending #{data}. Comufy returned #{message}. Please get in touch with Comufy if you cannot resolve the problem."
|
748
|
+
}
|
749
|
+
end
|
750
|
+
# an issue occurred, reset the access token and expiry time.
|
751
|
+
@config.access_token = nil
|
752
|
+
@config.expiry_time = nil
|
753
|
+
false
|
754
|
+
end
|
755
|
+
|
756
|
+
# Calls the Comufy backed with the provided set of parameters, which the system will expect
|
757
|
+
#
|
758
|
+
# * (Array) +params+ - Data to be passed to the server.
|
759
|
+
# * (Boolean) +add_access_token+ - (Optional) Whether or not the access token should be provided.
|
760
|
+
def call_api data, add_access_token=true
|
761
|
+
if add_access_token
|
762
|
+
return nil if not get_access_token
|
763
|
+
data[:token] = @config.access_token
|
764
|
+
end
|
765
|
+
|
766
|
+
uri = URI.parse(@config::base_api_url)
|
767
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
768
|
+
#http.use_ssl = true # Only used when directly accessing Comufy!
|
769
|
+
request = Net::HTTP::Post.new(uri.path, initheader = { 'Content-Type' => 'application/json' })
|
770
|
+
request.set_form_data({ request: data.to_json })
|
771
|
+
response = http.request(request)
|
772
|
+
JSON.parse(response.read_body) if response.message == 'OK'
|
773
|
+
end
|
774
|
+
|
775
|
+
# Checks that the token is not expired, and if expired, force an authentication.
|
776
|
+
def get_access_token
|
777
|
+
has_token_expired ? authenticate : true
|
778
|
+
end
|
779
|
+
|
780
|
+
# If the expiry time is set, and hasn't been reached, return false, otherwise
|
781
|
+
# reset the access_token and expiry time,
|
782
|
+
def has_token_expired
|
783
|
+
return false if @config.expiry_time != nil and Time.at(@config.expiry_time) > Time.now
|
784
|
+
@config.expiry_time = nil
|
785
|
+
@config.access_token = nil
|
786
|
+
true
|
787
|
+
end
|
23
788
|
end
|