hara 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/README.md +3 -0
- data/Rakefile +4 -0
- data/lib/hara/app.rb +14 -10
- data/lib/hara/base.rb +10 -1
- data/lib/hara/server.rb +5 -4
- data/lib/hara/version.rb +1 -1
- data/spec/app_spec.rb +18 -5
- data/spec/hara_spec.rb +10 -0
- metadata +3 -2
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# Hara
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/hara.png)](http://badge.fury.io/rb/hara)
|
4
|
+
[![Build Status](https://travis-ci.org/jjyr/hara.png?branch=master)](https://travis-ci.org/jjyr/hara)
|
5
|
+
|
3
6
|
Hara is a simple framework, help you build websocket server.
|
4
7
|
|
5
8
|
## Notice!!
|
data/Rakefile
CHANGED
data/lib/hara/app.rb
CHANGED
@@ -46,14 +46,17 @@ module Hara
|
|
46
46
|
def after_authentication
|
47
47
|
end
|
48
48
|
|
49
|
-
def
|
49
|
+
def before_action action, args
|
50
50
|
end
|
51
51
|
|
52
|
-
def
|
52
|
+
def after_action action, args
|
53
|
+
end
|
54
|
+
|
55
|
+
def after_process
|
53
56
|
end
|
54
57
|
|
55
58
|
def action_missing action, args
|
56
|
-
|
59
|
+
info " #{socket.remote_ip} request action: #{action} args: #{args.inspect}, action not defined"
|
57
60
|
end
|
58
61
|
|
59
62
|
def command
|
@@ -71,7 +74,7 @@ module Hara
|
|
71
74
|
|
72
75
|
def setup
|
73
76
|
init
|
74
|
-
info "#{
|
77
|
+
info "#{socket.remote_ip} coming"
|
75
78
|
unless authentication
|
76
79
|
authentication_failed
|
77
80
|
else
|
@@ -89,7 +92,7 @@ module Hara
|
|
89
92
|
end
|
90
93
|
end
|
91
94
|
rescue Reel::SocketError, EOFError #client disconnect
|
92
|
-
info "#{
|
95
|
+
info "#{socket.remote_ip} disconnect"
|
93
96
|
begin
|
94
97
|
@closed = true unless @closed
|
95
98
|
on_close
|
@@ -102,17 +105,18 @@ module Hara
|
|
102
105
|
@command = JSON.parse(message)
|
103
106
|
action = @command["action"]
|
104
107
|
args = @command["args"]
|
105
|
-
info "#{
|
106
|
-
|
108
|
+
info "#{socket.remote_ip} request action: #{action} args: #{args.inspect}"
|
109
|
+
before_action action, *args
|
107
110
|
call_action action, *args
|
111
|
+
after_action action, *args
|
108
112
|
rescue JSON::ParserError
|
109
|
-
|
113
|
+
info "#{socket.remote_ip} message can't parse"
|
110
114
|
terminate
|
111
115
|
rescue StandardError => e
|
112
|
-
|
116
|
+
info "#{socket.remote_ip} processing error:\n#{e.inspect}"
|
113
117
|
terminate
|
114
118
|
ensure
|
115
|
-
after_process
|
119
|
+
after_process
|
116
120
|
end
|
117
121
|
|
118
122
|
def call_action action, *args
|
data/lib/hara/base.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require "hara/version"
|
2
2
|
require 'hara/app'
|
3
|
-
require 'hara/server'
|
4
3
|
|
5
4
|
module Hara
|
6
5
|
class << self
|
@@ -11,5 +10,15 @@ module Hara
|
|
11
10
|
def env= env
|
12
11
|
@_env = env
|
13
12
|
end
|
13
|
+
|
14
|
+
def request_handler &blk
|
15
|
+
if blk
|
16
|
+
@_request_handler = blk
|
17
|
+
else
|
18
|
+
@_request_handler
|
19
|
+
end
|
20
|
+
end
|
14
21
|
end
|
15
22
|
end
|
23
|
+
|
24
|
+
require 'hara/server'
|
data/lib/hara/server.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'reel'
|
2
|
+
require 'hara/base'
|
2
3
|
|
3
4
|
module Hara
|
4
5
|
class Server < Reel::Server
|
@@ -16,16 +17,16 @@ module Hara
|
|
16
17
|
info "Received a WebSocket connection"
|
17
18
|
handle_websocket request
|
18
19
|
when Reel::Request
|
19
|
-
|
20
|
-
handle_request connection, request
|
20
|
+
Hara.request_handler.call connection, request
|
21
21
|
else
|
22
|
-
warn "
|
22
|
+
warn "Connection not support"
|
23
23
|
request.close
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
|
28
|
+
Hara.request_handler do |connection, request|
|
29
|
+
info "#{request.remote_ip} request #{request.url}, not support"
|
29
30
|
request.close
|
30
31
|
end
|
31
32
|
|
data/lib/hara/version.rb
CHANGED
data/spec/app_spec.rb
CHANGED
@@ -7,12 +7,16 @@ describe Hara::App do
|
|
7
7
|
@states = []
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
@states << [:
|
10
|
+
def before_action action, *args
|
11
|
+
@states << [:before_action, action, args]
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
@states << [:
|
14
|
+
def after_action action, *args
|
15
|
+
@states << [:after_action, action, args]
|
16
|
+
end
|
17
|
+
|
18
|
+
def after_process
|
19
|
+
@states << :after_process
|
16
20
|
end
|
17
21
|
|
18
22
|
def on_close
|
@@ -78,6 +82,14 @@ describe Hara::App do
|
|
78
82
|
msg.should == 'hello world'
|
79
83
|
end
|
80
84
|
|
85
|
+
it 'error remote call' do
|
86
|
+
@socket.client_send("a error call")
|
87
|
+
@app = Hara::Application.new @socket
|
88
|
+
sleep 0.1 while @app.alive?
|
89
|
+
msg = @socket.client_read
|
90
|
+
msg.should == nil
|
91
|
+
end
|
92
|
+
|
81
93
|
it 'action_missing should work' do
|
82
94
|
@socket.client_send({action: :hello_world, args: ['hello', 'world']}.to_json)
|
83
95
|
@app = Hara::Application.new @socket
|
@@ -91,6 +103,7 @@ describe Hara::App do
|
|
91
103
|
states = @app.states
|
92
104
|
sleep 0.2
|
93
105
|
@app.halt
|
94
|
-
|
106
|
+
sleep 0.1 while @app.alive?
|
107
|
+
states.should == [:success, [:before_action, "hello", [" world"]], [:after_action, "hello", [" world"]], :after_process,:closed]
|
95
108
|
end
|
96
109
|
end
|
data/spec/hara_spec.rb
CHANGED
@@ -9,4 +9,14 @@ describe Hara do
|
|
9
9
|
it 'Hara.env should be development' do
|
10
10
|
Hara.env.should == :development
|
11
11
|
end
|
12
|
+
|
13
|
+
it 'Hara.request_handler should work' do
|
14
|
+
handler = Hara.request_handler
|
15
|
+
handler.class.should == Proc
|
16
|
+
Hara.request_handler do |conn, req|
|
17
|
+
conn + req
|
18
|
+
end
|
19
|
+
Hara.request_handler.call(40, 2).should == 42
|
20
|
+
Hara.request_handler &handler
|
21
|
+
end
|
12
22
|
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
|
4
|
+
version: 0.1.0
|
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
|
+
date: 2013-06-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: reel
|
@@ -68,6 +68,7 @@ extensions: []
|
|
68
68
|
extra_rdoc_files: []
|
69
69
|
files:
|
70
70
|
- .gitignore
|
71
|
+
- .travis.yml
|
71
72
|
- Gemfile
|
72
73
|
- LICENSE.txt
|
73
74
|
- README.md
|