rubyfox-client 0.2.0-java → 0.3.0-java

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -35,7 +35,7 @@ Or install it yourself as:
35
35
  client.send :login, *ARGV
36
36
  end
37
37
  client.on_event :login do |event|
38
- p :login => event.arguments["zone"]
38
+ p :login => event.params[:zone]
39
39
  client.disconnect
40
40
  end
41
41
  client.on_event :login_error do |event|
data/examples/simple.rb CHANGED
@@ -10,7 +10,7 @@ client.on_event :connection do |event|
10
10
  client.send :login, *ARGV
11
11
  end
12
12
  client.on_event :login do |event|
13
- p :login => event.arguments["zone"]
13
+ p :login => event.params[:zone]
14
14
  client.disconnect
15
15
  end
16
16
  client.on_event :login_error do |event|
@@ -11,7 +11,7 @@ module Rubyfox
11
11
  require 'rubyfox/sfsobject/core_ext'
12
12
 
13
13
  require 'rubyfox/client/transport'
14
- require 'rubyfox/client/event_handler'
14
+ require 'rubyfox/client/recorder'
15
15
  end
16
16
 
17
17
  def self.require_libs(dir)
@@ -5,6 +5,8 @@ module Rubyfox
5
5
  Event = Java::SFSEvent
6
6
 
7
7
  class Event
8
+ self.__persistent__ = true
9
+
8
10
  def self.types(&block)
9
11
  constants
10
12
  end
@@ -13,13 +15,33 @@ module Rubyfox
13
15
  const_get(name.to_s.upcase)
14
16
  end
15
17
 
16
- def method_missing(name, *args, &block)
17
- self[name]
18
+ def params
19
+ @params ||= EventParams.new(arguments)
18
20
  end
19
21
 
20
22
  def inspect
21
23
  "#{super}: #{type}(#{arguments.inspect})"
22
24
  end
23
25
  end
26
+
27
+ class EventParams
28
+ include Enumerable
29
+
30
+ def initialize(hash)
31
+ @hash = hash
32
+ end
33
+
34
+ def [](key)
35
+ @hash[key.to_s]
36
+ end
37
+
38
+ def each(&block)
39
+ @hash.each(&block)
40
+ end
41
+
42
+ def inspect
43
+ @hash.inspect
44
+ end
45
+ end
24
46
  end
25
47
  end
@@ -4,7 +4,7 @@ module Rubyfox
4
4
  module Client
5
5
  module Java
6
6
  SmartFox = ::Java.sfs2x.client.SmartFox
7
- Requests = ::Java.sfs2x.client.requests
7
+ Request = ::Java.sfs2x.client.requests
8
8
  SFSEvent = ::Java.sfs2x.client.core.SFSEvent
9
9
  System = ::Java.java.lang.System
10
10
  end
@@ -0,0 +1,81 @@
1
+ require 'timeout'
2
+ require 'rubyfox/client/event'
3
+
4
+ module Rubyfox
5
+ module Client
6
+ # Records all incoming and outgoing events, requests and extension requests
7
+ # for test purposes.
8
+ #
9
+ # == Example
10
+ #
11
+ # client = Rubyfox::Client.new
12
+ # recorder = Rubyfox::Rubyfox::Recorder.new(client)
13
+ #
14
+ # client.connect
15
+ # client.send :login, "username", "password", "zone"
16
+ # client.disconnect
17
+ #
18
+ # assert_equal 1, recorder.events(:connection).size
19
+ # assert_equal 1, recorder.events(:login).size
20
+ # assert_equal 1, recorder.extensions("Me.User").size
21
+ # assert_equal 1, recorder.events(:connection_lost).size
22
+ class Recorder
23
+ include Timeout
24
+
25
+ def initialize(client)
26
+ @client = client
27
+ @events = Hash.new { |hash, type| hash[type] = [] }
28
+ @extensions = Hash.new { |hash, command| hash[command] = [] }
29
+ @connection_lost = false
30
+ install_callbacks
31
+ end
32
+
33
+ def events(name)
34
+ type = Event[name]
35
+ @events[type]
36
+ end
37
+
38
+ def extensions(command)
39
+ @extensions[command]
40
+ end
41
+
42
+ def verify(options={}, &block)
43
+ @client.connect
44
+ wait = options[:timeout] || 1
45
+ timeout wait do
46
+ while not @connection_lost
47
+ sleep 0.1
48
+ end
49
+ end
50
+ rescue Timeout::Error
51
+ # ignore
52
+ ensure
53
+ yield
54
+ end
55
+
56
+ private
57
+
58
+ def install_callbacks
59
+ @client.on_event(:any) do |event|
60
+ record_event(event)
61
+ end
62
+
63
+ @client.on_extension(:any) do |extension|
64
+ record_extension(extension)
65
+ end
66
+
67
+ @client.on_event(:connection_lost) do |event|
68
+ @connection_lost = true
69
+ end
70
+ end
71
+
72
+ def record_event(event)
73
+ @events[event.type] << event
74
+ end
75
+
76
+ def record_extension(extension)
77
+ @extensions[extension.command] << extension
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,21 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require 'rubyfox/client/java'
3
+
4
+ module Rubyfox
5
+ module Client
6
+ Request = Java::Request
7
+
8
+ module Request
9
+ def self.[](name)
10
+ case name
11
+ when Request::BaseRequest
12
+ name
13
+ else
14
+ name = name.to_s.camelcase
15
+ name += "Request" unless name.end_with?("Request")
16
+ Request.__send__(name)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,6 +1,6 @@
1
1
  require 'rubyfox/client/java'
2
2
  require 'rubyfox/client/config'
3
- require 'rubyfox/client/requests'
3
+ require 'rubyfox/client/request'
4
4
  require 'rubyfox/client/event_handler'
5
5
  require 'rubyfox/client/extension_handler'
6
6
 
@@ -22,6 +22,11 @@ module Rubyfox
22
22
  @event_handler.register
23
23
  @extension_handler.register
24
24
  @smartfox.connect(@config.host, @config.port)
25
+ sleep 0.1
26
+ end
27
+
28
+ def connected?
29
+ @smartfox.connected?
25
30
  end
26
31
 
27
32
  def disconnect
@@ -35,8 +40,8 @@ module Rubyfox
35
40
  Java::System.exit(ret)
36
41
  end
37
42
 
38
- def send(request, *args)
39
- request = Requests[request, *args]
43
+ def send(command, *args)
44
+ request = Request[command].new(*args)
40
45
  @smartfox.send(request)
41
46
  end
42
47
 
@@ -1,5 +1,5 @@
1
1
  module Rubyfox
2
2
  module Client
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rubyfox-client
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.3.0
6
6
  platform: java
7
7
  authors:
8
8
  - Peter Suschlik
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-11-23 00:00:00.000000000 Z
13
+ date: 2012-12-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -104,7 +104,8 @@ files:
104
104
  - lib/rubyfox/client/event_handler.rb
105
105
  - lib/rubyfox/client/extension_handler.rb
106
106
  - lib/rubyfox/client/java.rb
107
- - lib/rubyfox/client/requests.rb
107
+ - lib/rubyfox/client/recorder.rb
108
+ - lib/rubyfox/client/request.rb
108
109
  - lib/rubyfox/client/transport.rb
109
110
  - lib/rubyfox/client/vendor/SFS2X_API_Java.jar
110
111
  - lib/rubyfox/client/vendor/jdom.jar
@@ -1,23 +0,0 @@
1
- require 'active_support/core_ext/string/inflections'
2
- require 'rubyfox/client/java'
3
-
4
- module Rubyfox
5
- module Client
6
- Requests = Java::Requests
7
-
8
- module Requests
9
- def self.[](*args)
10
- arg = args.shift
11
- case arg
12
- when Java::Requests::BaseRequest
13
- arg
14
- else
15
- name = arg.to_s.camelcase
16
- name += "Request" unless name.end_with?("Request")
17
- request_klass = Requests.__send__(name)
18
- request_klass.new(*args)
19
- end
20
- end
21
- end
22
- end
23
- end