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