hara 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'rspec'
4
+
3
5
  # Specify your gem's dependencies in hara.gemspec
4
6
  gemspec
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Hara is a simple framework, help you build websocket server.
4
4
 
5
- ## Notice
5
+ ## Notice!!
6
6
 
7
7
  Current API is unstable, you should lock the version.
8
8
 
@@ -46,7 +46,11 @@ ws.send(msg)
46
46
  *start server*
47
47
  `ruby test.rb`
48
48
 
49
- `ruby test.rb -h` to view details
49
+ `ruby test.rb -h` to view options
50
+
51
+ *Other Usage*
52
+
53
+ checkout tests
50
54
 
51
55
  ## Contributing
52
56
 
data/lib/hara/app.rb CHANGED
@@ -10,18 +10,27 @@ module Hara
10
10
 
11
11
  alias halt terminate
12
12
 
13
+ finalizer :app_finalizer
14
+
13
15
  Actions = {}
14
16
 
15
- def self.inherited klass
16
- ::Hara.const_set :Application, klass
17
- end
17
+ class << self
18
+ def inherited klass
19
+ ::Hara.const_set :Application, klass
20
+ end
18
21
 
19
- def initialize socket
20
- @socket = socket
21
- async.setup
22
+ def define_action action, &block
23
+ action = action.to_s
24
+ warn "Action #{action} duplication defined" if Actions.has_key? action
25
+ Hara::Application.send :define_method, action, &block
26
+ method = Hara::Application.send :instance_method, action
27
+ Hara::Application.send :remove_method, action
28
+ Actions[action] = method
29
+ end
22
30
  end
23
31
 
24
- def after_connect
32
+
33
+ def init
25
34
  end
26
35
 
27
36
  #return true or false
@@ -33,37 +42,10 @@ module Hara
33
42
  halt
34
43
  end
35
44
 
45
+ #after_authentication only called when authentication succeeded
36
46
  def after_authentication
37
47
  end
38
48
 
39
- class << self
40
- def define_action action, &block
41
- action = action.to_s
42
- warn "Action #{action} duplication defined" if Actions.has_key? action
43
- Hara::Application.send :define_method, action, &block
44
- method = Hara::Application.send :instance_method, action
45
- Hara::Application.send :remove_method, action
46
- Actions[action] = method
47
- end
48
- end
49
-
50
- def process message
51
- @command = JSON.parse(message)
52
- action = @command["action"]
53
- args = @command["args"]
54
- info "#{Time.now} : #{@socket.remote_ip} request action: #{action}"
55
- before_process action, args
56
- call_action action, *args
57
- rescue JSON::ParserError
58
- warn "#{Time.now} : #{@socket.remote_ip} message can't parse"
59
- terminate
60
- rescue StandardError => e
61
- warn "#{Time.now} : #{@socket.remote_ip} want action: #{action} args: #{args} but process error:\n#{e.inspect}"
62
- terminate
63
- ensure
64
- after_process action, args
65
- end
66
-
67
49
  def before_process action, args
68
50
  end
69
51
 
@@ -81,6 +63,23 @@ module Hara
81
63
  def on_close
82
64
  end
83
65
 
66
+ private
67
+ def initialize socket
68
+ @socket = socket
69
+ async.setup
70
+ end
71
+
72
+ def setup
73
+ init
74
+ info "#{Time.now} : #{socket.remote_ip} coming"
75
+ unless authentication
76
+ authentication_failed
77
+ else
78
+ after_authentication
79
+ async.run
80
+ end
81
+ end
82
+
84
83
  def run
85
84
  while msg = @socket.read
86
85
  process msg
@@ -99,30 +98,35 @@ module Hara
99
98
  end
100
99
  end
101
100
 
102
- def finalizer
103
- on_close unless @closed
101
+ def process message
102
+ @command = JSON.parse(message)
103
+ action = @command["action"]
104
+ args = @command["args"]
105
+ info "#{Time.now} : #{@socket.remote_ip} request action: #{action}"
106
+ before_process action, *args
107
+ call_action action, *args
108
+ rescue JSON::ParserError
109
+ warn "#{Time.now} : #{@socket.remote_ip} message can't parse"
110
+ terminate
111
+ rescue StandardError => e
112
+ warn "#{Time.now} : #{@socket.remote_ip} want action: #{action} args: #{args} but process error:\n#{e.inspect}"
113
+ terminate
104
114
  ensure
105
- @socket.close if @socket
106
- end
107
-
108
- private
109
- def setup
110
- info "#{Time.now} : #{socket.remote_ip} coming"
111
- after_connect
112
- unless authentication
113
- authentication_failed
114
- else
115
- after_authentication
116
- async.run
117
- end
115
+ after_process action, *args
118
116
  end
119
117
 
120
118
  def call_action action, *args
121
119
  if Actions.has_key? action
122
120
  Actions[action].bind(self).call *args
123
121
  else
124
- action_missing action, args
122
+ action_missing action, *args
125
123
  end
126
124
  end
125
+
126
+ def app_finalizer
127
+ on_close unless @closed
128
+ ensure
129
+ @socket.close if @socket
130
+ end
127
131
  end
128
132
  end
data/lib/hara/base.rb CHANGED
@@ -3,11 +3,13 @@ require 'hara/app'
3
3
  require 'hara/server'
4
4
 
5
5
  module Hara
6
- def env
7
- @_env ||= :development
8
- end
6
+ class << self
7
+ def env
8
+ @_env ||= (ENV['APP_ENV'] || :development).to_sym
9
+ end
9
10
 
10
- def env= env
11
- @_env = env
11
+ def env= env
12
+ @_env = env
13
+ end
12
14
  end
13
15
  end
data/lib/hara/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hara
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/spec/app_spec.rb ADDED
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hara::App do
4
+ before :all do
5
+ Class.new Hara::App do
6
+ def init
7
+ @states = []
8
+ end
9
+
10
+ def before_process action, *args
11
+ @states << [:before_process, action, args]
12
+ end
13
+
14
+ def after_process action, *args
15
+ @states << [:after_process, action, args]
16
+ end
17
+
18
+ def on_close
19
+ @states << :closed
20
+ end
21
+
22
+ def action_missing action, *args
23
+ socket << [:action_missing, action, args]
24
+ end
25
+
26
+ define_action :hello do |msg|
27
+ socket << "hello#{msg}"
28
+ end
29
+
30
+ def authentication
31
+ socket.remote_ip == 'localhost'
32
+ end
33
+
34
+ def authentication_failed
35
+ @states << :fail
36
+ sleep 0.1
37
+ end
38
+
39
+ def after_authentication
40
+ @states << :success
41
+ sleep 0.1
42
+ end
43
+
44
+ def states
45
+ @states
46
+ end
47
+ end
48
+ end
49
+
50
+ before :each do
51
+ @socket = FayeSocket.new
52
+ @socket.remote_ip = 'localhost'
53
+ end
54
+
55
+ after :each do
56
+ if @app && @app.alive?
57
+ @app.terminate!
58
+ end
59
+ end
60
+
61
+ it 'authentication_failed' do
62
+ @socket.remote_ip = '0.0.0.0'
63
+ @app = Hara::Application.new @socket
64
+ sleep 0.1 while @app.states.size == 0
65
+ @app.states.should == [:fail]
66
+ end
67
+
68
+ it 'authentication_succeeded' do
69
+ @app = Hara::Application.new @socket
70
+ sleep 0.1 while @app.states.size == 0
71
+ @app.states.should == [:success]
72
+ end
73
+
74
+ it 'remote call actions' do
75
+ @socket.client_send({action: :hello, args: [' world']}.to_json)
76
+ @app = Hara::Application.new @socket
77
+ sleep 0.1 until msg = @socket.client_read
78
+ msg.should == 'hello world'
79
+ end
80
+
81
+ it 'action_missing should work' do
82
+ @socket.client_send({action: :hello_world, args: ['hello', 'world']}.to_json)
83
+ @app = Hara::Application.new @socket
84
+ sleep 0.1 until msg = @socket.client_read
85
+ msg.should == [:action_missing, 'hello_world', ['hello', 'world']]
86
+ end
87
+
88
+ it 'callbacks should work' do
89
+ @app = Hara::Application.new @socket
90
+ @socket.client_send({action: :hello, args: [' world']}.to_json)
91
+ states = @app.states
92
+ sleep 0.2
93
+ @app.halt
94
+ states.should == [:success, [:before_process, "hello", [" world"]], [:after_process, "hello", [" world"]],:closed]
95
+ end
96
+ end
data/spec/hara_spec.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hara do
4
+ it 'Hara::Application should equal with App' do
5
+ App = Class.new Hara::App
6
+ App.should == Hara::Application
7
+ end
8
+
9
+ it 'Hara.env should be development' do
10
+ Hara.env.should == :development
11
+ end
12
+ end
@@ -0,0 +1,38 @@
1
+ require 'hara/base'
2
+
3
+ require 'json'
4
+
5
+ Celluloid.logger = nil
6
+
7
+ class FayeSocket
8
+ attr_accessor :remote_ip
9
+
10
+ def initialize
11
+ @client_messages = []
12
+ @server_messages = []
13
+ end
14
+
15
+ def alive?
16
+ @alive
17
+ end
18
+
19
+ def close
20
+ @alive = false
21
+ end
22
+
23
+ def read
24
+ @server_messages.shift
25
+ end
26
+
27
+ def << message
28
+ @client_messages << message
29
+ end
30
+
31
+ def client_read
32
+ @client_messages.shift
33
+ end
34
+
35
+ def client_send message
36
+ @server_messages << message
37
+ end
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hara
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-12 00:00:00.000000000 Z
12
+ date: 2013-06-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: reel
@@ -79,6 +79,9 @@ files:
79
79
  - lib/hara/main.rb
80
80
  - lib/hara/server.rb
81
81
  - lib/hara/version.rb
82
+ - spec/app_spec.rb
83
+ - spec/hara_spec.rb
84
+ - spec/spec_helper.rb
82
85
  homepage: http://github.com/jjyr/hara
83
86
  licenses:
84
87
  - MIT
@@ -104,4 +107,7 @@ rubygems_version: 1.8.23
104
107
  signing_key:
105
108
  specification_version: 3
106
109
  summary: Hara can help your build [web ]socket server easier.
107
- test_files: []
110
+ test_files:
111
+ - spec/app_spec.rb
112
+ - spec/hara_spec.rb
113
+ - spec/spec_helper.rb