cuba-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cuba_api.rb~ ADDED
@@ -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,57 @@
1
+ require 'spec_helper'
2
+ require 'cuba_api/write_aspect'
3
+ require 'cuba_api/accept_content'
4
+
5
+ class B
6
+ def method_missing( method, *args )
7
+ method.to_s
8
+ end
9
+ end
10
+
11
+ describe CubaApi::AcceptContent do
12
+
13
+ before do
14
+ Cuba.reset!
15
+ Cuba.plugin CubaApi::Config
16
+ Cuba[ :aspects ] = []
17
+ Cuba.plugin CubaApi::WriteAspect
18
+ Cuba.plugin CubaApi::AcceptContent
19
+ Cuba.accept :yaml
20
+ Cuba.define do
21
+ on default do
22
+ write B.new
23
+ end
24
+ end
25
+ end
26
+
27
+ it 'creates yaml' do
28
+ _, _, resp = Cuba.call({"SCRIPT_NAME" => "/bla.yaml"})
29
+ resp[ 0 ] = resp[ 0 ].sub(/.*!/, "---!").sub( /\n\n/, "\n")
30
+ resp.join.must.eq "---!ruby/object:B {}\n"
31
+
32
+ _, _, resp = Cuba.call({"HTTP_ACCEPT" => "application/x-yaml"})
33
+ resp[ 0 ] = resp[ 0 ].sub(/.*!/, "---!").sub( /\n\n/, "\n")
34
+ resp.join.must.eq "---!ruby/object:B {}\n"
35
+
36
+ _, _, resp = Cuba.call({"HTTP_ACCEPT" => "text/yaml"})
37
+ resp[ 0 ] = resp[ 0 ].sub(/.*!/, "---!").sub( /\n\n/, "\n")
38
+ resp.join.must.eq "---!ruby/object:B {}\n"
39
+ end
40
+
41
+ it 'gives not found for not configured xml' do
42
+ status, _, _ = Cuba.call({"SCRIPT_NAME" => "/bla.xml"})
43
+ status.must.eq 404
44
+
45
+ status, _, _ = Cuba.call({"HTTP_ACCEPT" => "application/xml"})
46
+ status.must.eq 404
47
+ end
48
+
49
+ it 'gives preference to script extension' do
50
+ _, _, resp = Cuba.call({"SCRIPT_NAME" => "/bla.yaml", "HTTP_ACCEPT" => "application/xml"})
51
+ resp[ 0 ] = resp[ 0 ].sub(/.*!/, "---!").sub( /\n\n/, "\n")
52
+ resp.join.must.eq "---!ruby/object:B {}\n"
53
+
54
+ status, _, _ = Cuba.call({"SCRIPT_NAME" => "/bla.xml", "HTTP_ACCEPT" => "application/x-yaml"})
55
+ status.must.eq 404
56
+ end
57
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'cuba_api/config'
3
+ require 'cuba_api/write_aspect'
4
+
5
+ module Plugin
6
+ def one( obj, opts )
7
+ obj + "-one"
8
+ end
9
+ def two( obj, opts )
10
+ obj + "-two"
11
+ end
12
+ def three( obj, opts )
13
+ obj + "-three"
14
+ end
15
+ end
16
+
17
+ describe CubaApi::WriteAspect do
18
+
19
+ before do
20
+ Cuba.reset!
21
+ Cuba[ :aspects ] = []
22
+ Cuba.plugin CubaApi::Config
23
+ Cuba.plugin CubaApi::WriteAspect
24
+ Cuba.plugin Plugin
25
+ Cuba.append_aspect :one
26
+ Cuba.prepend_aspect :two
27
+ Cuba.append_aspect :three
28
+ Cuba.define do
29
+ on true do
30
+ write 'start'
31
+ end
32
+ end
33
+ end
34
+
35
+ after { Cuba.config.clear }
36
+
37
+ it 'should execute aspects in the right order' do
38
+ _, _, resp = Cuba.call({})
39
+
40
+ resp.join.must.eq "start-two-one-three"
41
+ end
42
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require 'cuba_api/config'
3
+
4
+ describe CubaApi::Config do
5
+
6
+ before do
7
+ Cuba.reset!
8
+ Cuba.plugin CubaApi::Config
9
+ Cuba[ :main ] = Cuba.method( :define )
10
+ Cuba[ :name ] = :root
11
+ class Other < Cuba; end
12
+ end
13
+
14
+ after { Cuba.config.clear }
15
+
16
+ it 'should overwrite super-cuba' do
17
+ Other[ :main ] = :other
18
+
19
+ Cuba[ :main ].class.must.eq Method
20
+ Other[ :main ].must.eq :other
21
+ end
22
+
23
+ it 'should inherit super-cuba on new attributes' do
24
+ Cuba[ :more ] = :more
25
+
26
+ Cuba[ :more ].must.eq :more
27
+ Other[ :more ].must.eq :more
28
+ end
29
+
30
+ it 'should see config from super-cuba' do
31
+ Cuba[ :name ].must.eq :root
32
+ Other[ :name ].must.eq :root
33
+ end
34
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'cuba_api/current_user'
3
+
4
+ class SessionManager
5
+ def to_session( user )
6
+ @u ||= user
7
+ end
8
+ def from_session( data )
9
+ u = @u.dup unless @u.nil?
10
+ def u.login; self;end
11
+ u
12
+ end
13
+ end
14
+
15
+ describe CubaApi::CurrentUser do
16
+
17
+ before do
18
+ Cuba.reset!
19
+ Cuba.plugin CubaApi::CurrentUser
20
+ Cuba.use Rack::Session::Cookie
21
+ Cuba[ :sessions ] = SessionManager.new
22
+ Cuba.define do
23
+ on authenticated? do
24
+ res.write current_user
25
+ end
26
+ on default do
27
+ name = current_user_name
28
+ current_user "user1"
29
+ res.write "logged in - #{name}"
30
+ end
31
+ end
32
+ end
33
+
34
+ it 'should authenticate' do
35
+ _, _, resp = Cuba.call({})
36
+
37
+ resp.join.must.eq "logged in - ???"
38
+
39
+ _, _, resp = Cuba.call({})
40
+
41
+ resp.join.must.eq "user1"
42
+ end
43
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'cuba_api/write_aspect'
3
+ require 'cuba_api/serializer'
4
+ require 'yaml'
5
+ require 'ixtlan/babel/serializer'
6
+
7
+ class A
8
+ def attributes
9
+ { :name => 'me and the corner' }
10
+ end
11
+ end
12
+ class ASerializer < Ixtlan::Babel::Serializer
13
+ end
14
+ module ToYaml
15
+ def to_yaml( obj, opts )
16
+ obj.to_yaml
17
+ end
18
+ end
19
+
20
+ describe CubaApi::Serializer do
21
+
22
+ before do
23
+ Cuba.reset!
24
+ Cuba[ :aspects ] = []
25
+ Cuba.plugin CubaApi::WriteAspect
26
+ Cuba.plugin CubaApi::Serializer
27
+ Cuba.plugin ToYaml
28
+ Cuba.append_aspect :to_yaml
29
+ Cuba.define do
30
+ on default do
31
+ write A.new
32
+ end
33
+ end
34
+ end
35
+
36
+ it 'should write out yaml' do
37
+ _, _, resp = Cuba.call({})
38
+
39
+ resp[ 0 ] = resp[ 0 ].sub(/.*\n/, "---\n")
40
+ resp.must_equal ["---\nname: me and the corner\n"]
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cuba-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christian Meier
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cuba
16
+ requirement: &18852600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *18852600
25
+ - !ruby/object:Gem::Dependency
26
+ name: ixtlan-babel
27
+ requirement: &18850840 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.2.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *18850840
36
+ - !ruby/object:Gem::Dependency
37
+ name: copyright-header
38
+ requirement: &18849320 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.7
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *18849320
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: &18845760 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 4.3.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *18845760
58
+ - !ruby/object:Gem::Dependency
59
+ name: mustard
60
+ requirement: &18844760 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '0.1'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *18844760
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: &18843280 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 10.0.3
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *18843280
80
+ - !ruby/object:Gem::Dependency
81
+ name: backports
82
+ requirement: &18792320 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: 2.6.3
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *18792320
91
+ description: add content negogiation, serialization of objects (their attributes map),
92
+ and some helpers for authentication + authorization to the cuba framework
93
+ email:
94
+ - m.kristian@web.de
95
+ executables: []
96
+ extensions: []
97
+ extra_rdoc_files: []
98
+ files:
99
+ - MIT-LICENSE
100
+ - README.md
101
+ - lib/cuba_api/write_aspects.rb~
102
+ - lib/cuba_api/guard.rb
103
+ - lib/cuba_api/serializer.rb~
104
+ - lib/cuba_api/accept_content.rb~
105
+ - lib/cuba_api/current_user.rb
106
+ - lib/cuba_api/write_aspect.rb
107
+ - lib/cuba_api/write_aspect.rb~
108
+ - lib/cuba_api/current_user.rb~
109
+ - lib/cuba_api/config.rb
110
+ - lib/cuba_api/guard.rb~
111
+ - lib/cuba_api/accept_content.rb
112
+ - lib/cuba_api/config.rb~
113
+ - lib/cuba_api/serializer.rb
114
+ - lib/cuba_api.rb
115
+ - lib/cuba_api.rb~
116
+ - spec/serializer_spec.rb
117
+ - spec/aspects_spec.rb
118
+ - spec/config_spec.rb
119
+ - spec/accept_spec.rb
120
+ - spec/current_user_spec.rb
121
+ homepage: http://github.com/mkristian/cuba-api
122
+ licenses:
123
+ - MIT
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 1.8.11
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: set of plugins for using cuba as API server
146
+ test_files:
147
+ - spec/serializer_spec.rb
148
+ - spec/aspects_spec.rb
149
+ - spec/config_spec.rb
150
+ - spec/accept_spec.rb
151
+ - spec/current_user_spec.rb
152
+ has_rdoc: