socky-client-rails 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ :hosts:
2
+ - :host: 127.0.0.1
3
+ :port: 8080
4
+ :secret: my_secret_key
5
+ :secure: false
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/lib/socky-client-rails'
@@ -0,0 +1,19 @@
1
+ require 'action_view'
2
+
3
+ module Socky
4
+ module Helper
5
+
6
+ def socky(options = {})
7
+ host = Socky.random_host
8
+ options = {
9
+ :host => (host[:secure] ? "wss://" : "ws://") + host[:host],
10
+ :port => host[:port],
11
+ }.merge(options)
12
+ javascript_tag "new Socky('#{options.delete(:host)}', '#{options.delete(:port)}', '#{options.to_query}');"
13
+ end
14
+
15
+ end
16
+ end
17
+
18
+ ActionView::Base.send(:include, Socky::Helper)
19
+ ActionView::Helpers::AssetTagHelper.register_javascript_expansion :socky => ['socky']
@@ -0,0 +1,30 @@
1
+ require 'action_view'
2
+
3
+ module Socky
4
+ class JavaScriptGenerator
5
+ include ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods
6
+
7
+ def initialize(&block)
8
+ @lines = []
9
+
10
+ if defined?(Rails)
11
+ case Rails::VERSION::MAJOR
12
+ when 2
13
+ @context = eval("self.instance_variable_get('@template')", block.binding)
14
+ block.call(self)
15
+ unless @context.nil?
16
+ @context.controller.response.body = nil
17
+ @context.controller.instance_variable_set("@performed_render",false)
18
+ end
19
+ when 3
20
+ @context = eval("self.view_context", block.binding)
21
+ block.call(self)
22
+ @context.controller.instance_variable_set("@_response_body",nil) unless @context.nil?
23
+ end
24
+ else
25
+ block.call(self)
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ require 'socky-client-rails'
2
+ require 'rails'
3
+ module Socky
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load "tasks/socky-client-rails.rake"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require 'yaml'
2
+ require 'socky-client'
3
+ require File.dirname(__FILE__) + '/socky-client-rails/helper'
4
+ require File.dirname(__FILE__) + '/socky-client-rails/java_script_generator'
5
+ require 'socky-client-rails/railtie' if defined?(Rails::Railtie)
6
+
7
+ module Socky
8
+
9
+ class << self
10
+
11
+ def config_path
12
+ @config_path ||= Rails.root.join("config","socky_hosts.yml")
13
+ end
14
+
15
+ def send(data = nil, options = {}, &block)
16
+ options = normalize_options(data, options)
17
+ options[:data] = JavaScriptGenerator.new(&block).to_s unless block.nil?
18
+ send_message(options.delete(:data), options)
19
+ end
20
+
21
+ def random_host
22
+ hosts[rand(hosts.size)]
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,70 @@
1
+ require 'fileutils'
2
+
3
+ SOCKY_SCRIPTS = ['/socky.js', '/socky']
4
+ SOURCE_PREFIX = File.join(File.dirname(__FILE__), '..', '..', 'assets')
5
+ DEST_PREFIX = Rails.root.join('public', 'javascripts').to_s
6
+
7
+ namespace :socky do
8
+ desc 'Install the Socky scripts and create configuration file'
9
+ task :install => [:create_scripts, :create_config]
10
+
11
+ desc 'Update the Socky scripts'
12
+ task :update => [:create_scripts]
13
+
14
+ desc 'Remove the Socky scripts'
15
+ task :uninstall => [:remove_scripts]
16
+
17
+ task :create_config do
18
+ source = SOURCE_PREFIX + '/socky_hosts.yml'
19
+ dest = Rails.root.join('config', 'socky_hosts.yml').to_s
20
+ if File.exists?(dest)
21
+ puts "Removing #{dest}."
22
+ FileUtils.rm_rf dest
23
+ end
24
+ begin
25
+ puts "Copying to #{dest}."
26
+ FileUtils.cp_r source, dest
27
+ puts "Successfully updated #{dest}."
28
+ rescue
29
+ puts "ERROR: Problem creating config. Please manually copy " + source + " to " + dest
30
+ end
31
+ end
32
+
33
+
34
+ task :create_scripts do
35
+ SOCKY_SCRIPTS.each do |file_suffix|
36
+ source = SOURCE_PREFIX + file_suffix
37
+ dest = DEST_PREFIX + file_suffix
38
+ if File.exists?(dest)
39
+ puts "Removing #{dest}."
40
+ FileUtils.rm_rf dest
41
+ end
42
+ begin
43
+ puts "Copying to #{dest}."
44
+ FileUtils.cp_r source, dest
45
+ puts "Successfully updated #{dest}."
46
+ rescue
47
+ puts "ERROR: Problem updating scripts. Please manually copy " + source + " to " + dest
48
+ end
49
+ end
50
+ end
51
+
52
+ task :remove_scripts do
53
+ SOCKY_SCRIPTS.each do |dest_suffix|
54
+ dest = DEST_PREFIX + dest_suffix
55
+ if File.exists?(dest)
56
+ begin
57
+ puts "Removing #{dest}..."
58
+ FileUtils.rm_rf dest
59
+ puts "Successfully removed #{dest}"
60
+ rescue
61
+ puts "ERROR: Problem removing #{dest}. Please remove manually."
62
+ end
63
+ else
64
+ puts "ERROR: #{dest} not found."
65
+ end
66
+ end
67
+ puts "Successfully removed Socky."
68
+ end
69
+
70
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Socky do
4
+ context "#send" do
5
+ before(:each) do
6
+ described_class.stub!(:send_data)
7
+ end
8
+ context "should normalize options" do
9
+ it "when block given" do
10
+ described_class.should_receive(:send_data).with({:command => :broadcast, :body => "test"})
11
+ described_class.send do |page|
12
+ page << "test"
13
+ end
14
+ end
15
+ end
16
+ context "with block" do
17
+ it "should allow javascript helpers" do
18
+ described_class.should_receive(:send_data).with({:command => :broadcast, :body => "alert(\"test!\");"})
19
+ described_class.send do |page|
20
+ page.alert("test!")
21
+ end
22
+ end
23
+ it "should handle variables from current context" do
24
+ phrase = "test phrase"
25
+ described_class.should_receive(:send_data).with({:command => :broadcast, :body => phrase})
26
+ described_class.send do |page|
27
+ page << phrase
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ it "#random_host should return host" do
34
+ described_class.random_host.should eql(described_class.hosts.first)
35
+ end
36
+
37
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+
4
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
5
+ require 'socky-client-rails'
@@ -0,0 +1,5 @@
1
+ :hosts:
2
+ - :host: 127.0.0.1
3
+ :port: 8080
4
+ :secret: my_secret_key
5
+ :secure: false
@@ -0,0 +1,9 @@
1
+ unless defined?(Rails)
2
+ require 'pathname'
3
+ require 'rails/version'
4
+ module Rails
5
+ def self.root
6
+ Pathname.new(File.dirname(__FILE__))
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: socky-client-rails
3
+ version: !ruby/object:Gem::Version
4
+ hash: 13
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 1
10
+ version: 0.4.1
11
+ platform: ruby
12
+ authors:
13
+ - Bernard Potocki
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-29 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: socky-client
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 11
30
+ segments:
31
+ - 0
32
+ - 4
33
+ - 2
34
+ version: 0.4.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 2
48
+ - 0
49
+ version: "2.0"
50
+ type: :development
51
+ version_requirements: *id002
52
+ description: Socky is a WebSocket server and client for Ruby
53
+ email: bernard.potocki@imanel.org
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files:
59
+ - README.md
60
+ files:
61
+ - CHANGELOG.md
62
+ - README.md
63
+ - Rakefile
64
+ - VERSION
65
+ - assets/socky.js
66
+ - assets/socky/WebSocketMain.swf
67
+ - assets/socky_hosts.yml
68
+ - init.rb
69
+ - lib/socky-client-rails.rb
70
+ - lib/socky-client-rails/helper.rb
71
+ - lib/socky-client-rails/java_script_generator.rb
72
+ - lib/socky-client-rails/railtie.rb
73
+ - lib/tasks/socky-client-rails.rake
74
+ - spec/socky-client-rails_spec.rb
75
+ - spec/spec_helper.rb
76
+ - spec/support/config.rb
77
+ - spec/support/config/socky_hosts.yml
78
+ has_rdoc: true
79
+ homepage: http://imanel.org/projects/socky
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --charset=UTF-8
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project:
108
+ rubygems_version: 1.3.7
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Socky is a WebSocket server and client for Ruby
112
+ test_files:
113
+ - spec/socky-client-rails_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/support/config.rb