angelo 0.1.0 → 0.1.1

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: 55ec6c0b7ba672c2c75bf6c18ea54e465dba68aa
4
- data.tar.gz: 60d95a47a93e1509782c9967c0fc42f0e5d0b7e0
3
+ metadata.gz: c49fed8026c10ee370aea064b5be14c7a38848b1
4
+ data.tar.gz: b56899e2f4ac2310c414b09bb2838357e223df6c
5
5
  SHA512:
6
- metadata.gz: 1bd103e7497922c6bf8489577f3f87d5bcbf046abf4524731b6c9c98c68f3791ca0dee51ed42232bd166d2c99ef9fc40d7f26fa1f8e6d19bbd62d4ffd9dee271
7
- data.tar.gz: 3a33ada93accc65b7843d6c910e075932b5f556fede208eea01405d10361f458976199c0cd8086fb3048330f4eda8c4553435773f66a8df12c1cb35ad2785f11
6
+ metadata.gz: bfcc2c164ab9d1908ff4f5c0557c115395d3ab48bd92c530d5040cac4e29c469c4a718cf15206e746d78da12d891bc01555a493b359b481bfed2c85987370a11
7
+ data.tar.gz: a586ff3c8271e29e7475b1c26a796240a0854e86f3c0449e7f099da02c95e725b0dd298623d9ecd7058bc4465c8061cee468cbf75bf12de32523ad35a64eca91
@@ -2,14 +2,19 @@ require 'cgi'
2
2
 
3
3
  module Angelo
4
4
 
5
+ class FormEncodingError < StandardError; end
6
+
5
7
  module ParamsParser
6
8
 
7
9
  EMPTY_JSON = '{}'
8
10
  SEMICOLON = ';'
11
+ EQUALS = '='
12
+ AMPERSAND = '&'
9
13
 
10
14
  def parse_formencoded str
11
- str.split('&').reduce(Responder.symhash) do |p, kv|
12
- key, value = kv.split('=').map {|s| CGI.unescape s}
15
+ raise FormEncodingError unless str.empty? or str.index EQUALS
16
+ str.split(AMPERSAND).reduce(Responder.symhash) do |p, kv|
17
+ key, value = kv.split(EQUALS).map {|s| CGI.unescape s}
13
18
  p[key] = value
14
19
  p
15
20
  end
@@ -21,14 +26,18 @@ module Angelo
21
26
 
22
27
  def parse_post_body
23
28
  body = request.body.to_s
29
+ qs = parse_query_string
24
30
  case
25
31
  when form_encoded?
26
32
  body = parse_formencoded body
33
+ qs.merge! body
27
34
  when json?
28
35
  body = EMPTY_JSON if body.empty?
29
36
  body = JSON.parse body
37
+ qs.merge! body
38
+ else
39
+ qs
30
40
  end
31
- parse_query_string.merge! body
32
41
  end
33
42
 
34
43
  def form_encoded?
@@ -40,7 +49,11 @@ module Angelo
40
49
  end
41
50
 
42
51
  def content_type? type
43
- request.headers[CONTENT_TYPE_HEADER_KEY].split(SEMICOLON).include? type
52
+ if request.headers[CONTENT_TYPE_HEADER_KEY]
53
+ request.headers[CONTENT_TYPE_HEADER_KEY].split(SEMICOLON).include? type
54
+ else
55
+ nil
56
+ end
44
57
  end
45
58
 
46
59
  end
@@ -59,14 +59,18 @@ module Angelo
59
59
  else
60
60
  raise NotImplementedError
61
61
  end
62
+ rescue JSON::ParserError => jpe
63
+ handle_error jpe, :bad_request, false
64
+ rescue FormEncodingError => fee
65
+ handle_error fee, :bad_request, false
62
66
  rescue => e
63
67
  handle_error e
64
68
  end
65
69
 
66
- def handle_error _error, report = true
70
+ def handle_error _error, type = :internal_server_error, report = true
67
71
  err_msg = error_message _error
68
- Angelo.log @connection, @request, nil, :internal_server_error, err_msg.size
69
- @connection.respond :internal_server_error, headers, err_msg
72
+ Angelo.log @connection, @request, nil, type, err_msg.size
73
+ @connection.respond type, headers, err_msg
70
74
  @connection.close
71
75
  if report
72
76
  error "#{_error.class} - #{_error.message}"
@@ -121,7 +125,7 @@ module Angelo
121
125
  Angelo.log @connection, @request, nil, :ok, @body.size
122
126
  @connection.respond :ok, headers, @body
123
127
  rescue => e
124
- handle_error e, false
128
+ handle_error e, :internal_server_error, false
125
129
  end
126
130
 
127
131
  end
@@ -52,7 +52,12 @@ module Angelo
52
52
 
53
53
  def erb view, opts = {locals: {}}
54
54
  locals = Hash === opts[:locals] ? opts[:locals] : {}
55
- render = ->{self.class.templates[view].render self, locals}
55
+ render = case view
56
+ when String
57
+ ->{ view }
58
+ when Symbol
59
+ ->{self.class.templates[view].render self, locals}
60
+ end
56
61
  case opts[:layout]
57
62
  when false
58
63
  render[]
@@ -1,3 +1,3 @@
1
1
  module Angelo
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
data/lib/angelo.rb CHANGED
@@ -35,8 +35,17 @@ module Angelo
35
35
  LOG_FORMAT = '%s - - "%s %s%s HTTP/%s" %d %s'
36
36
 
37
37
  def self.log connection, request, socket, status, body_size = '-'
38
+
39
+ remote_ip = ->{
40
+ if socket.nil?
41
+ connection.remote_ip rescue 'unknown'
42
+ else
43
+ socket.peeraddr(false)[3]
44
+ end
45
+ }
46
+
38
47
  Celluloid::Logger.debug LOG_FORMAT % [
39
- socket.nil? ? connection.remote_ip : socket.peeraddr(false)[3],
48
+ remote_ip[],
40
49
  request.method,
41
50
  request.path,
42
51
  request.query_string.nil? ? nil : '?'+request.query_string,
@@ -44,6 +53,7 @@ module Angelo
44
53
  Symbol === status ? HTTP::Response::SYMBOL_TO_STATUS_CODE[status] : status,
45
54
  body_size
46
55
  ]
56
+
47
57
  end
48
58
 
49
59
  end
@@ -68,4 +68,13 @@ describe Angelo::ParamsParser do
68
68
  parser.parse_post_body.should eq post_params
69
69
  end
70
70
 
71
+ it 'does not parse POST bodies if no Content-Type' do
72
+ parser.form_encoded = false
73
+ parser.json = false
74
+ parser.query_string = get_params
75
+ parser.body = nil
76
+ parser.parse_post_body.should eq params_s
77
+ parser.parse_query_string.should eq params_s
78
+ end
79
+
71
80
  end
data/spec/angelo_spec.rb CHANGED
@@ -37,8 +37,7 @@ describe Angelo::Base do
37
37
 
38
38
  it 'responds to get requests with json properly' do
39
39
  get '/json', obj
40
- string_vals = obj.keys.reduce({}){|h,k| h[k] = obj[k].to_s; h}
41
- last_response_should_be_json string_vals
40
+ last_response_should_be_json obj_s
42
41
  end
43
42
 
44
43
  it 'responds to post requests with json properly' do
@@ -277,6 +276,33 @@ describe Angelo::Base do
277
276
  end
278
277
 
279
278
  describe 'params helper' do
279
+
280
+ define_app do
281
+
282
+ [:get, :post].each do |m|
283
+ __send__ m, '/json' do
284
+ content_type :json
285
+ params
286
+ end
287
+ end
288
+
289
+ end
290
+
291
+ it 'parses formencoded body when content-type is formencoded' do
292
+ post '/json', obj, {'Content-Type' => Angelo::FORM_TYPE}
293
+ last_response_should_be_json obj_s
294
+ end
295
+
296
+ it 'does not parse JSON body when content-type is formencoded' do
297
+ post '/json', obj.to_json, {'Content-Type' => Angelo::FORM_TYPE}
298
+ last_response.status.should eq 400
299
+ end
300
+
301
+ it 'does not parse body when request content-type not set' do
302
+ post '/json', obj, {'Content-Type' => ''}
303
+ last_response_should_be_json({})
304
+ end
305
+
280
306
  end
281
307
 
282
308
  describe 'websockets helper' do
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.0
4
+ version: 0.1.1
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-02-24 00:00:00.000000000 Z
11
+ date: 2014-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: reel