cuba-api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +82 -0
- data/lib/cuba_api/accept_content.rb +71 -0
- data/lib/cuba_api/accept_content.rb~ +176 -0
- data/lib/cuba_api/config.rb +41 -0
- data/lib/cuba_api/config.rb~ +28 -0
- data/lib/cuba_api/current_user.rb +55 -0
- data/lib/cuba_api/current_user.rb~ +176 -0
- data/lib/cuba_api/guard.rb +37 -0
- data/lib/cuba_api/guard.rb~ +176 -0
- data/lib/cuba_api/serializer.rb +47 -0
- data/lib/cuba_api/serializer.rb~ +176 -0
- data/lib/cuba_api/write_aspect.rb +57 -0
- data/lib/cuba_api/write_aspect.rb~ +39 -0
- data/lib/cuba_api/write_aspects.rb~ +176 -0
- data/lib/cuba_api.rb +40 -0
- data/lib/cuba_api.rb~ +28 -0
- data/spec/accept_spec.rb +57 -0
- data/spec/aspects_spec.rb +42 -0
- data/spec/config_spec.rb +34 -0
- data/spec/current_user_spec.rb +43 -0
- data/spec/serializer_spec.rb +42 -0
- metadata +152 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Kristian Meier
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
cuba-api
|
2
|
+
========
|
3
|
+
|
4
|
+
* [![Build Status](https://secure.travis-ci.org/mkristian/cuba-api.png)](http://travis-ci.org/mkristian/cuba-api)
|
5
|
+
* [![Dependency Status](https://gemnasium.com/mkristian/cuba-api.png)](https://gemnasium.com/mkristian/cuba-api)
|
6
|
+
* [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/mkristian/cuba-api)
|
7
|
+
|
8
|
+
these are just a handful for [cuba](https://github.com/soveran/cuba) to use cuba as API server.
|
9
|
+
|
10
|
+
cuba\_-api/config.rb
|
11
|
+
------------------
|
12
|
+
|
13
|
+
this plugin adds configuration to cuba which inherits from its superclass. this is very similar to `Cuba.settings`.
|
14
|
+
|
15
|
+
the short comng of `Cuba.settings`are
|
16
|
+
|
17
|
+
* data needs to be marshalable, i.e. needs Marshal.load(Marshal.dump(obj)) to work
|
18
|
+
|
19
|
+
* inheritence takes place on class parsing. when you first require all your **cubas** and then set your settings in the root **cuba** then the children will not see this settings. set the data like this
|
20
|
+
|
21
|
+
CubaAPI[ :mykey ] = "mydata"
|
22
|
+
puts CubaAPI[ :mykey ]
|
23
|
+
|
24
|
+
cuba\_api/write_aspect.rb
|
25
|
+
-------------------------
|
26
|
+
|
27
|
+
first you write out response data with `write( data, options )` instead of using the response object from cuba. then a plugin can register an aspect which is basically a method which has the same arguments as write. now write will call each of the aspects using the result of the aspect as new data object, i.e. chaining those aspects.
|
28
|
+
|
29
|
+
cuba\_api/serializer.rb
|
30
|
+
-------------------------
|
31
|
+
|
32
|
+
aspect which wraps the data object into a serializer which has `to_json`, `to_yaml` and `to_xml` as configured. see also [ixtlan-babel](https://github.com/mkristian/ixtlan-babel). to configure these methods just **require 'json'** or **require 'yaml'** or any xml lbrary which offers a `to_xml` method on the `Hash` class. dito use an json library which does offer `to_json` on `Hash`.
|
33
|
+
|
34
|
+
cuba\_api/accept_content.rb
|
35
|
+
----------------------------
|
36
|
+
|
37
|
+
this aspect looks first at the file extension of the path part of the uri and the on request header **Accept** to determine the content type the response shall deliver. it will set the respective content-type as well calls `to_json`, '`to_yaml` or `to_xml` respectively. this aspect plays well with a preceeding serializer aspect ;)
|
38
|
+
|
39
|
+
|
40
|
+
cuba\_api/current_user.rb
|
41
|
+
--------------------------
|
42
|
+
|
43
|
+
some helper methods to manage a current_user n the session. needs a session-manager in `CubaAPI[ :sessions ]` which respond to `to_session( user_object )` and `from_session( session_data )`. could be something like
|
44
|
+
|
45
|
+
class SessionManager
|
46
|
+
def to_session( user_object ) user_object.id; end
|
47
|
+
def from_session( session_data ) User.find_by_id( hash_data ); end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
cuba\_api/guard.rb
|
52
|
+
--------------------------
|
53
|
+
|
54
|
+
simple authorization which assumes a user belongs to many groups and group has a name attribute. now the cuba "routing" can use this
|
55
|
+
|
56
|
+
on allowed?( :root, :admin ), get, 'configuration' do
|
57
|
+
# retrieve configuration allowed by root and admin
|
58
|
+
end
|
59
|
+
|
60
|
+
on allowed?( :root ), put, 'configuration' do
|
61
|
+
# update configuration only allowed by root
|
62
|
+
end
|
63
|
+
|
64
|
+
Pending
|
65
|
+
-------
|
66
|
+
|
67
|
+
request payload needs to parse from json, yaml or xml into a hash.
|
68
|
+
|
69
|
+
Contributing
|
70
|
+
------------
|
71
|
+
|
72
|
+
1. Fork it
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create new Pull Request
|
77
|
+
|
78
|
+
meta-fu
|
79
|
+
-------
|
80
|
+
|
81
|
+
enjoy :)
|
82
|
+
|
@@ -0,0 +1,71 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2012 Christian Meier
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
5
|
+
# this software and associated documentation files (the "Software"), to deal in
|
6
|
+
# the Software without restriction, including without limitation the rights to
|
7
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
8
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
9
|
+
# subject to the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be included in all
|
12
|
+
# copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
16
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
17
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
18
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
19
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
#
|
21
|
+
# -*- Coding: utf-8 -*-
|
22
|
+
module CubaApi
|
23
|
+
module AcceptContent
|
24
|
+
|
25
|
+
module ClassMethods
|
26
|
+
|
27
|
+
MIMES = { :yaml => ['application/x-yaml', 'text/yaml'],
|
28
|
+
:json => ['application/json'],
|
29
|
+
:xml => ['application/xml'] }
|
30
|
+
|
31
|
+
def accept( *args )
|
32
|
+
args.each do |arg|
|
33
|
+
(MIMES[ arg ] || []).each do |mime|
|
34
|
+
mimes[ mime ] = "to_#{arg}".to_sym
|
35
|
+
end
|
36
|
+
end
|
37
|
+
warn "[CubaAPI] Accept: #{mimes.keys.join(', ')}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def mimes
|
41
|
+
self[ :mimes ] ||= {}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def accept_content( obj, options = {} )
|
46
|
+
script = env[ 'SCRIPT_NAME' ]
|
47
|
+
if script =~ /\./
|
48
|
+
extension = script.sub( /^.*\./, '' )
|
49
|
+
mime = ClassMethods::MIMES[ extension.to_sym ] || []
|
50
|
+
_accept( obj, mime.first )
|
51
|
+
else
|
52
|
+
_accept( obj, env[ 'HTTP_ACCEPT' ] )
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def _accept( obj, mime )
|
57
|
+
if self.class.mimes.key?( mime )
|
58
|
+
res[ "Content-Type" ] = mime + "; charset=utf-8"
|
59
|
+
obj.send self.class[ :mimes ][ mime ]
|
60
|
+
else
|
61
|
+
head 404
|
62
|
+
nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
private :_accept
|
66
|
+
|
67
|
+
def self.included( base )
|
68
|
+
base.append_aspect :accept_content
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
# -*- Coding: utf-8 -*-
|
2
|
+
require "cuba"
|
3
|
+
require 'ixtlan/babel/factory'
|
4
|
+
|
5
|
+
module CubaApi
|
6
|
+
module WriteAspect
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def append_aspect( arg )
|
10
|
+
aspects << arg
|
11
|
+
warn "[CubaAPI] Appended aspect #{arg}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def prepend_aspect( arg )
|
15
|
+
aspects.insert( 0, arg )
|
16
|
+
warn "[CubaAPI] Prepended aspect #{arg}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def aspects
|
20
|
+
self[ :aspects ] ||= []
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def head( status )
|
25
|
+
res.status = status
|
26
|
+
res.write ''
|
27
|
+
end
|
28
|
+
|
29
|
+
def write( obj, args = {} )
|
30
|
+
self.res.status = args[:status] || 200
|
31
|
+
# make sure we inherit aspects and repsect the order
|
32
|
+
aspects = self.class == CubaAPI ? [] : self.class.superclass[ :aspects ]
|
33
|
+
(aspects + self.class[ :aspects ]).uniq.each do |w|
|
34
|
+
obj = send( w, obj, args ) if obj
|
35
|
+
end
|
36
|
+
res.write obj.to_s
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
module Serializer
|
41
|
+
|
42
|
+
def serializer_factory
|
43
|
+
@_factory ||= Ixtlan::Babel::Factory.new
|
44
|
+
end
|
45
|
+
private :serializer_factory
|
46
|
+
|
47
|
+
def serializer( obj, args = {})
|
48
|
+
if args[:serializer] == false || obj.is_a?( String )
|
49
|
+
obj
|
50
|
+
else
|
51
|
+
s = serializer_factory.new( obj )
|
52
|
+
s.use( args[ :use ] ) if args[ :use ]
|
53
|
+
s
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.included( base )
|
58
|
+
base.append_aspect :serializer
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
module CurrentUser
|
63
|
+
|
64
|
+
module ClassMethods
|
65
|
+
|
66
|
+
def sessions
|
67
|
+
self[ :sessions ]
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
def current_user( user = nil )
|
73
|
+
if user
|
74
|
+
session['user'] = self.class.sessions.to_session( user )
|
75
|
+
@_current_user = user
|
76
|
+
else
|
77
|
+
@_current_user ||= self.class.sessions.from_session( session['user'] )
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def reset_current_user
|
82
|
+
session[ 'user' ] = nil
|
83
|
+
end
|
84
|
+
|
85
|
+
def authenticated?
|
86
|
+
session[ 'user' ] != nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def current_user_name
|
90
|
+
authenticated? ? current_user.login : "???"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
module Guard
|
95
|
+
def allowed?( *group_names )
|
96
|
+
authenticated? && ( allowed_groups( *group_names ).size > 0 )
|
97
|
+
end
|
98
|
+
|
99
|
+
def allowed_groups( *group_names )
|
100
|
+
current_groups.select { |g| group_names.member?( g.name ) }
|
101
|
+
end
|
102
|
+
|
103
|
+
def current_groups
|
104
|
+
current_user.groups
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
module AcceptContent
|
109
|
+
|
110
|
+
module ClassMethods
|
111
|
+
|
112
|
+
MIMES = { :yaml => ['application/x-yaml', 'text/yaml'],
|
113
|
+
:json => ['application/json'],
|
114
|
+
:xml => ['application/xml'] }
|
115
|
+
|
116
|
+
def accept( *args )
|
117
|
+
args.each do |arg|
|
118
|
+
(MIMES[ arg ] || []).each do |mime|
|
119
|
+
mimes[ mime ] = "to_#{arg}".to_sym
|
120
|
+
end
|
121
|
+
end
|
122
|
+
warn "[CubaAPI] Accept: #{mimes.keys.join(', ')}"
|
123
|
+
end
|
124
|
+
|
125
|
+
def mimes
|
126
|
+
self[ :mimes ] ||= {}
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def accept_content( obj, options = {} )
|
131
|
+
script = env[ 'SCRIPT_NAME' ]
|
132
|
+
if script =~ /\./
|
133
|
+
extension = script.sub( /^.*\./, '' )
|
134
|
+
mime = ClassMethods::MIMES[ extension.to_sym ] || []
|
135
|
+
accept( obj, mime.first )
|
136
|
+
else
|
137
|
+
accept( obj, env[ 'HTTP_ACCEPT' ] )
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def accept( obj, mime )
|
142
|
+
if self.class[ :mimes ].key?( mime )
|
143
|
+
res[ "Content-Type" ] = mime + "; charset=utf-8"
|
144
|
+
obj.send self.class[ :mimes ][ mime ]
|
145
|
+
else
|
146
|
+
head 404
|
147
|
+
nil
|
148
|
+
end
|
149
|
+
end
|
150
|
+
private :accept
|
151
|
+
|
152
|
+
def self.included( base )
|
153
|
+
base.append_aspect :accept_content
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
class CubaAPI < Cuba
|
159
|
+
def self.map
|
160
|
+
@map ||= {}
|
161
|
+
end
|
162
|
+
|
163
|
+
def self.[]( key )
|
164
|
+
map[ key ] || settings[ key ] || (superclass == Cuba ? Cuba.settings[ key ] : superclass[ key ])
|
165
|
+
end
|
166
|
+
|
167
|
+
def self.[]=( key, value )
|
168
|
+
map[ key ] = value
|
169
|
+
end
|
170
|
+
|
171
|
+
plugin Ixtlan::Write
|
172
|
+
plugin Ixtlan::Serializer
|
173
|
+
plugin Ixtlan::AcceptContent
|
174
|
+
plugin Ixtlan::CurrentUser
|
175
|
+
plugin Ixtlan::Guard
|
176
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2012 Christian Meier
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
5
|
+
# this software and associated documentation files (the "Software"), to deal in
|
6
|
+
# the Software without restriction, including without limitation the rights to
|
7
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
8
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
9
|
+
# subject to the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be included in all
|
12
|
+
# copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
16
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
17
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
18
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
19
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
#
|
21
|
+
# -*- Coding: utf-8 -*-
|
22
|
+
require "cuba"
|
23
|
+
|
24
|
+
module CubaApi
|
25
|
+
module Config
|
26
|
+
module ClassMethods
|
27
|
+
|
28
|
+
def config
|
29
|
+
@config ||= {}
|
30
|
+
end
|
31
|
+
|
32
|
+
def []( key )
|
33
|
+
config[ key ] || settings[ key ] || (superclass.respond_to?( :[] ) ? superclass[ key ] : (superclass == Cuba ? Cuba.settings[ key ] : nil ) )
|
34
|
+
end
|
35
|
+
|
36
|
+
def []=( key, value )
|
37
|
+
config[ key ] = value
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- Coding: utf-8 -*-
|
2
|
+
require "cuba"
|
3
|
+
|
4
|
+
require 'cuba_api/write_aspect'
|
5
|
+
require 'cuba_api/serializer'
|
6
|
+
require 'cuba_api/current_user'
|
7
|
+
require 'cuba_api/guard'
|
8
|
+
require 'cuba_api/accept_content'
|
9
|
+
|
10
|
+
class CubaAPI < Cuba
|
11
|
+
def self.map
|
12
|
+
@map ||= {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.[]( key )
|
16
|
+
map[ key ] || settings[ key ] || (superclass == Cuba ? Cuba.settings[ key ] : superclass[ key ])
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.[]=( key, value )
|
20
|
+
map[ key ] = value
|
21
|
+
end
|
22
|
+
|
23
|
+
plugin CubaApi::WriteAspect
|
24
|
+
plugin CubaApi::Serializer
|
25
|
+
plugin CubaApi::AcceptContent
|
26
|
+
plugin CubaApi::CurrentUser
|
27
|
+
plugin CubaApi::Guard
|
28
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2012 Christian Meier
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
5
|
+
# this software and associated documentation files (the "Software"), to deal in
|
6
|
+
# the Software without restriction, including without limitation the rights to
|
7
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
8
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
9
|
+
# subject to the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be included in all
|
12
|
+
# copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
16
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
17
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
18
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
19
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
#
|
21
|
+
# -*- Coding: utf-8 -*-
|
22
|
+
|
23
|
+
module CubaApi
|
24
|
+
module CurrentUser
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
|
28
|
+
def sessions
|
29
|
+
self[ :sessions ]
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def current_user( user = nil )
|
35
|
+
if user
|
36
|
+
session['user'] = self.class.sessions.to_session( user )
|
37
|
+
@_current_user = user
|
38
|
+
else
|
39
|
+
@_current_user ||= self.class.sessions.from_session( session['user'] )
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def reset_current_user
|
44
|
+
session[ 'user' ] = nil
|
45
|
+
end
|
46
|
+
|
47
|
+
def authenticated?
|
48
|
+
current_user != nil
|
49
|
+
end
|
50
|
+
|
51
|
+
def current_user_name
|
52
|
+
authenticated? ? current_user.login : "???"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
# -*- Coding: utf-8 -*-
|
2
|
+
require "cuba"
|
3
|
+
require 'ixtlan/babel/factory'
|
4
|
+
|
5
|
+
module CubaApi
|
6
|
+
module WriteAspect
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def append_aspect( arg )
|
10
|
+
aspects << arg
|
11
|
+
warn "[CubaAPI] Appended aspect #{arg}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def prepend_aspect( arg )
|
15
|
+
aspects.insert( 0, arg )
|
16
|
+
warn "[CubaAPI] Prepended aspect #{arg}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def aspects
|
20
|
+
self[ :aspects ] ||= []
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def head( status )
|
25
|
+
res.status = status
|
26
|
+
res.write ''
|
27
|
+
end
|
28
|
+
|
29
|
+
def write( obj, args = {} )
|
30
|
+
self.res.status = args[:status] || 200
|
31
|
+
# make sure we inherit aspects and repsect the order
|
32
|
+
aspects = self.class == CubaAPI ? [] : self.class.superclass[ :aspects ]
|
33
|
+
(aspects + self.class[ :aspects ]).uniq.each do |w|
|
34
|
+
obj = send( w, obj, args ) if obj
|
35
|
+
end
|
36
|
+
res.write obj.to_s
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
module Serializer
|
41
|
+
|
42
|
+
def serializer_factory
|
43
|
+
@_factory ||= Ixtlan::Babel::Factory.new
|
44
|
+
end
|
45
|
+
private :serializer_factory
|
46
|
+
|
47
|
+
def serializer( obj, args = {})
|
48
|
+
if args[:serializer] == false || obj.is_a?( String )
|
49
|
+
obj
|
50
|
+
else
|
51
|
+
s = serializer_factory.new( obj )
|
52
|
+
s.use( args[ :use ] ) if args[ :use ]
|
53
|
+
s
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.included( base )
|
58
|
+
base.append_aspect :serializer
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
module CurrentUser
|
63
|
+
|
64
|
+
module ClassMethods
|
65
|
+
|
66
|
+
def sessions
|
67
|
+
self[ :sessions ]
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
def current_user( user = nil )
|
73
|
+
if user
|
74
|
+
session['user'] = self.class.sessions.to_session( user )
|
75
|
+
@_current_user = user
|
76
|
+
else
|
77
|
+
@_current_user ||= self.class.sessions.from_session( session['user'] )
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def reset_current_user
|
82
|
+
session[ 'user' ] = nil
|
83
|
+
end
|
84
|
+
|
85
|
+
def authenticated?
|
86
|
+
session[ 'user' ] != nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def current_user_name
|
90
|
+
authenticated? ? current_user.login : "???"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
module Guard
|
95
|
+
def allowed?( *group_names )
|
96
|
+
authenticated? && ( allowed_groups( *group_names ).size > 0 )
|
97
|
+
end
|
98
|
+
|
99
|
+
def allowed_groups( *group_names )
|
100
|
+
current_groups.select { |g| group_names.member?( g.name ) }
|
101
|
+
end
|
102
|
+
|
103
|
+
def current_groups
|
104
|
+
current_user.groups
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
module AcceptContent
|
109
|
+
|
110
|
+
module ClassMethods
|
111
|
+
|
112
|
+
MIMES = { :yaml => ['application/x-yaml', 'text/yaml'],
|
113
|
+
:json => ['application/json'],
|
114
|
+
:xml => ['application/xml'] }
|
115
|
+
|
116
|
+
def accept( *args )
|
117
|
+
args.each do |arg|
|
118
|
+
(MIMES[ arg ] || []).each do |mime|
|
119
|
+
mimes[ mime ] = "to_#{arg}".to_sym
|
120
|
+
end
|
121
|
+
end
|
122
|
+
warn "[CubaAPI] Accept: #{mimes.keys.join(', ')}"
|
123
|
+
end
|
124
|
+
|
125
|
+
def mimes
|
126
|
+
self[ :mimes ] ||= {}
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def accept_content( obj, options = {} )
|
131
|
+
script = env[ 'SCRIPT_NAME' ]
|
132
|
+
if script =~ /\./
|
133
|
+
extension = script.sub( /^.*\./, '' )
|
134
|
+
mime = ClassMethods::MIMES[ extension.to_sym ] || []
|
135
|
+
accept( obj, mime.first )
|
136
|
+
else
|
137
|
+
accept( obj, env[ 'HTTP_ACCEPT' ] )
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def accept( obj, mime )
|
142
|
+
if self.class[ :mimes ].key?( mime )
|
143
|
+
res[ "Content-Type" ] = mime + "; charset=utf-8"
|
144
|
+
obj.send self.class[ :mimes ][ mime ]
|
145
|
+
else
|
146
|
+
head 404
|
147
|
+
nil
|
148
|
+
end
|
149
|
+
end
|
150
|
+
private :accept
|
151
|
+
|
152
|
+
def self.included( base )
|
153
|
+
base.append_aspect :accept_content
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
class CubaAPI < Cuba
|
159
|
+
def self.map
|
160
|
+
@map ||= {}
|
161
|
+
end
|
162
|
+
|
163
|
+
def self.[]( key )
|
164
|
+
map[ key ] || settings[ key ] || (superclass == Cuba ? Cuba.settings[ key ] : superclass[ key ])
|
165
|
+
end
|
166
|
+
|
167
|
+
def self.[]=( key, value )
|
168
|
+
map[ key ] = value
|
169
|
+
end
|
170
|
+
|
171
|
+
plugin Ixtlan::Write
|
172
|
+
plugin Ixtlan::Serializer
|
173
|
+
plugin Ixtlan::AcceptContent
|
174
|
+
plugin Ixtlan::CurrentUser
|
175
|
+
plugin Ixtlan::Guard
|
176
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2012 Christian Meier
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
5
|
+
# this software and associated documentation files (the "Software"), to deal in
|
6
|
+
# the Software without restriction, including without limitation the rights to
|
7
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
8
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
9
|
+
# subject to the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be included in all
|
12
|
+
# copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
16
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
17
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
18
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
19
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
#
|
21
|
+
# -*- Coding: utf-8 -*-
|
22
|
+
|
23
|
+
module CubaApi
|
24
|
+
module Guard
|
25
|
+
def allowed?( *group_names )
|
26
|
+
authenticated? && ( allowed_groups( *group_names ).size > 0 )
|
27
|
+
end
|
28
|
+
|
29
|
+
def allowed_groups( *group_names )
|
30
|
+
current_groups.select { |g| group_names.member?( g.name ) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def current_groups
|
34
|
+
current_user.groups
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|