notification_server 0.2.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/README.md ADDED
@@ -0,0 +1,4 @@
1
+ ruby-notification-server
2
+ ========================
3
+
4
+ Ruby integration with nodejs based "notification-server"
@@ -0,0 +1,12 @@
1
+ module NotificationServer
2
+ module Generators
3
+ class ConfigGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def generate_config
7
+ copy_file 'config.yml', 'config/notification_server.yml'
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ production:
2
+ hostname: localhost
3
+ port: 8081
4
+
5
+ development:
6
+ hostname: localhost
7
+ port: 2212
8
+
9
+ test:
10
+ hostname: localhost
11
+ port: 2211
@@ -0,0 +1,58 @@
1
+ module NotificationServer
2
+ module ActionView
3
+ module ScriptHelper
4
+
5
+ #
6
+ # Generate script tag for including notification server script
7
+ def javascript_include_notification_server
8
+ html = if defined?(content_tag)
9
+ content_tag 'script', '',
10
+ src: NotificationServer::Configuration.script_path,
11
+ type: 'text/javascript'
12
+ else
13
+ "<script type='text/javascript' " +
14
+ " src=\"#{NotificationServer::Configuration.script_path}\">" +
15
+ "</script>"
16
+ end
17
+
18
+ _safe html
19
+ end
20
+
21
+ #
22
+ # Generate async javascript embeddable code
23
+ #
24
+ # If you use this method remember you can't get "now" variable immediately
25
+ # you have to wait for "now" variable.
26
+ def javascript_include_async_notification_server
27
+ html = <<HTML
28
+ <script type='text/javascript' id='__notification_server'></script>
29
+ <script type="text/javascript">
30
+ (function() {
31
+ if (typeof(jQuery) != 'undefined') {
32
+ setTimeout(function() {
33
+ jQuery('head').append(jQuery('<script />', {
34
+ type: "text/javascript",
35
+ src: "#{NotificationServer::Configuration.script_path}"
36
+ }));
37
+ } , 1000);
38
+ } else {
39
+ document.getElementById('__notification_server').src = "#{NotificationServer::Configuration.script_path}";
40
+ }
41
+ })();
42
+ </script>
43
+ HTML
44
+ _safe html
45
+ end
46
+
47
+ private
48
+ def _safe html
49
+ if html.respond_to? :html_safe
50
+ html.html_safe
51
+ else
52
+ html
53
+ end
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ require 'notification_server/action_view/script_helper'
2
+
3
+ module NotificationServer
4
+ module ActionView
5
+ include ScriptHelper
6
+ end
7
+ end
@@ -0,0 +1,47 @@
1
+ module NotificationServer
2
+ class Configuration
3
+ class << self
4
+
5
+ def hostname; _config['hostname'] end
6
+ def port; _config['port'] end
7
+ def forever; _config['forever'] end
8
+ def secure; _config['secure'] end
9
+ def script_path; "#{secure ? 'https' : 'http'}://#{hostname}:#{port}/nowjs/now.js" end
10
+
11
+ private
12
+ def _config
13
+ if defined?(@@_config).nil?
14
+ _load_config_file(_file_join('config', 'notification_server.yml'))
15
+ else
16
+ @@_config
17
+ end
18
+ end
19
+
20
+ def _file_join(*paths)
21
+ if defined?(Rails)
22
+ Rails.root.join(*paths)
23
+ else
24
+ File.join(paths)
25
+ end
26
+ end
27
+
28
+ def _load_config_file(config_file)
29
+ if File.exist?(config_file)
30
+ yaml = YAML.load(File.read(config_file))
31
+ @@_config = yaml[_config_env]
32
+ else
33
+ puts "#{config_file} not found. please execute `rails g notification_server:config`"
34
+ @@_config = {}
35
+ end
36
+ end
37
+
38
+ def _config_env
39
+ if defined?(Rails)
40
+ Rails.env
41
+ else
42
+ 'test'
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+ require 'notification_server/action_view'
3
+
4
+ module NotificationServer
5
+ class Engine < Rails::Engine
6
+ engine_name 'notification_server'
7
+
8
+ initializer 'notification_server.helpers' do |app|
9
+ ActiveSupport.on_load :action_controller do |ac|
10
+ ac.helper NotificationServer::ActionView
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module NotificationServer
2
+ require 'notification_server/engine' if defined?(Rails)
3
+ require 'notification_server/configuration'
4
+ end
@@ -0,0 +1,113 @@
1
+ require 'fileutils'
2
+
3
+ module NotificationServer
4
+ module Tasks
5
+ class Server
6
+ class << self
7
+
8
+ def start(_env)
9
+ _config = config(_env)
10
+ _argv = []
11
+ _argv << "-p #{_config['port']}"
12
+
13
+ if _config['host'].present?
14
+ _argv << "-H #{_config['host']}"
15
+ end
16
+
17
+ if cmd_exist?
18
+ _pid = create_pid(spawn("/usr/bin/env notification-server #{_argv.join(' ')}"), _env)
19
+ STDOUT.puts "Process id - #{_pid}"
20
+ else
21
+ STDERR.puts "`notification-server` command not found, please install `npm install -g notification-server`"
22
+ end
23
+ end
24
+
25
+ def stop(_env)
26
+ existing_pid = read_pid(_env)
27
+ if existing_pid.present?
28
+ output = `kill #{existing_pid}`
29
+ STDOUT.puts output
30
+ STDOUT.puts 'Notification server stopped.'
31
+ remove_pid(_env)
32
+ else
33
+ STDERR.puts "No such existing notification server process found"
34
+ end
35
+ end
36
+
37
+ def remove_pid(_env)
38
+ FileUtils.remove pid_file(_env)
39
+ end
40
+
41
+ def create_pid(_pid, _env)
42
+ File.open(pid_file(_env), 'w') do |f|
43
+ f.puts _pid
44
+ end
45
+
46
+ _pid
47
+ end
48
+
49
+ def read_pid(_env)
50
+ if File.exist?(pid_file(_env))
51
+ File.read(pid_file(_env))
52
+ else
53
+ nil
54
+ end
55
+ end
56
+
57
+ def pid_file(_env)
58
+ pids_path = Rails.root.join('tmp', 'pids')
59
+ FileUtils.makedirs(pids_path)
60
+
61
+ File.join(pids_path, "notification_server_#{_env}.pid")
62
+ end
63
+
64
+ def cmd_exist?
65
+ _output = `which notification-server`
66
+ _output.present?
67
+ end
68
+
69
+ def config_exist?
70
+ File.exist? config_file
71
+ end
72
+
73
+ def config(_env)
74
+ if config_exist?
75
+ yaml = YAML.load(File.read(config_file))
76
+ if valid_config? yaml[_env]
77
+ yaml[_env]
78
+ else
79
+ raise "notification-server >> `port` is not defined in #{config_file}"
80
+ end
81
+ else
82
+ raise "notification-server >> config not found, please run `rails g notification_server:config`"
83
+ end
84
+ end
85
+
86
+ def config_file
87
+ Rails.root.join('config', 'notification_server.yml')
88
+ end
89
+
90
+ def valid_config?(_config)
91
+ _config.is_a?(Hash) && _config.keys.include?('port')
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ namespace :notification_server do
99
+ desc 'Start notification server'
100
+ task :start => :environment do
101
+ NotificationServer::Tasks::Server.start(Rails.env)
102
+ end
103
+
104
+ desc 'Stop notification server'
105
+ task :stop => :environment do
106
+ NotificationServer::Tasks::Server.stop(Rails.env)
107
+ end
108
+
109
+ desc 'Restart notification server'
110
+ task :restart => [:stop, :start] do
111
+ puts 'Restarted notification server.'
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notification_server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - nhm tanveer hossain khan
9
+ - nafi ul karim
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-05-10 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: &2154091460 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2154091460
26
+ - !ruby/object:Gem::Dependency
27
+ name: jeweler
28
+ requirement: &2154107600 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *2154107600
37
+ description: Node notification server integration library for ruby project
38
+ email:
39
+ - hasan@somewherein.net
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files:
43
+ - README.md
44
+ files:
45
+ - lib/generators/notification_server/config/config_generator.rb
46
+ - lib/generators/notification_server/config/templates/config.yml
47
+ - lib/notification_server.rb
48
+ - lib/notification_server/action_view.rb
49
+ - lib/notification_server/action_view/script_helper.rb
50
+ - lib/notification_server/configuration.rb
51
+ - lib/notification_server/engine.rb
52
+ - lib/tasks/server.rake
53
+ - README.md
54
+ homepage: https://github.com/we4tech/ruby-notification-server
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: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.10
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: An extension for integrating node 'notification-server' with ruby project
78
+ test_files: []