angelo 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eace5c23e9adee9dcfd79505ca157123efe2a8d5
4
- data.tar.gz: f53fe954693847bb50a5ea88c08904d3313e092b
3
+ metadata.gz: d3ee383cbc5d2f838c1a46aa1d5e18e7e22bb900
4
+ data.tar.gz: fbf02365249c4c948c81148906247bb357e0c887
5
5
  SHA512:
6
- metadata.gz: 1970b01b008eabd222cb8b5219ae6783707d5d801ac3b26971fd59f31b0d8683bb0810e48b900199227cfbbbdc95fbbf205b0804d8bb483e0c0491844fcd53b8
7
- data.tar.gz: 10d4cec420a5d0e5a441189d996616b2753ee80d69c434aaba780e9bec1c823f8bba8f66b62d30a6df9c97764ab66b3a329a8054166cf275b8ac01f625ca98ce
6
+ metadata.gz: 3bc808f0d1104da6f11f7d7b9e8e97ed7a6613f516e91f7734eac2d9a614babb6bbd727c947a72647791b1606dc5cbad025f972909ef4ccf3ffbc0033e3f0d78
7
+ data.tar.gz: 5daa0814e6825926be13f01b5a42b40b20b3ffaba6ddd88ad9ee4e0001b398ce13286c3f1407e0c0af58c7ceda6875b263979be416114a16637e153b8b2be731
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  changelog
2
2
  =========
3
3
 
4
+ ### 0.1.6 2 apr 2014
5
+
6
+ * better testing of Responder#headers
7
+ * better handling of Responder#redirect
8
+ * add Base#request_headers helper
9
+ * rename Base.async -> Base.task, add Base#future
10
+
4
11
  ### 0.1.5 31 mar 2014
5
12
 
6
13
  * add WebsocketsArray#all
data/README.md CHANGED
@@ -20,8 +20,10 @@ Lots of work left to do!
20
20
 
21
21
  ```ruby
22
22
  require 'angelo'
23
+ require 'angelo/mustermann'
23
24
 
24
25
  class Foo < Angelo::Base
26
+ include Angelo::Mustermann
25
27
 
26
28
  TEST = {foo: "bar", baz: 123, bat: false}.to_json
27
29
 
@@ -74,14 +76,28 @@ class Foo < Angelo::Base
74
76
  websockets[:hearts] << ws
75
77
  end
76
78
 
77
- # this is a call to Base.async, defining the task
79
+ # this is a call to Base.task, defining the task
78
80
  # to perform on the reactor
79
81
  #
80
- async :hearts do
82
+ task :hearts do
81
83
  @@hearting = true
82
84
  every(10){ websockets[:hearts].each {|ws| ws.write HEART } }
83
85
  end
84
86
 
87
+ post '/in/:sec/sec/:msg' do
88
+
89
+ # this is a call to Base#future, telling the reactor
90
+ # do this thing and we'' want the value eventually
91
+ #
92
+ f = future :in_sec params[:sec], params[:msg]
93
+ f.value
94
+ end
95
+
96
+ task :in_sec do |sec, msg|
97
+ sleep sec.to_i
98
+ msg
99
+ end
100
+
85
101
  end
86
102
 
87
103
  Foo.run
data/lib/angelo/base.rb CHANGED
@@ -82,8 +82,8 @@ module Angelo
82
82
  WebsocketResponder.on_pong = block
83
83
  end
84
84
 
85
- def async name, &block
86
- Angelo::Server.define_action name, &block
85
+ def task name, &block
86
+ Angelo::Server.define_task name, &block
87
87
  end
88
88
 
89
89
  def websockets
@@ -112,6 +112,10 @@ module Angelo
112
112
  self.class.server.async.__send__ meth, *args
113
113
  end
114
114
 
115
+ def future meth, *args
116
+ self.class.server.future.__send__ meth, *args
117
+ end
118
+
115
119
  def params
116
120
  @params ||= case request.method
117
121
  when GET; parse_query_string
@@ -122,7 +126,18 @@ module Angelo
122
126
 
123
127
  def websockets; self.class.websockets; end
124
128
 
125
- async :handle_websocket do |ws|
129
+ def request_headers
130
+ @request_headers ||= Hash.new do |hash, key|
131
+ if Symbol === key
132
+ k = key.to_s.upcase
133
+ k.gsub! UNDERSCORE, DASH
134
+ rhv = request.headers.select {|header_key,v| header_key.upcase == k}
135
+ hash[key] = rhv.values.first
136
+ end
137
+ end
138
+ end
139
+
140
+ task :handle_websocket do |ws|
126
141
  begin
127
142
  while !ws.closed? do
128
143
  ws.read
@@ -132,7 +147,7 @@ module Angelo
132
147
  end
133
148
  end
134
149
 
135
- async :ping_websockets do
150
+ task :ping_websockets do
136
151
  every(@@ping_time) do
137
152
  websockets.all.each do |ws|
138
153
  ws.socket << ::WebSocket::Message.ping.to_data
@@ -46,6 +46,7 @@ module Angelo
46
46
 
47
47
  def request= request
48
48
  @params = nil
49
+ @redirect = nil
49
50
  @request = request
50
51
  handle_request
51
52
  respond
@@ -121,18 +122,21 @@ module Angelo
121
122
  when Hash
122
123
  raise 'html response requires String' if respond_with? :html
123
124
  @body.to_json if respond_with? :json
125
+ when NilClass
126
+ EMPTY_STRING
124
127
  end
125
- Angelo.log @connection, @request, nil, :ok, @body.size
126
- @connection.respond :ok, headers, @body
128
+
129
+ status = @redirect.nil? ? :ok : :moved_permanently
130
+ headers LOCATION_HEADER_KEY => @redirect if @redirect
131
+
132
+ Angelo.log @connection, @request, nil, status, @body.size
133
+ @connection.respond status, headers, @body
127
134
  rescue => e
128
135
  handle_error e, :internal_server_error, false
129
136
  end
130
137
 
131
138
  def redirect url
132
- Angelo.log @connection, @request, nil, :moved_permanently, 0
133
- @connection.respond :moved_permanently, headers(LOCATION_HEADER_KEY => url), ''
134
- rescue => e
135
- handle_error e, :internal_server_error, false
139
+ @redirect = url
136
140
  end
137
141
 
138
142
  end
data/lib/angelo/server.rb CHANGED
@@ -25,11 +25,11 @@ module Angelo
25
25
  # RubyProf.pause
26
26
  end
27
27
 
28
- def self.define_action name, &action
28
+ def self.define_task name, &action
29
29
  define_method name, &action
30
30
  end
31
31
 
32
- def self.remove_action name
32
+ def self.remove_task name
33
33
  remove_method name
34
34
  end
35
35
 
@@ -1,3 +1,3 @@
1
1
  module Angelo
2
- VERSION = '0.1.5'
2
+ VERSION = '0.1.6'
3
3
  end
data/lib/angelo.rb CHANGED
@@ -47,6 +47,10 @@ module Angelo
47
47
 
48
48
  DEFAULT_PING_TIME = 30
49
49
 
50
+ UNDERSCORE = '_'
51
+ DASH = '-'
52
+ EMPTY_STRING = ''
53
+
50
54
  def self.log connection, request, socket, status, body_size = '-'
51
55
 
52
56
  remote_ip = ->{
data/spec/angelo_spec.rb CHANGED
@@ -122,6 +122,36 @@ describe Angelo::Base do
122
122
 
123
123
  end
124
124
 
125
+ describe 'headers helper' do
126
+
127
+ headers_count = 0
128
+
129
+ define_app do
130
+
131
+ put '/incr' do
132
+ headers 'X-Http-Angelo-Server' => 'catbutt' if headers_count % 2 == 0
133
+ headers_count += 1
134
+ ''
135
+ end
136
+
137
+ end
138
+
139
+ it 'sets headers for a response' do
140
+ put '/incr'
141
+ expect(last_response.headers['X-Http-Angelo-Server']).to eq('catbutt')
142
+ end
143
+
144
+ it 'does not carry headers over responses' do
145
+ headers_count = 0
146
+ put '/incr'
147
+ expect(last_response.headers['X-Http-Angelo-Server']).to eq('catbutt')
148
+
149
+ put '/incr'
150
+ expect(last_response.headers['X-Http-Angelo-Server']).to be_nil
151
+ end
152
+
153
+ end
154
+
125
155
  describe 'content_type helper' do
126
156
 
127
157
  describe 'when in route block' do
@@ -315,4 +345,39 @@ describe Angelo::Base do
315
345
 
316
346
  end
317
347
 
348
+ describe 'request_headers helper' do
349
+
350
+ define_app do
351
+
352
+ get '/rh' do
353
+ content_type :json
354
+ { values: [
355
+ request_headers[params[:hk_1].to_sym],
356
+ request_headers[params[:hk_2].to_sym],
357
+ request_headers[params[:hk_3].to_sym]
358
+ ]
359
+ }
360
+ end
361
+
362
+ end
363
+
364
+ it 'matches snakecased symbols against case insensitive header keys' do
365
+ ps = {
366
+ hk_1: 'foo_bar',
367
+ hk_2: 'x_http_mozilla_ie_safari_puke',
368
+ hk_3: 'authorization'
369
+ }
370
+
371
+ hs = {
372
+ 'Foo-BAR' => 'abcdef',
373
+ 'X-HTTP-Mozilla-IE-Safari-PuKe' => 'ghijkl',
374
+ 'Authorization' => 'Bearer oauth_token_hi'
375
+ }
376
+
377
+ get '/rh', ps, hs
378
+ last_response_should_be_json 'values' => hs.values
379
+ end
380
+
381
+ end
382
+
318
383
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angelo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenichi Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-31 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: reel