comufy 0.0.2b

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