cuba-api 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -51,7 +51,7 @@ module CubaApi
51
51
  res[ "Content-Type" ] = mime + "; charset=utf-8"
52
52
  obj.send self.class[ :mimes ][ mime ]
53
53
  else
54
- head :not_found
54
+ no_body :not_found
55
55
  nil
56
56
  end
57
57
  end
@@ -143,7 +143,7 @@ module CubaApi
143
143
  res[ "Content-Type" ] = mime + "; charset=utf-8"
144
144
  obj.send self.class[ :mimes ][ mime ]
145
145
  else
146
- head 404
146
+ no_body 404
147
147
  nil
148
148
  end
149
149
  end
@@ -1,113 +1,117 @@
1
- class CORS
1
+ require 'uri'
2
+ module CubaApi
3
+ class CORS
2
4
 
3
- DEFAULT_METHODS = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE' ]
5
+ DEFAULT_METHODS = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE' ]
4
6
 
5
- def initialize( options, &block )
6
- @options = options
7
- # only for inspect
8
- @config = options.config
9
- # set default max_ago
10
- self.max_age = 60 * 60 * 24 # one day
11
- block.call self if block
12
- end
7
+ def initialize( options, &block )
8
+ @options = options
9
+ # only for inspect
10
+ @config = options.config
11
+ # set default max_ago
12
+ self.max_age = 60 * 60 * 24 # one day
13
+ block.call self if block
14
+ end
13
15
 
14
- def config
15
- @config
16
- end
16
+ def config
17
+ @config
18
+ end
17
19
 
18
- def method_missing( method, *args )
19
- m = method.to_s
20
- if m.match /^_/
21
- if m =~ /=$/
22
- @options[ "cors_#{m[1..-2]}".to_sym ] = args.flatten
20
+ def method_missing( method, *args )
21
+ m = method.to_s
22
+ if m.match /^_/
23
+ if m =~ /=$/
24
+ @options[ "cors_#{m[1..-2]}".to_sym ] = args.flatten
25
+ else
26
+ @options[ "cors_#{m[1..-1]}".to_sym ]
27
+ end
23
28
  else
24
- @options[ "cors_#{m[1..-1]}".to_sym ]
29
+ super
25
30
  end
26
- else
27
- super
28
31
  end
29
- end
30
-
31
- def max_age=( max )
32
- @options[ :cors_max_age ] = max
33
- end
34
-
35
- def expose=( expose )
36
- self._expose = expose
37
- end
38
32
 
39
- def origins=( *origins )
40
- self._origins = [ origins ].flatten
41
- end
42
-
43
- def origins( domain )
44
- origins = self._origins
45
- if origins == [ '*' ] || origins.nil? || origins.member?( domain )
46
- domain
33
+ def max_age=( max )
34
+ @options[ :cors_max_age ] = max
47
35
  end
48
- end
49
-
50
- def methods=( *methods )
51
- self._methods = [ methods ].flatten.collect{ |h| h.to_s.upcase }
52
- end
53
-
54
- def methods( method, methods = nil )
55
- methods = methods.collect { |m| m.to_s.upcase } if methods
56
- if (methods || self._methods || DEFAULT_METHODS).member?( method.to_s.upcase )
57
- methods || self._methods || DEFAULT_METHODS
36
+
37
+ def expose=( expose )
38
+ self._expose = expose
58
39
  end
59
- end
60
40
 
61
- def allowed?( methods )
62
- if methods
63
- methods = methods.collect { |m| m.to_s.upcase }
64
- ( ( self._methods || DEFAULT_METHODS ) & methods ).size > 0
65
- else
66
- true
41
+ def origins=( *origins )
42
+ self._origins = [ origins ].flatten
43
+ end
44
+
45
+ def origins( domain )
46
+ if domain
47
+ host = URI.parse( domain ).host
48
+ origins = self._origins
49
+ if origins == [ '*' ] || origins.nil? || origins.member?( host )
50
+ domain
51
+ end
52
+ end
67
53
  end
68
- end
69
54
 
70
- def headers=( *headers )
71
- self._headers = [ headers ].flatten.collect{ |h| h.to_s.upcase }
72
- end
55
+ def methods=( *methods )
56
+ self._methods = [ methods ].flatten.collect{ |h| h.to_s.upcase }
57
+ end
58
+
59
+ def methods( method, methods = nil )
60
+ methods = methods.collect { |m| m.to_s.upcase } if methods
61
+ if (methods || self._methods || DEFAULT_METHODS).member?( method.to_s.upcase )
62
+ methods || self._methods || DEFAULT_METHODS
63
+ end
64
+ end
73
65
 
74
- def headers( headers )
75
- # return headers as they come in when not configured
76
- headers = headers.split( /,\s+/ ) if headers
77
- if self._headers && headers
78
- given = headers.collect{ |h| h.to_s.upcase }
79
- # give back the allowed subset of the given headers
80
- result = given & self._headers
81
- result = nil if result.empty?
82
- result
83
- else
84
- headers
66
+ def allowed?( methods )
67
+ if methods
68
+ methods = methods.collect { |m| m.to_s.upcase }
69
+ ( ( self._methods || DEFAULT_METHODS ) & methods ).size > 0
70
+ else
71
+ true
72
+ end
85
73
  end
86
- end
87
74
 
88
- def allow_origin( req, res )
89
- if orig = origins( req[ 'HTTP_ORIGIN' ] )
90
- res[ 'Access-Control-Allow-Origin' ] = orig
75
+ def headers=( *headers )
76
+ self._headers = [ headers ].flatten.collect{ |h| h.to_s.upcase }
91
77
  end
92
- end
93
78
 
94
- def process( req, res, methods )
95
- allow_origin( req, res )
96
- res[ 'Access-Control-Max-Age' ] = _max_age.to_s if _max_age
97
- if m = methods( req[ 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' ], methods )
98
- res[ 'Access-Control-Allow-Methods' ] = m.join( ', ' )
79
+ def headers( headers )
80
+ # return headers as they come in when not configured
81
+ headers = headers.split( /,\s+/ ) if headers
82
+ if self._headers && headers
83
+ given = headers.collect{ |h| h.to_s.upcase }
84
+ # give back the allowed subset of the given headers
85
+ result = given & self._headers
86
+ result = nil if result.empty?
87
+ result
88
+ else
89
+ headers
90
+ end
99
91
  end
100
- if h = headers( req[ 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' ] )
101
- res[ 'Access-Control-Allow-Headers' ] = h.join( ', ' )
92
+
93
+ def allow_origin( req, res )
94
+ if orig = origins( req[ 'HTTP_ORIGIN' ] )
95
+ res[ 'Access-Control-Allow-Origin' ] = orig
96
+ end
102
97
  end
103
- unless _expose.nil? || _expose.empty?
104
- res[ 'Access-Control-Expose-Headers' ] = _expose.join( ', ' )
98
+
99
+ def process( req, res, methods )
100
+ allow_origin( req, res )
101
+ res[ 'Access-Control-Max-Age' ] = _max_age.to_s if _max_age
102
+ if m = methods( req[ 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' ], methods )
103
+ res[ 'Access-Control-Allow-Methods' ] = m.join( ', ' )
104
+ end
105
+ if h = headers( req[ 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' ] )
106
+ res[ 'Access-Control-Allow-Headers' ] = h.join( ', ' )
107
+ end
108
+ unless _expose.nil? || _expose.empty?
109
+ res[ 'Access-Control-Expose-Headers' ] = _expose.join( ', ' )
110
+ end
111
+ res.status = 200
105
112
  end
106
- res.status = 200
107
113
  end
108
- end
109
114
 
110
- module CubaApi
111
115
  module Cors
112
116
  module ClassMethods
113
117
 
@@ -143,7 +143,7 @@ module CubaApi
143
143
  res[ "Content-Type" ] = mime + "; charset=utf-8"
144
144
  obj.send self.class[ :mimes ][ mime ]
145
145
  else
146
- head 404
146
+ no_body 404
147
147
  nil
148
148
  end
149
149
  end
@@ -143,7 +143,7 @@ module CubaApi
143
143
  res[ "Content-Type" ] = mime + "; charset=utf-8"
144
144
  obj.send self.class[ :mimes ][ mime ]
145
145
  else
146
- head 404
146
+ no_body 404
147
147
  nil
148
148
  end
149
149
  end
@@ -133,7 +133,7 @@ puts req.request_method
133
133
  # config = Configuration.optimistic_get( updated_at,
134
134
  # id )
135
135
  # config.destroy
136
- # head 200
136
+ # no_body 200
137
137
  # end
138
138
 
139
139
  end
@@ -143,7 +143,7 @@ module CubaApi
143
143
  res[ "Content-Type" ] = mime + "; charset=utf-8"
144
144
  obj.send self.class[ :mimes ][ mime ]
145
145
  else
146
- head 404
146
+ no_body 404
147
147
  nil
148
148
  end
149
149
  end
@@ -39,7 +39,7 @@ module CubaApi
39
39
  end
40
40
  end
41
41
 
42
- def head( status )
42
+ def no_body( status )
43
43
  res.status = Rack::Utils.status_code( status )
44
44
  res.write Rack::Utils::HTTP_STATUS_CODES[ res.status ]
45
45
  res['Content-Type' ] = 'text/plain'
@@ -143,7 +143,7 @@ module CubaApi
143
143
  res[ "Content-Type" ] = mime + "; charset=utf-8"
144
144
  obj.send self.class[ :mimes ][ mime ]
145
145
  else
146
- head 404
146
+ no_body 404
147
147
  nil
148
148
  end
149
149
  end
@@ -14,7 +14,7 @@ describe CubaApi::Cors do
14
14
  cors.max_age = 123
15
15
  cors.methods = :put
16
16
  cors.headers = 'x-requested-with'
17
- cors.origins = 'http://middleearth'
17
+ cors.origins = 'middleearth'
18
18
  cors.expose = 'x-requested-with'
19
19
  end
20
20
  define do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuba-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
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-09-25 00:00:00.000000000 Z
12
+ date: 2013-10-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cuba