cuba-sugar 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,6 +26,7 @@ require "cuba/sugar"
26
26
 
27
27
  Cuba.use Rack::Session::Cookie
28
28
 
29
+ Cuba.plugin Cuba::Sugar
29
30
  Cuba.define do
30
31
  on post do
31
32
  on "users" do
@@ -46,6 +47,7 @@ require "cuba/sugar"
46
47
 
47
48
  Cuba.use Rack::Session::Cookie
48
49
 
50
+ Cuba.plugin Cuba::Sugar
49
51
  Cuba.define do
50
52
  on get do
51
53
  on "weather" do
@@ -77,6 +79,7 @@ require "cuba/sugar"
77
79
 
78
80
  Cuba.use Rack::Csrf
79
81
 
82
+ Cuba.plugin Cuba::Sugar
80
83
  Cuba.define do
81
84
  # Automatic csrf validation
82
85
  on post
@@ -90,6 +93,7 @@ end
90
93
  require "cuba"
91
94
  require "cuba/sugar"
92
95
 
96
+ Cuba.plugin Cuba::Sugar
93
97
  Cuba.define do
94
98
  helpers do
95
99
  def now
@@ -109,6 +113,7 @@ end
109
113
  require "cuba"
110
114
  require "cuba/sugar"
111
115
 
116
+ Cuba.plugin Cuba::Sugar
112
117
  Cuba.define do
113
118
  on subdomain("wsdl") do
114
119
  run WSDL
data/cuba-sugar.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cuba-sugar"
3
- s.version = "0.2.1"
3
+ s.version = "0.2.2"
4
4
  s.summary = "Give Cuba some Sugar!"
5
- s.description = "Bundled contrib utils to use with cuba"
5
+ s.description = "Useful stuff to use with cuba-contrib"
6
6
  s.authors = ["elcuervo"]
7
7
  s.email = ["yo@brunoaguirre.com"]
8
8
  s.homepage = "http://github.com/elcuervo/cuba-sugar"
@@ -10,6 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.test_files = `git ls-files test`.split("\n")
11
11
 
12
12
  s.add_dependency("cuba")
13
+ s.add_dependency("cuba-contrib")
13
14
  s.add_dependency("json")
14
15
  s.add_dependency("rack_csrf")
15
16
 
data/lib/cuba/sugar.rb CHANGED
@@ -1,11 +1,36 @@
1
- require 'json'
1
+ require "cuba/contrib"
2
2
  require "rack/csrf"
3
3
 
4
- class Cuba
4
+ module Cuba::Sugar
5
5
 
6
+ # Sugar to access root of a page
7
+ #
8
+ # @example
9
+ # on post, root do
10
+ # as do
11
+ # "Welcome!"
12
+ # end
13
+ # end
6
14
  def root; "" end
15
+
16
+ # Sugar to access an option verb
17
+ #
18
+ # @example
19
+ # on options do
20
+ # as_json do
21
+ # API.interaction_methods
22
+ # end
23
+ # end
7
24
  def options; req.options? end
8
25
 
26
+ # Sugar to easily access params
27
+ #
28
+ # @example
29
+ # on get, root do
30
+ # as do
31
+ # "This id: #{params['id']}"
32
+ # end
33
+ # end
9
34
  def params; req.params end
10
35
 
11
36
  # Sugar to do some common response tasks
@@ -22,36 +47,86 @@ class Cuba
22
47
  res.write yield if block_given?
23
48
  end
24
49
 
50
+ # Sugar to do some common response tasks as_json
51
+ #
52
+ # @example
53
+ # on post, "users" do
54
+ # as_json 201 do
55
+ # "User successfully created!"
56
+ # end
57
+ # end
25
58
  def as_json(http_code = 200, extra_headers = {})
59
+ require 'json'
26
60
  extra_headers["Content-Type"] ||= "application/json"
27
61
  as(http_code, extra_headers) do
28
62
  (yield).to_json if block_given?
29
63
  end
30
64
  end
31
65
 
66
+ # Sugar to do match a given subdomain.
67
+ # eg. blog.example.com
68
+ #
69
+ # @example
70
+ # on subdomain("blog") do
71
+ # as do
72
+ # run Blog
73
+ # end
74
+ # end
32
75
  def subdomain(sub)
33
76
  sub == req.host.split(".").first
34
77
  end
35
78
 
79
+ # Sugar to access session info
80
+ #
81
+ # @example
82
+ # on root do
83
+ # as do
84
+ # "User name: #{session['name']}"
85
+ # end
86
+ # end
36
87
  def session
37
88
  env["rack.session"]
38
89
  end
39
90
 
91
+ # Sugar to redirect a request
92
+ #
93
+ # @example
94
+ # on "old" do
95
+ # redirect "/new"
96
+ # end
40
97
  def redirect(*args)
41
98
  res.redirect(*args)
42
- @matched = true
43
99
  end
44
100
 
45
- # From sinatra/base
101
+ # Sugar to include helpers and access them from
102
+ # views
103
+ #
104
+ # @example
105
+ # helpers do
106
+ # def now
107
+ # Time.now
108
+ # end
109
+ # end
46
110
  def helpers(*extensions, &block)
47
111
  instance_eval(&block) if block_given?
48
112
  extend(*extensions) if extensions.any?
49
113
  end
50
114
 
115
+ # Sugar to include a csrf tag
116
+ #
117
+ # @example
118
+ # <form action="/new">
119
+ # <%= csrf_tag %>
120
+ # <input type="text" />
121
+ # </form>
51
122
  def csrf_tag
52
123
  Rack::Csrf.tag(env)
53
124
  end
54
125
 
126
+ # Sugar to access the csrf token
127
+ #
128
+ # @example
129
+ # <%= csrf_token %>
55
130
  def csrf_token
56
131
  Rack::Csrf.token(env)
57
132
  end
data/test/as.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require File.expand_path("helper", File.dirname(__FILE__))
2
2
 
3
3
  test "set status and headers through helper" do
4
+ Cuba.plugin Cuba::Sugar
4
5
  Cuba.define do
5
6
  on "users" do
6
7
  as 201, {"Content-Location" => "http://somewhere.com/users/705"} do
data/test/as_json.rb CHANGED
@@ -3,6 +3,8 @@ require 'json'
3
3
 
4
4
  test "set a block to return json" do
5
5
  rum_and_coke = { "rum" => "hot", "coke" => "sweet" }
6
+
7
+ Cuba.plugin Cuba::Sugar
6
8
  Cuba.define do
7
9
  on "drinks" do
8
10
  as_json 201, {"Content-Location" => "http://somewhere.com/drinks/42"} do
data/test/csrf.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require File.expand_path("helper", File.dirname(__FILE__))
2
2
 
3
3
  test "set status and headers through helper" do
4
+ Cuba.plugin Cuba::Sugar
4
5
  Cuba.define do
5
6
  on "users" do
6
7
  as do
data/test/helpers.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require File.expand_path("helper", File.dirname(__FILE__))
2
2
 
3
3
  test "set status and headers through helpers" do
4
+ Cuba.plugin Cuba::Sugar
4
5
  Cuba.define do
5
6
  helpers do
6
7
  def upperize(string)
data/test/redirect.rb ADDED
@@ -0,0 +1,17 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "redirect helper" do
4
+ Cuba.plugin Cuba::Sugar
5
+ Cuba.define do
6
+ on "old" do
7
+ redirect "/new"
8
+ end
9
+ end
10
+
11
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/old" }
12
+
13
+ _, _, resp = Cuba.call(env)
14
+
15
+ assert_equal 302, resp.status
16
+ assert_equal "/new", resp.headers["Location"]
17
+ end
data/test/subdomain.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require File.expand_path("helper", File.dirname(__FILE__))
2
2
 
3
3
  test "eval only in a given subdomain" do
4
+ Cuba.plugin Cuba::Sugar
4
5
  Cuba.define do
5
6
  on subdomain("api"), root do
6
7
  as do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuba-sugar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-14 00:00:00.000000000Z
12
+ date: 2011-12-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cuba
16
- requirement: &70347365836240 !ruby/object:Gem::Requirement
16
+ requirement: &70204609640940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70347365836240
24
+ version_requirements: *70204609640940
25
+ - !ruby/object:Gem::Dependency
26
+ name: cuba-contrib
27
+ requirement: &70204609640500 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70204609640500
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: json
27
- requirement: &70347365835400 !ruby/object:Gem::Requirement
38
+ requirement: &70204605330560 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: '0'
33
44
  type: :runtime
34
45
  prerelease: false
35
- version_requirements: *70347365835400
46
+ version_requirements: *70204605330560
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rack_csrf
38
- requirement: &70347365834740 !ruby/object:Gem::Requirement
49
+ requirement: &70204605330140 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: '0'
44
55
  type: :runtime
45
56
  prerelease: false
46
- version_requirements: *70347365834740
57
+ version_requirements: *70204605330140
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: cutest
49
- requirement: &70347365834080 !ruby/object:Gem::Requirement
60
+ requirement: &70204605329720 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,8 +65,8 @@ dependencies:
54
65
  version: '0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *70347365834080
58
- description: Bundled contrib utils to use with cuba
68
+ version_requirements: *70204605329720
69
+ description: Useful stuff to use with cuba-contrib
59
70
  email:
60
71
  - yo@brunoaguirre.com
61
72
  executables: []
@@ -75,6 +86,7 @@ files:
75
86
  - test/fixtures/csrf.erb
76
87
  - test/helper.rb
77
88
  - test/helpers.rb
89
+ - test/redirect.rb
78
90
  - test/subdomain.rb
79
91
  homepage: http://github.com/elcuervo/cuba-sugar
80
92
  licenses: []
@@ -107,4 +119,5 @@ test_files:
107
119
  - test/fixtures/csrf.erb
108
120
  - test/helper.rb
109
121
  - test/helpers.rb
122
+ - test/redirect.rb
110
123
  - test/subdomain.rb