cuba-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,47 @@
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 'ixtlan/babel/factory'
23
+
24
+ module CubaApi
25
+ module Serializer
26
+
27
+ module ClassMethods
28
+ def serializer_factory
29
+ @_factory ||= Ixtlan::Babel::Factory.new
30
+ end
31
+ end
32
+
33
+ def serializer( obj, options = {})
34
+ if options[:serializer] == false || obj.is_a?( String )
35
+ obj
36
+ else
37
+ s = self.class.serializer_factory.new( obj )
38
+ s.use( options[ :use ] ) if options[ :use ]
39
+ s
40
+ end
41
+ end
42
+
43
+ def self.included( base )
44
+ base.append_aspect :serializer
45
+ end
46
+ end
47
+ 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,57 @@
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 WriteAspect
25
+
26
+ module ClassMethods
27
+ def append_aspect( arg )
28
+ aspects << arg
29
+ warn "[CubaAPI] Appended aspect #{arg}"
30
+ end
31
+
32
+ def prepend_aspect( arg )
33
+ aspects.insert( 0, arg )
34
+ warn "[CubaAPI] Prepended aspect #{arg}"
35
+ end
36
+
37
+ def aspects
38
+ self[ :aspects ] ||= []
39
+ end
40
+ end
41
+
42
+ def head( status )
43
+ res.status = status
44
+ res.write ''
45
+ end
46
+
47
+ def write( obj, options = {} )
48
+ self.res.status = options[:status] || 200
49
+ # make sure we inherit aspects and repsect the order
50
+ aspects = self.class[ :aspects ] # == CubaAPI ? [] : self.class.superclass[ :aspects ]
51
+ (aspects + self.class[ :aspects ]).uniq.each do |w|
52
+ obj = send( w, obj, options ) if obj
53
+ end
54
+ res.write obj.to_s
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,39 @@
1
+ # -*- Coding: utf-8 -*-
2
+ require "cuba"
3
+ require 'ixtlan/babel/factory'
4
+
5
+ module CubaApi
6
+ module WriteAspects
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
+ 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
data/lib/cuba_api.rb ADDED
@@ -0,0 +1,40 @@
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
+ require 'cuba_api/write_aspect'
25
+ require 'cuba_api/serializer'
26
+ require 'cuba_api/current_user'
27
+ require 'cuba_api/guard'
28
+ require 'cuba_api/accept_content'
29
+ require 'cuba_api/config'
30
+
31
+ class CubaAPI < Cuba
32
+
33
+ plugin CubaApi::Config
34
+ plugin CubaApi::WriteAspect
35
+ plugin CubaApi::Serializer
36
+ plugin CubaApi::AcceptContent
37
+ plugin CubaApi::CurrentUser
38
+ plugin CubaApi::Guard
39
+
40
+ end