socky 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -8,6 +8,7 @@ begin
8
8
  gemspec.homepage = "http://github.com/imanel/socky_gem"
9
9
  gemspec.authors = ["Bernard Potocki"]
10
10
  gemspec.add_dependency('em-websocket', '>= 0.0.6')
11
+ gemspec.add_dependency('em-http-request')
11
12
  gemspec.files.exclude ".gitignore"
12
13
  end
13
14
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
data/lib/socky.rb CHANGED
@@ -2,8 +2,8 @@ require 'rubygems'
2
2
  require 'logger'
3
3
  require 'fileutils'
4
4
  require 'em-websocket'
5
- require 'em-websocket_hacks'
6
5
  $:.unshift(File.dirname(__FILE__))
6
+ require 'em-websocket_hacks'
7
7
 
8
8
  module Socky
9
9
 
@@ -3,28 +3,27 @@ module Socky
3
3
  module Authentication
4
4
 
5
5
  def subscribe_request
6
- if authenticated?
7
- debug [self.name, "authentication successed"]
8
- add_to_pool
9
- else
10
- debug [self.name, "authentication failed"]
11
- disconnect
12
- end
6
+ send_subscribe_request do |response|
7
+ if response
8
+ debug [self.name, "authentication successed"]
9
+ add_to_pool
10
+ @authenticated_by_url = true
11
+ else
12
+ debug [self.name, "authentication failed"]
13
+ disconnect
14
+ end
15
+ end unless authenticated?
13
16
  end
14
17
 
15
18
  def unsubscribe_request
16
19
  if authenticated?
17
20
  remove_from_pool
18
- send_unsubscribe_request unless admin
21
+ send_unsubscribe_request{} unless admin
19
22
  end
20
23
  end
21
24
 
22
25
  def authenticated?
23
- if @authenticated.nil?
24
- @authenticated = (admin ? authenticate_as_admin : authenticate_as_user)
25
- else
26
- @authenticated
27
- end
26
+ @authenticated ||= (admin ? authenticate_as_admin : authenticate_as_user)
28
27
  end
29
28
 
30
29
  def authenticate_as_admin
@@ -36,24 +35,24 @@ module Socky
36
35
  end
37
36
 
38
37
  def authenticated_by_url?
39
- send_subscribe_request
38
+ @authenticated_by_url
40
39
  end
41
40
 
42
- def send_subscribe_request
41
+ def send_subscribe_request(&block)
43
42
  subscribe_url = options[:subscribe_url]
44
43
  if subscribe_url
45
44
  debug [self.name, "sending subscribe request to", subscribe_url]
46
- Socky::NetRequest.post(subscribe_url, params_for_request)
47
- else
45
+ Socky::NetRequest.post(subscribe_url, params_for_request, &block)
48
46
  true
49
47
  end
48
+ true
50
49
  end
51
50
 
52
- def send_unsubscribe_request
51
+ def send_unsubscribe_request(&block)
53
52
  unsubscribe_url = options[:unsubscribe_url]
54
53
  if unsubscribe_url
55
54
  debug [self.name, "sending unsubscribe request to", unsubscribe_url]
56
- Socky::NetRequest.post(unsubscribe_url, params_for_request)
55
+ Socky::NetRequest.post(unsubscribe_url, params_for_request, &block)
57
56
  else
58
57
  true
59
58
  end
@@ -1,8 +1,4 @@
1
- require 'timeout'
2
- require 'net/http'
3
- require 'net/https'
4
- require 'uri'
5
- require 'cgi'
1
+ require 'em-http'
6
2
 
7
3
  module Socky
8
4
  class NetRequest
@@ -10,38 +6,14 @@ module Socky
10
6
 
11
7
  class << self
12
8
 
13
- def post(url, params = { })
14
- uri = URI.parse(url)
15
- params = params_from_hash(params)
16
- headers = {"User-Agent" => "Ruby/#{RUBY_VERSION}"}
17
- begin
18
- http = Net::HTTP.new(uri.host, uri.port)
19
- if uri.scheme == 'https'
20
- http.use_ssl = true
21
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
22
- end
23
- http.read_timeout = options[:timeout] || 5
24
- resp, data = http.post(uri.path, params, headers)
25
- return resp.is_a?(Net::HTTPOK)
26
- rescue => e
27
- debug ["Bad request", url.to_s, e.class, e.message]
28
- return false
29
- rescue Timeout::Error
30
- debug [url.to_s, "timeout"]
31
- return false
32
- end
33
- end
34
-
35
- def params_from_hash(params)
36
- result = []
37
- params.each do |key, value|
38
- if value.is_a? Array
39
- value.each{ |v| result << "#{key}[]=#{CGI::escape(v.to_s)}"}
40
- else
41
- result << "#{key}=#{CGI::escape(value.to_s)}"
42
- end
43
- end
44
- result.join('&')
9
+ def post(url, params = {}, &block)
10
+ http = EventMachine::HttpRequest.new(url).post :body => params, :timeout => options[:timeout] || 3
11
+ http.errback { yield false }
12
+ http.callback { yield http.response_header.status == 200 }
13
+ true
14
+ rescue => e
15
+ error "Bad request", e
16
+ false
45
17
  end
46
18
 
47
19
  end
@@ -32,6 +32,8 @@ module Socky
32
32
  :unsubscribe_url: http://localhost:3000/socky/unsubscribe
33
33
 
34
34
  :secret: my_secret_key
35
+
36
+ # :timeout: 3
35
37
  EOF
36
38
 
37
39
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 6
9
- version: 0.0.6
8
+ - 7
9
+ version: 0.0.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Bernard Potocki
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-24 00:00:00 +02:00
17
+ date: 2010-05-26 00:00:00 +02:00
18
18
  default_executable: socky
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -31,6 +31,18 @@ dependencies:
31
31
  version: 0.0.6
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: em-http-request
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
34
46
  description: Socky is a WebSocket server and client for Ruby on Rails
35
47
  email: b.potocki@imanel.org
36
48
  executables: