onlineable 1.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 253399538f87316c4e2b12b95a5346f173344670
4
+ data.tar.gz: 751e768073cec33fea1db6ae0c29cf7bed1bc72a
5
+ SHA512:
6
+ metadata.gz: e4f03fbf79702831d8710299faf51c463ed32c87a463640c8b352ca34440ed97fbe0d2ee4c35bb1fd7ffbfd06bcac36967b1768d0962d6b76e1273b8909c51bf
7
+ data.tar.gz: ba977af685988f597fc610f623f2aadb9c5ae75c9d28050a6b4292ae40394ff46368a08299ba7b8b939d88ad37fa4b8e439b7403dc27e773c64f11c44a0077be
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in onliner.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Todd Nestor
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,65 @@
1
+ # onlineable
2
+ Gem for Rails application - provides you list of online users (for authentication gem 'devise') for ActiveRecord ou Mongoid.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'onlineable'
9
+
10
+ And then execute:
11
+
12
+ $ bundle install
13
+
14
+ Next you have to run this to make it install the initializer
15
+
16
+ $ rails g onlineable:install
17
+
18
+ *Note: if you are doing this on a Heroku server run the following command:
19
+
20
+ $ heroku run rails g onlineable:install
21
+
22
+ Do not do a run:detached as it prompts you and you will have to manually
23
+ terminate the process if you run:detached.
24
+
25
+ ## Usage
26
+
27
+ For using functionality add to ApplicationControler:
28
+
29
+ before_action { |c| current_usuario.track_online unless current_usuario.nil?}
30
+
31
+ This will make the app consider the online user while they interact with the application.
32
+
33
+ and In your template, add: onlineable-by-todd for the current device line at the end, and if you want to change the time (in seconds or minutes) that a user is considered online add below: time seconds: n or time minutes: n, the default is seconds: 30.
34
+
35
+ class User < ActiveRecord::Base
36
+ devise ..., :onlineable
37
+ time seconds:30 #or
38
+ time minutes:2
39
+ end
40
+
41
+ Helper for online users list (for the whole list, not just an individual):
42
+
43
+ Model.who_online, e.g. User.who_online, Admin.who_online
44
+
45
+ To see if a user is online you would run something like this:
46
+
47
+ User.who_online.include?(user)
48
+
49
+ Now you can override the default time to check for user activity. It defaults
50
+ to 15 seconds (i.e. if the user hasn't done anything for 15 seconds they are
51
+ not considered online). To override this with say 30 seconds you would do the
52
+ following:
53
+
54
+ User.who_online(30).include?(user)
55
+
56
+ I have also modified the code to work more easily with RedisToGo on Heroku.
57
+ It now works with RedisToGo on Heroku, it needed the password to be parsed,
58
+ now it is.
59
+
60
+ ## Requirement
61
+
62
+ * Redis; this gem will make sure the dependencies get installed but you have
63
+ to make sure you have it on your server, for Heroku I recommend RedisToGo.
64
+
65
+ * Devise;
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,12 @@
1
+ require "rails"
2
+
3
+ module Onlineable
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../../templates', __FILE__)
6
+
7
+ def copy_files
8
+ create_file "config/initializers/onlineable.rb"
9
+ copy_file "onlineable.rb", "config/initializers/onlineable.rb"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ require "redis"
2
+
3
+ if ( Rails.env.development? || Rails.env.test? )
4
+ uri = URI.parse("localhost:6379")
5
+ else
6
+ uri = URI.parse(ENV["REDISTOGO_URL"])
7
+ end
8
+
9
+ REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
data/lib/onlineable.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "onlineable/version"
2
+
3
+ unless defined?(Devise)
4
+ require 'devise'
5
+ end
6
+
7
+ require 'onlineable'
8
+
9
+ Devise.add_module :onlineable, :model => 'onlineable/model'
10
+
11
+ module Onlineable
12
+ end
@@ -0,0 +1,54 @@
1
+ module Devise
2
+ module Models
3
+ module Onlineable
4
+ extend ActiveSupport::Concern
5
+ module ClassMethods
6
+ attr_reader :time_opts
7
+
8
+ def who_online(time_def=nil)
9
+ opts = get_time_opts
10
+ if time_def
11
+ time = time_def
12
+ else
13
+ time = opts[:seconds].present? ? opts[:seconds].to_i : opts[:minutes].to_i
14
+ end
15
+
16
+ if defined?(REDIS)
17
+ array_ids = []
18
+ online_array = REDIS.hgetall "o_#{self.to_s.downcase.pluralize}"
19
+
20
+ online_array.each do |k, v|
21
+ seconds_diff = (Time.now - v.to_time).to_i.abs
22
+ if (opts[:seconds].present? && (seconds_diff <= time)) || (opts[:minutes].present? && ((seconds_diff / 60) <= time)) || (Time.now - v.to_time <= time)
23
+ array_ids << k
24
+ end
25
+ end
26
+ self.find( array_ids )
27
+ end
28
+ end
29
+
30
+ def get_time_opts
31
+ if self.time_opts.blank?
32
+ opts = {seconds: 30}
33
+ elsif self.time_opts.present?
34
+ opts = self.time_opts
35
+ end
36
+ opts || {}
37
+ end
38
+
39
+ private
40
+
41
+ def time(opts={})
42
+ @time_opts = opts
43
+ end
44
+ end
45
+
46
+ def track_online
47
+ if defined?(REDIS)
48
+ REDIS.mapped_hmset "o_#{self.class.to_s.downcase.pluralize}", { id.to_s => "#{Time.now}" }
49
+ end
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module Onlineable
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'onlineable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "onlineable"
8
+ spec.platform = Gem::Platform::RUBY
9
+ spec.version = Onlineable::VERSION
10
+ spec.authors = ["guilhermeap", "Jeová Guilherme de Carvalho Filho"]
11
+ spec.email = ["jguilhermeap@gmail.com"]
12
+ spec.description = "Gem for Rails application - provides you list of online users (for authentication gem 'devise')"
13
+ spec.summary = %q{Gem for Rails application - provides you list of online users (for authentication gem 'devise'), built off of the onlineable gem}
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency('redis', '~> 3.0.6')
25
+ spec.add_dependency "redis-namespace", ">= 1.3.0"
26
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onlineable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - guilhermeap
8
+ - Jeová Guilherme de Carvalho Filho
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-09-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: redis
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 3.0.6
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 3.0.6
56
+ - !ruby/object:Gem::Dependency
57
+ name: redis-namespace
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.3.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.0
70
+ description: Gem for Rails application - provides you list of online users (for authentication
71
+ gem 'devise')
72
+ email:
73
+ - jguilhermeap@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/generators/onlineable/install_generator.rb
84
+ - lib/generators/templates/onlineable.rb
85
+ - lib/onlineable.rb
86
+ - lib/onlineable/model.rb
87
+ - lib/onlineable/version.rb
88
+ - onlineable.gemspec
89
+ homepage:
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.5
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Gem for Rails application - provides you list of online users (for authentication
113
+ gem 'devise'), built off of the onlineable gem
114
+ test_files: []