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 ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - jruby-19mode
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
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+ task :default => :spec
data/lib/hara/app.rb CHANGED
@@ -46,14 +46,17 @@ module Hara
46
46
  def after_authentication
47
47
  end
48
48
 
49
- def before_process action, args
49
+ def before_action action, args
50
50
  end
51
51
 
52
- def after_process action, args
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
- warn "#{Time.now} : #{socket.remote_ip} request action: #{action} args: #{args}, action not defined"
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 "#{Time.now} : #{socket.remote_ip} coming"
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 "#{Time.now} : #{@socket.remote_ip} disconnect, user: #{@user ? @user.id : "none"}"
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 "#{Time.now} : #{@socket.remote_ip} request action: #{action}"
106
- before_process action, *args
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
- warn "#{Time.now} : #{@socket.remote_ip} message can't parse"
113
+ info "#{socket.remote_ip} message can't parse"
110
114
  terminate
111
115
  rescue StandardError => e
112
- warn "#{Time.now} : #{@socket.remote_ip} want action: #{action} args: #{args} but process error:\n#{e.inspect}"
116
+ info "#{socket.remote_ip} processing error:\n#{e.inspect}"
113
117
  terminate
114
118
  ensure
115
- after_process action, *args
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
- warn "Not support normal connection"
20
- handle_request connection, request
20
+ Hara.request_handler.call connection, request
21
21
  else
22
- warn "Not support normal connection"
22
+ warn "Connection not support"
23
23
  request.close
24
24
  end
25
25
  end
26
26
  end
27
27
 
28
- def handle_request connection, request
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
@@ -1,3 +1,3 @@
1
1
  module Hara
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/spec/app_spec.rb CHANGED
@@ -7,12 +7,16 @@ describe Hara::App do
7
7
  @states = []
8
8
  end
9
9
 
10
- def before_process action, *args
11
- @states << [:before_process, action, args]
10
+ def before_action action, *args
11
+ @states << [:before_action, action, args]
12
12
  end
13
13
 
14
- def after_process action, *args
15
- @states << [:after_process, action, args]
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
- states.should == [:success, [:before_process, "hello", [" world"]], [:after_process, "hello", [" world"]],:closed]
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.2
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-15 00:00:00.000000000 Z
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