comufyrails 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .idea/
19
+ html/
data/Gemfile CHANGED
@@ -4,3 +4,5 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem 'rails'
7
+ gem 'em-synchrony'
8
+ gem 'em-http-request'
data/README.md CHANGED
@@ -1,24 +1,112 @@
1
1
  # Comufyrails
2
2
 
3
- TODO: Write a gem description
3
+ This gem allows [Ruby on Rails](http://rubyonrails.org/) projects to connect with the
4
+ [Comufy service](http://www.comufy.com/). It uses asynchronous calls and separate rake tasks to communicate with
5
+ the Comufy API, allowing you to create, update your list of users as well as registering new tags and send
6
+ messages to your users.
4
7
 
5
8
  ## Installation
6
9
 
7
- Add this line to your application's Gemfile:
10
+ Add any of the lines to your application's Gemfile:
8
11
 
9
12
  gem 'comufyrails'
13
+ gem 'comufyrails', :git => "git://github.com/plcstevens/comufyrails.git"
14
+ gem 'comufyrails', :path => "/path/to/comufyrails/directory"
10
15
 
11
16
  And then execute:
12
17
 
13
18
  $ bundle
14
19
 
15
- Or install it yourself as:
20
+ Or install it yourself:
16
21
 
17
22
  $ gem install comufyrails
18
23
 
24
+
25
+ ## Web servers
26
+
27
+ As this gem uses EventMachine to perform asynchronous methods you need to use a web server that supports EventMachine,
28
+ such as [thin](http://code.macournoyer.com/thin/).
29
+
30
+ ## Configuration
31
+
32
+ The gem requires configuration before being used. To get these values you must create an account with Comufy through
33
+ our Heroku service, or by yourself.
34
+
35
+ On Heroku you should add the Comufy add-on and follow the configuration steps it gives you. This will automatically
36
+ set the environment variables for your Comufy account that this gem will use. If you are not using our Heroku
37
+ service you will need to find another way to get these values, listed below.
38
+
39
+ If you are using this on your local development machine or elsewhere you have two ways to configure this gem. You
40
+ can get these values by using this heroku command below and looking for all values starting with 'COMUFY_'.
41
+
42
+ heroku config
43
+
44
+ You can set the values in your config/environments/*.rb in the same manner you set rails-specific values.
45
+
46
+ ```ruby
47
+ config.comufy_rails.app_name = 'YOUR_APPLICATION_NAME'
48
+ config.comufy_rails.access_token = 'YOUR_ACCESS_TOKEN'
49
+ config.comufy_rails.url = 'COMUFY'
50
+ ```
51
+
52
+ Alternatively you can set these in your environment/path.
53
+
54
+ ```
55
+ COMUFY_APP_NAME - Application name on Comufy, defaults to your Ruby on Rails application name.
56
+ COMUFY_TOKEN - Token given to you by our Comufy Heroku service or from Comufy directly.
57
+ COMUFY_URL - Full HTTP address to connect to, defaults to our service.
58
+ ```
59
+
19
60
  ## Usage
20
61
 
21
- TODO: Write usage instructions here
62
+ In its current iteration you can use this gem to send information to Comufy, allowing you to add users, update data
63
+ on the users and send messages/notifications to your users via your own service.
64
+
65
+ If you have your own user database, and wish to keep the Comufy database in sync with it, you can use the observer
66
+ behaviour for your model and asynchronously send the data to Comufy.
67
+
68
+ ```ruby
69
+ class UserObserver < ActiveRecord::Observer
70
+
71
+ def after_save(user)
72
+ data = { dob: user.dob.to_comufy_time, fact: user.fact }
73
+ Comufyrails::Connection.store_user(user.facebook_id, data)
74
+ end
75
+ end
76
+ ```
77
+
78
+ Or you can place the code in your controllers. As this method is asynchronous it will not block and affect
79
+ performance. It should be noted that these methods return their results to the logs.
80
+
81
+ ```ruby
82
+ # POST /users
83
+ # POST /users.json
84
+ def create
85
+ @user = User.new(params[:user])
86
+
87
+ respond_to do |format|
88
+ if @user.save
89
+ Comufyrails::Connection.store_user(user.facebook_id, { dob: user.dob.to_comufy_time, fact: user.fact })
90
+ format.html { redirect_to @user, notice: 'User was successfully created.' }
91
+ format.json { render json: @user, status: :created, location: @user }
92
+ else
93
+ format.html { render action: "new" }
94
+ format.json { render json: @user.errors, status: :unprocessable_entity }
95
+ end
96
+ end
97
+ end
98
+ ```
99
+
100
+ There are also a number of methods that are added to your rake environment, for one-time actions. These include
101
+ the ability to add/remove tags for users of your applications.
102
+
103
+ ```bash
104
+ $ bundle exec rake comufy:tag["DOB", "DATE"]
105
+ ```
106
+
107
+ This will run a blocking call to register this tag with your application, informing you if it was successful or not.
108
+ It will use the configuration exactly as your Rails application will, so if you need to run it as production, you
109
+ merely have to add RAILS_ENV=production or -e production.
22
110
 
23
111
  ## Contributing
24
112
 
data/Rakefile CHANGED
@@ -1,7 +1,13 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
+ require 'rdoc/task'
3
4
 
4
5
  RSpec::Core::RakeTask.new
5
6
 
6
7
  task :default => :spec
7
8
  task :test => :spec
9
+
10
+ Rake::RDocTask.new do |rd|
11
+ rd.main = "README.md"
12
+ rd.rdoc_files.include("README.md", "lib/**/*.rb")
13
+ end
data/comufyrails.gemspec CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency('activesupport')
21
+ gem.add_dependency('em-synchrony')
22
+ gem.add_dependency('em-http-request')
21
23
 
22
24
  gem.add_development_dependency 'rake'
23
25
  gem.add_development_dependency 'rspec'
data/lib/comufyrails.rb CHANGED
@@ -1,20 +1,37 @@
1
1
  require 'rails'
2
2
 
3
- require "comufyrails/version"
3
+ require "comufyrails/core_ext"
4
4
  require "comufyrails/connection"
5
+ require "comufyrails/constants"
5
6
  require 'comufyrails/railtie' if defined?(Rails)
7
+ require "comufyrails/version"
6
8
 
7
9
  module Comufyrails
8
10
 
11
+ # Contains the list of constant variables that will be used throughout this gem.
9
12
  class Config
10
- attr_accessor :app_name, :username, :password, :access_token, :expiry_time, :base_api_url
13
+ # Application name on Comufy, and on Facebook.
14
+ attr_accessor :app_name
15
+ # Access token to access your Applications on Comufy.
16
+ attr_accessor :access_token
17
+ # Expiry time of the AccessToken.
18
+ attr_accessor :expiry_time
19
+ # The URL of the Comufy service to connect to.
20
+ attr_accessor :url
11
21
  end
12
22
 
13
- def self.config
14
- @@config ||= Config.new
15
- end
23
+ class << self
24
+ # Comufyrails logger (usually uses the Rails.logger)
25
+ attr_accessor :logger
26
+
27
+ # define as a Config object unless defined
28
+ def config
29
+ @@config ||= Config.new
30
+ end
16
31
 
17
- def self.configure
18
- yield self.config
32
+ # yield the Comufyrails config class variable
33
+ def configure
34
+ yield self.config
35
+ end
19
36
  end
20
37
  end
@@ -1,10 +1,262 @@
1
+ require "em-synchrony"
2
+ require "em-synchrony/em-http"
1
3
 
4
+ # This module contains asynchronous methods for contacting the +comufy.url+ specified in +Config+.
5
+ # It uses +em-synchrony+ and +EventMachine+ to achieve this, and therefore to be run asynchronously you
6
+ # must use a web server that supports these such as +thin+.
7
+ #
8
+ # Methods to store users or send messages all return their results to logs, as they do not provide any information
9
+ # back to the user.
10
+ #
11
+ # Requests for users and tags can return information and will yield to a block if provided, otherwise they will
12
+ # also print to the log. This is often useful for debugging, but in practise you should provide these with a block.
2
13
  module Comufyrails::Connection
3
14
 
4
- # aim of this module is to will be to allow sync/async connections to be sent to our heroku servers
5
- # to be processed and then returned (or the error message returned)
15
+ class IllegalKeyTypeError < StandardError; end
16
+ class IllegalValueTypeError < StandardError; end
17
+
18
+ class << self
19
+
20
+ # Shortened method name for storing a user, or users in your Application.
21
+ # Please see +store_users+ for details.
22
+ def store(uids, tags) self.store_user(uids, tags) end
23
+
24
+ # This API call allows you to register a Facebook user of your application into Comufy’s social CRM.
25
+ # If this user was already registered with Comufy, their information will be updated.
26
+ #
27
+ # * (String) +uid+ - The Facebook ID of the user you'll be adding.
28
+ # * (Hash) +tags+ - The tags you'll setting for this user.
29
+ # * (String) +tag_name+ - Must correspond to one of the tag names of the application.
30
+ # * (String) +value+ - Must be the correct value type for that tag.
31
+ #
32
+ # = Example
33
+ #
34
+ # Comufyrails::Connection.store_user USER_FACEBOOK_ID, { dob: '1978-10-01 19:50:48' }
35
+ def store_user(uids, tags)
36
+ uids = [uids] unless uids.is_a? Array
37
+ tags = [tags] unless tags.is_a? Array
38
+ self.store_users(uids, tags)
39
+ end
40
+
41
+ # This API call allows you to register multiple Facebook users of your application into Comufy’s social CRM.
42
+ # If these users were already registered into Comufy, their information will be updated.
43
+ #
44
+ # Please note that you can provide a single String or Integer for uids and one +tag+ if you wish.
45
+ #
46
+ # * (Array) +uids+ - A list of (String/Integer) user ids you wish to add/update.
47
+ # * (Array) +tags+ - A list of hashes for each of the users.
48
+ # * (Hash) +tag+
49
+ # * (String) +tag_name+ - Must correspond to one of the tag names of the application.
50
+ # * (String) +value+ - Must be the correct value type for that tag.
51
+ #
52
+ # = Example
53
+ #
54
+ # Comufyrails::Connection.store_users(
55
+ # [ USER_ID, USER_ID_2 ],
56
+ # [ { 'dob' => '1978-10-01 19:50:48' }, { 'dob' => '1978-10-01 19:50:48'}]
57
+ # )
58
+ def store_users(uids, tags)
59
+ raise ArgumentError, "uids must be an Array." unless uids.is_a? Array
60
+ raise ArgumentError, "tags must be an Array." unless tags.is_a? Array
61
+
62
+ # Ensure the tags are valid
63
+ tags.each(&:symbolize_keys!)
64
+ zipped = uids.zip(tags)
65
+
66
+ data = {
67
+ cd: '88',
68
+ token: Comufyrails.config.access_token,
69
+ applicationName: Comufyrails.config.app_name,
70
+ accounts: zipped.map { |uid, tagged | Hash[:account, { fbId: uid.to_s }, :tags, tagged] }
71
+ }
72
+ EM.synchrony do
73
+ http = EventMachine::HttpRequest.new(Comufyrails.config.url).post(
74
+ :body => { request: data.to_json }, :initheader => { 'Content-Type' => 'application/json' })
75
+ if http.response_header.status == 200
76
+ message = JSON.parse(http.response)
77
+ case message["cd"]
78
+ when 388 then
79
+ p "388 - Success! - data = #{data} - message = #{message}."
80
+ when 475 then
81
+ p "475 - Invalid parameter provided. - data = #{data} - message = #{message}."
82
+ when 617 then
83
+ p "617 - Some of the tags passed are not registered. - data = #{data} - message = #{message}."
84
+ when 632 then
85
+ p "632 - _ERROR_FACEBOOK_PAGE_NOT_FOUND - data = #{data} - message = #{message}."
86
+ else
87
+ p "UNKNOWN RESPONSE - data = #{data} - message = #{message}."
88
+ end
89
+ else
90
+ p "Server responded with #{http.response_header}."
91
+ end
92
+ end
93
+ end
94
+
95
+ # Shorthand method for sending messages to the selected uids. See +send_facebook_message+ for more information.
96
+ def message(desc, content, uids, opts = {}) self.send_facebook_message(desc, content, uids, opts) end
97
+
98
+ # Sends a message with the description and content to the facebook id or id's specified, allowing multiple
99
+ # options to be set concerning the privacy, and content of the message.
100
+ #
101
+ # * (String) +description+ - Description of the message. Useful to aggregate data in the Comufy dashboard.
102
+ # * (String) +content+ - The text message content.
103
+ # * (Array) +uids+ - The Facebook IDs of the users to send the message to.
104
+ # * (Hash) +opts+ - Optional settings you can pass.
105
+ # * (Integer) +delivery_time+ - The scheduled time of delivery defaults to now. (Unix millisecond timestamps)
106
+ # * (Boolean) +shorten_urls+ - UNTRACKED if false, otherwise defaults to Comufy TRACKED
107
+ # * (String) +filter+ - filtering condition in CFL.
108
+ # * (Hash) +message_options+ - options to set for the message especially.
109
+ # * (String) +name+ - facebook message name.
110
+ # * (String) +link+ - Facebook message link.
111
+ # * (String) +caption+ - facebook message caption.
112
+ # * (String) +description+ - description of the message.
113
+ # * (String) +picture+ - URL of the image that should appear on the image section of the message.
114
+ # * (Boolean) +privacy+ - whether the message should be sent private or not.
115
+ #
116
+ # = Example
117
+ # Comufyrails::Connection.send_facebook_message(
118
+ # DESCRIPTION, CONTENT_GOES_HERE, UIDS,
119
+ # message_options: {
120
+ # private: true, link: 'www.example.com', name: 'test', description: 'description'
121
+ # }
122
+ # )
123
+ def send_facebook_message(description, content, uids, opts = {})
124
+ raise ArgumentError, "Description must be a String." unless description.is_a? String
125
+ raise ArgumentError, "Content must be a String." unless content.is_a? String
126
+ raise ArgumentError, "Uids must be an Array." unless uids.is_a? Array
127
+ raise ArgumentError, "Opts must be a Hash if you include it." unless opts.is_a? Hash
128
+
129
+ opts.symbolize_keys!
130
+
131
+ facebook_ids = "FACEBOOK.ID=\"#{uids.join('\" OR FACEBOOK.ID=\"')}\""
132
+ filter = opts[:filter] || ""
133
+ delivery_time = opts[:delivery_time]
134
+ shorten_urls = opts.has_key?(:shorten_urls) ? opts[:shorten_urls] : true
135
+ options = opts[:message_options]
136
+
137
+ data = {
138
+ cd: 83,
139
+ token: Comufyrails.config.access_token,
140
+ applicationName: Comufyrails.config.app_name,
141
+ description: description,
142
+ content: content,
143
+ filter: "#{facebook_ids} #{filter}"
144
+ }
145
+ data[:deliveryTime] = delivery_time if delivery_time
146
+ data[:trackingMode] = "UNTRACKED" unless shorten_urls
147
+ data[:facebookTargetingMode] = "NOTIFICATION"
148
+
149
+ if options
150
+ data[:fbMessagePrivacyMode] = options[:private] ? "PRIVATE" : "PUBLIC" if options.has_key?(:private)
151
+ data[:fbMessageCaption] = options[:caption] if options.has_key?(:caption)
152
+ data[:fbMessageLink] = options[:link] if options.has_key?(:link)
153
+ data[:fbMessageName] = options[:name] if options.has_key?(:name)
154
+ data[:fbMessageDescription] = options[:description] if options.has_key?(:description)
155
+ data[:fbMessagePictureUrl] = options[:picture] if options.has_key?(:picture)
156
+ end
157
+
158
+ EM.synchrony do
159
+ http = EventMachine::HttpRequest.new(Comufyrails.config.url).post(
160
+ :body => { request: data.to_json }, :initheader => { 'Content-Type' => 'application/json' })
161
+ if http.response_header.status == 200
162
+ message = JSON.parse(http.response)
163
+ case message["cd"]
164
+ when 383 then
165
+ p "383 - Success! - data = #{data} - message = #{message}."
166
+ when 416 then
167
+ p "416 - _ERROR_MSG_SEND_FAILED - data = #{data} - message = #{message}."
168
+ when 475 then
169
+ p "475 - Invalid parameters provided - data = #{data} - message = #{message}."
170
+ when 551 then
171
+ p "551 _ERROR_TAG_VALUE_NOT_FOUND - data = #{data} - message = #{message}."
172
+ when 603 then
173
+ p "603 - _ERROR_DOMAIN_APPLICATION_NAME_NOT_FOUND - data = #{data} - message = #{message}."
174
+ when 607 then
175
+ p "607 - _ERROR_UNAUTHORISED_ACTION - data = #{data} - message = #{message}."
176
+ when 617 then
177
+ p "617 - _ERROR_DOMAIN_APPLICATION_TAG_NOT_FOUND - data = #{data} - message = #{message}."
178
+ when 648 then
179
+ p "648 - _ERROR_FACEBOOK_APPLICATION_USER_NOT_FOUND - data = #{data} - message = #{message}."
180
+ when 673 then
181
+ p "673 - Invalid time exception - data = #{data} - message = #{message}."
182
+ when 679 then
183
+ p "679 - _ERROR_MALFORMED_TARGETING_EXPRESSION - data = #{data} - message = #{message}."
184
+ else
185
+ p "UNKNOWN RESPONSE - data = #{data} - message = #{message}."
186
+ end
187
+ else
188
+ p "Server responded with #{http.response_header}."
189
+ end
190
+ end
191
+ end
192
+
193
+ # Provides a list of all tags for this application. If you provide a block it will yield the response,
194
+ # otherwise it will be sent the log.
195
+ def tags
196
+ data = {
197
+ cd: 101,
198
+ token: Comufyrails.config.access_token,
199
+ applicationName: Comufyrails.config.app_name
200
+ }
201
+
202
+ EM.synchrony do
203
+ http = EventMachine::HttpRequest.new(Comufyrails.config.url).post(
204
+ :body => { request: data.to_json }, :initheader => { 'Content-Type' => 'application/json' })
205
+ if http.response_header.status == 200
206
+ message = JSON.parse(http.response)
207
+ if block_given?
208
+ yield message
209
+ else
210
+ case message["cd"]
211
+ when 219 then
212
+ p "219 - Success! - data = #{data} - message = #{message}."
213
+ else
214
+ p "UNKNOWN RESPONSE - data = #{data} - message = #{message}."
215
+ end
216
+ end
217
+ else
218
+ p "Server responded with #{http.response_header}."
219
+ end
220
+ end
221
+ end
222
+
223
+ # Lists all current users data, with any additional filters you want.
224
+ # If you provide a block it will yield the response, otherwise it will be sent the log.
225
+ # TODO: Replace USER.USER_STATE with something we know will get all users.
226
+ def users filter = ""
227
+ filter = 'USER.USER_STATE="Unknown"' if filter.empty?
228
+ data = {
229
+ cd: 82,
230
+ token: Comufyrails.config.access_token,
231
+ applicationName: Comufyrails.config.app_name,
232
+ since: 1314835200000,
233
+ fetchMode: "ALL",
234
+ filter: filter
235
+ }
236
+
237
+ EM.synchrony do
238
+ http = EventMachine::HttpRequest.new(Comufyrails.config.url).post(
239
+ :body => { request: data.to_json }, :initheader => { 'Content-Type' => 'application/json' })
240
+ if http.response_header.status == 200
241
+ message = JSON.parse(http.response)
242
+ if block_given?
243
+ yield message
244
+ else
245
+ case message["cd"]
246
+ when 382 then
247
+ p "382 - Success! - data = #{data} - message = #{message}."
248
+ when 692 then
249
+ p "692 - Invalid filter/filter not found - data = #{data} - message = #{message}."
250
+ else
251
+ p "UNKNOWN RESPONSE - data = #{data} - message = #{message}."
252
+ end
253
+ end
254
+ else
255
+ p "Server responded with #{http.response_header}."
256
+ end
257
+ end
258
+ end
259
+
260
+ end
6
261
 
7
- # with this in mind, we will probably be using https://github.com/igrigorik/em-synchrony
8
- # with apost and post requests to send and receive data. It'll use the information generated from the Railtie
9
- # to know what/who is sending and to where.
10
262
  end
@@ -0,0 +1,21 @@
1
+ module Comufyrails
2
+ # String data accepted (this is the usual type to use)
3
+ STRING_TYPE = "STRING"
4
+ # Date data (1988-10-01 19:50:48 YYYY-MM-DD HH:mm:SS)
5
+ DATE_TYPE = "DATE"
6
+ # Gender data (TODO: format?)
7
+ GENDER_TYPE = "GENDER"
8
+ # Integer data accepted (32-bit)
9
+ INT_TYPE = "INT"
10
+ # Float data accepted (32-bit float)
11
+ FLOAT_TYPE = "FLOAT"
12
+ # Data types must be one of these formats
13
+ LEGAL_TYPES = [STRING_TYPE, DATE_TYPE, GENDER_TYPE, INT_TYPE, FLOAT_TYPE]
14
+
15
+ # Name tags
16
+ NAME_TAG = :name
17
+ # Type tags
18
+ TYPE_TAG = :type
19
+ # Allowed tag keys
20
+ LEGAL_TAGS = [NAME_TAG, TYPE_TAG]
21
+ end
@@ -0,0 +1,21 @@
1
+
2
+ String.class_eval do
3
+ def to_comufy_time
4
+ time = DateTime.parse(self)
5
+ time.strftime("%Y-%m-%d %H:%M:%S")
6
+ end
7
+ end
8
+
9
+ DateTime.class_eval do
10
+ def to_comufy_time
11
+ self.strftime("%Y-%m-%d %H:%M:%S")
12
+ end
13
+ end
14
+
15
+ if defined?(Rails) and defined?(ActiveSupport)
16
+ ActiveSupport::TimeWithZone.class_eval do
17
+ def to_comufy_time
18
+ self.strftime("%Y-%m-%d %H:%M:%S")
19
+ end
20
+ end
21
+ end
@@ -1,54 +1,29 @@
1
1
  require 'rails'
2
+ require 'comufyrails'
2
3
 
3
4
  class Comufyrails::Railtie < Rails::Railtie
4
5
 
5
6
  # this allows users to manage settings just like they manage rails settings
6
7
  config.comufy_rails = ActiveSupport::OrderedOptions.new # enable namespaced configuration in Rails environment
7
8
 
8
- rake_tasks do
9
- # load "path/to/my_railtie.tasks"
9
+ # initialize our logger
10
+ initializer 'Rails logger' do
11
+ Comufyrails.logger = Rails.logger
10
12
  end
11
13
 
12
14
  initializer "comufyrails.configure" do |app|
13
- Comufyrails.configure do |config|
14
- config.app_name = app.config.comufy_rails[:app_name] || Comufyrails::Railtie.app_name
15
- config.username = app.config.comufy_rails[:username] || Comufyrails::Railtie.username
16
- config.password = app.config.comufy_rails[:password] || Comufyrails::Railtie.password
17
- config.access_token = app.config.comufy_rails[:access_token] || Comufyrails::Railtie.access_token
18
- config.expiry_time = app.config.comufy_rails[:expiry_time] || Comufyrails::Railtie.expiry_time
19
- config.base_api_url = app.config.comufy_rails[:base_api_url] || Comufyrails::Railtie.base_api_url
15
+ Comufyrails.configure do |c|
16
+ c.app_name = app.config.comufy_rails[:app_name] || ENV.fetch('COMUFY_APP_NAME', ::Rails.application.class.to_s.split("::").first)
17
+ c.access_token = app.config.comufy_rails[:access_token] || ENV.fetch('COMUFY_TOKEN', nil)
18
+ c.url = app.config.comufy_rails[:url] || ENV.fetch('COMUFY_URL', 'http://www.sociableapi.com/xcoreweb/client')
19
+
20
+ # we just want a date far into the future
21
+ c.expiry_time = Time.now.to_i + 1000000
20
22
  end
21
23
  end
22
24
 
23
- # Add a to_prepare block which is executed once in production
24
- # and before each request in development
25
- config.to_prepare do
26
- # something
25
+ # load our rake tasks into this rails environment.
26
+ rake_tasks do
27
+ load "tasks/comufyrails.rake"
27
28
  end
28
-
29
- private
30
-
31
- def self.app_name
32
- ENV.fetch('COMUFY_APP_NAME', ::Rails.application.class.to_s.split("::").first)
33
- end
34
-
35
- def self.username
36
- ENV.fetch('COMUFY_USER', nil)
37
- end
38
-
39
- def self.password
40
- ENV.fetch('COMUFY_PASSWORD', nil)
41
- end
42
-
43
- def self.access_token
44
- ENV.fetch('COMUFY_TOKEN', nil)
45
- end
46
-
47
- def self.expiry_time
48
- ENV.fetch('COMUFY_EXPIRY_TIME', nil)
49
- end
50
-
51
- def self.base_api_url
52
- 'http://www.sociableapi.com/xcoreweb/client'
53
- end
54
29
  end
@@ -1,3 +1,4 @@
1
1
  module Comufyrails
2
- VERSION = "0.0.2"
2
+ # Current version of this gem.
3
+ VERSION = "0.1.0"
3
4
  end
@@ -0,0 +1,131 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'net/https'
4
+
5
+ namespace :comufy do
6
+
7
+ desc "Register a tag with your application. The type be
8
+ #{Comufyrails::LEGAL_TYPES.to_sentence(two_words_connector: ' or ', last_word_connector: ', or ')},
9
+ if empty it defaults to STRING."
10
+ task :tag , [:name, :type] => :environment do |t, args|
11
+ raise ArgumentError, "Must specify a name for the tag." unless args.name
12
+ args.with_defaults(type: 'STRING')
13
+
14
+ if Comufyrails.config.app_name.blank?
15
+ p "
16
+ Cannot find the application name, is it currently set to nil or an empty string?\n
17
+ Please check config.comufy_rails.app_name in your environment initializer or the environment variable
18
+ COMUFY_APP_NAME are valid strings.
19
+ "
20
+ elsif Comufyrails.config.base_api_url.blank?
21
+ p "
22
+ Cannot find the base api url, is it currently set to nil or an empty string?\n
23
+ Please check config.comufy_rails.base_api_url in your environment initializer or the environment variable
24
+ COMUFY_BASE_API_URL are valid strings.
25
+ "
26
+ elsif Comufyrails.config.access_token.blank?
27
+ p "
28
+ Cannot find the access token, is it currently set to nil or an empty string?\n
29
+ Please check config.comufy_rails.access_token in your environment initializer or the environment variable
30
+ COMUFY_TOKEN are valid strings.
31
+ "
32
+ elsif not Comufyrails::LEGAL_TYPES.include?(args.type)
33
+ p "The type must be #{Comufyrails::LEGAL_TYPES.to_sentence(
34
+ two_words_connector: ' or ', last_word_connector: ', or ')}"
35
+ else
36
+ data = {
37
+ cd: 86,
38
+ applicationName: Comufyrails.config.app_name,
39
+ token: Comufyrails.config.access_token,
40
+ tags: [{
41
+ name: args.name,
42
+ type: args.type.to_sym
43
+ }]
44
+ }
45
+ response = call_api(Comufyrails.config.url, data)
46
+
47
+ if response.message == 'OK'
48
+ message = JSON.parse(response.read_body)
49
+ case message["cd"]
50
+ when 386 then
51
+ p "386 - Success! - data = #{data} - message = #{message}."
52
+ when 475 then
53
+ p "475 - Invalid parameters provided - data = #{data} - message = #{message}."
54
+ when 603 then
55
+ p "603 - _ERROR_DOMAIN_APPLICATION_NAME_NOT_FOUND - data = #{data} - message = #{message}."
56
+ when 607 then
57
+ p "607 - _ERROR_UNAUTHORISED_ACTION - data = #{data} - message = #{message}."
58
+ when 618 then
59
+ p "618 - _ERROR_DOMAIN_APPLICATION_TAG_ALREADY_REGISTERED - data = #{data} - message = #{message}."
60
+ else
61
+ p "UNKNOWN RESPONSE - data = #{data} - message = #{message}."
62
+ end
63
+ else
64
+ p "Authentication failed when sending #{data}. Please get in touch with Comufy if you cannot resolve this."
65
+ end
66
+ end
67
+ end
68
+
69
+ desc "Unregister an existing tag from your application."
70
+ task :detag , [:name] => :environment do |t, args|
71
+ raise ArgumentError, "Must specify a name for the tag." unless args.name
72
+
73
+ if Comufyrails.config.app_name.blank?
74
+ p "
75
+ Cannot find the application name, is it currently set to nil or an empty string?\n
76
+ Please check config.comufy_rails.app_name in your environment initializer or the environment variable
77
+ COMUFY_APP_NAME are valid strings.
78
+ "
79
+ elsif Comufyrails.config.base_api_url.blank?
80
+ p "
81
+ Cannot find the base api url, is it currently set to nil or an empty string?\n
82
+ Please check config.comufy_rails.base_api_url in your environment initializer or the environment variable
83
+ COMUFY_BASE_API_URL are valid strings.
84
+ "
85
+ elsif Comufyrails.config.access_token.blank?
86
+ p "
87
+ Cannot find the access token, is it currently set to nil or an empty string?\n
88
+ Please check config.comufy_rails.access_token in your environment initializer or the environment variable
89
+ COMUFY_TOKEN are valid strings.
90
+ "
91
+ else
92
+ data = {
93
+ cd: 85,
94
+ applicationName: Comufyrails.config.app_name,
95
+ token: Comufyrails.config.access_token,
96
+ tag: args.name
97
+ }
98
+ response = call_api(Comufyrails.config.url, data)
99
+
100
+ if response.message == 'OK'
101
+ message = JSON.parse(response.read_body)
102
+ case message['cd']
103
+ when 385 then
104
+ p "385 - Success! - data = #{data} - message = #{message}."
105
+ when 475 then
106
+ p "475 - Invalid parameters provided - data = #{data} - message = #{message}."
107
+ when 603 then
108
+ p "603 - _ERROR_DOMAIN_APPLICATION_NAME_NOT_FOUND - data = #{data} - message = #{message}."
109
+ when 607 then
110
+ p "607 - _ERROR_UNAUTHORISED_ACTION - data = #{data} - message = #{message}."
111
+ when 617 then
112
+ p "617 - _ERROR_DOMAIN_APPLICATION_TAG_NOT_FOUND - data = #{data} - message = #{message}."
113
+ else
114
+ p "UNKNOWN RESPONSE - data = #{data} - message = #{message}."
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ private
121
+
122
+ # posts the form to the given url as json and blocks till a response is given.
123
+ def call_api(url, data)
124
+ uri = URI.parse(url)
125
+ http = Net::HTTP.new(uri.host, uri.port)
126
+ request = Net::HTTP::Post.new(uri.path, initheader = { 'Content-Type' => 'application/json' })
127
+ request.set_form_data({ request: data.to_json })
128
+ http.request(request)
129
+ end
130
+
131
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comufyrails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-21 00:00:00.000000000 Z
12
+ date: 2013-01-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: em-synchrony
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: em-http-request
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
30
62
  - !ruby/object:Gem::Dependency
31
63
  name: rake
32
64
  requirement: !ruby/object:Gem::Requirement
@@ -75,8 +107,11 @@ files:
75
107
  - comufyrails.gemspec
76
108
  - lib/comufyrails.rb
77
109
  - lib/comufyrails/connection.rb
110
+ - lib/comufyrails/constants.rb
111
+ - lib/comufyrails/core_ext.rb
78
112
  - lib/comufyrails/railtie.rb
79
113
  - lib/comufyrails/version.rb
114
+ - lib/tasks/comufyrails.rake
80
115
  homepage: https://github.com/plcstevens/comufyrails
81
116
  licenses: []
82
117
  post_install_message:
@@ -100,5 +135,5 @@ rubyforge_project:
100
135
  rubygems_version: 1.8.24
101
136
  signing_key:
102
137
  specification_version: 3
103
- summary: comufyrails-0.0.2
138
+ summary: comufyrails-0.1.0
104
139
  test_files: []