cuba-sugar 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ /pkg
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+ gem "rake"
data/README.md CHANGED
@@ -103,6 +103,33 @@ Cuba.define do
103
103
  end
104
104
  ```
105
105
 
106
+ ### subdomain
107
+
108
+ ```ruby
109
+ require "cuba"
110
+ require "cuba/sugar"
111
+
112
+ Cuba.define do
113
+ on subdomain("wsdl") do
114
+ run WSDL
115
+ end
116
+
117
+ on subdomain("api") do
118
+ on root do
119
+ as do
120
+ "Welcome to API"
121
+ end
122
+ end
123
+
124
+ on "users" do
125
+ as_json do
126
+ Users.all.to_json
127
+ end
128
+ end
129
+ end
130
+ end
131
+ ```
132
+
106
133
  Contributors
107
134
  ------------
108
135
 
@@ -111,4 +138,6 @@ Contributors
111
138
  Installation
112
139
  ------------
113
140
 
114
- $ gem install cuba-sugar
141
+ ```bash
142
+ $ gem install cuba-sugar
143
+ ```
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ task :test do
2
+ require "cutest"
3
+
4
+ Cutest.run(Dir["test/*.rb"])
5
+ end
6
+
7
+ task :default => :test
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "cuba-sugar"
3
+ s.version = "0.2.1"
4
+ s.summary = "Give Cuba some Sugar!"
5
+ s.description = "Bundled contrib utils to use with cuba"
6
+ s.authors = ["elcuervo"]
7
+ s.email = ["yo@brunoaguirre.com"]
8
+ s.homepage = "http://github.com/elcuervo/cuba-sugar"
9
+ s.files = `git ls-files`.split("\n")
10
+ s.test_files = `git ls-files test`.split("\n")
11
+
12
+ s.add_dependency("cuba")
13
+ s.add_dependency("json")
14
+ s.add_dependency("rack_csrf")
15
+
16
+ s.add_development_dependency("cutest")
17
+ end
data/lib/cuba/sugar.rb CHANGED
@@ -2,6 +2,12 @@ require 'json'
2
2
  require "rack/csrf"
3
3
 
4
4
  class Cuba
5
+
6
+ def root; "" end
7
+ def options; req.options? end
8
+
9
+ def params; req.params end
10
+
5
11
  # Sugar to do some common response tasks
6
12
  #
7
13
  # @example
@@ -23,10 +29,23 @@ class Cuba
23
29
  end
24
30
  end
25
31
 
32
+ def subdomain(sub)
33
+ sub == req.host.split(".").first
34
+ end
35
+
36
+ def session
37
+ env["rack.session"]
38
+ end
39
+
40
+ def redirect(*args)
41
+ res.redirect(*args)
42
+ @matched = true
43
+ end
44
+
26
45
  # From sinatra/base
27
46
  def helpers(*extensions, &block)
28
47
  instance_eval(&block) if block_given?
29
- include(*extensions) if extensions.any?
48
+ extend(*extensions) if extensions.any?
30
49
  end
31
50
 
32
51
  def csrf_tag
data/test/as.rb ADDED
@@ -0,0 +1,19 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "set status and headers through helper" do
4
+ Cuba.define do
5
+ on "users" do
6
+ as 201, {"Content-Location" => "http://somewhere.com/users/705"} do
7
+ "User Created"
8
+ end
9
+ end
10
+ end
11
+
12
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/users" }
13
+
14
+ _, _, resp = Cuba.call(env)
15
+
16
+ assert_equal 201, resp.status
17
+ assert_equal ["User Created"], resp.body
18
+ assert_equal "http://somewhere.com/users/705", resp.headers["Content-Location"]
19
+ end
data/test/as_json.rb ADDED
@@ -0,0 +1,22 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+ require 'json'
3
+
4
+ test "set a block to return json" do
5
+ rum_and_coke = { "rum" => "hot", "coke" => "sweet" }
6
+ Cuba.define do
7
+ on "drinks" do
8
+ as_json 201, {"Content-Location" => "http://somewhere.com/drinks/42"} do
9
+ rum_and_coke
10
+ end
11
+ end
12
+ end
13
+
14
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/drinks" }
15
+
16
+ _, _, resp = Cuba.call(env)
17
+
18
+ assert_equal 201, resp.status
19
+ assert_equal "application/json", resp.headers["Content-Type"]
20
+ assert_equal [rum_and_coke.to_json], resp.body
21
+ assert_equal "http://somewhere.com/drinks/42", resp.headers["Content-Location"]
22
+ end
data/test/csrf.rb ADDED
@@ -0,0 +1,19 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "set status and headers through helper" do
4
+ Cuba.define do
5
+ on "users" do
6
+ as do
7
+ render "test/fixtures/csrf.erb"
8
+ end
9
+ end
10
+ end
11
+
12
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/users", 'rack.session' => {} }
13
+
14
+ _, _, resp = Cuba.call(env)
15
+
16
+ assert env.fetch("rack.session").has_key?("csrf.token")
17
+ assert /_csrf/ =~ resp.body.first
18
+ assert_equal 44, env.fetch("rack.session").fetch("csrf.token").size
19
+ end
@@ -0,0 +1,5 @@
1
+ <form method="post" action="/secure">
2
+ <%= csrf_tag %>
3
+ <input type="text" name="message"/>
4
+ <input type="submit" value="Submit!"/>
5
+ </form>
data/test/helper.rb ADDED
@@ -0,0 +1,6 @@
1
+ $:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
2
+ require "cuba"
3
+ require "cuba/sugar"
4
+ require "cutest"
5
+
6
+ prepare { Cuba.reset! }
data/test/helpers.rb ADDED
@@ -0,0 +1,23 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "set status and headers through helpers" do
4
+ Cuba.define do
5
+ helpers do
6
+ def upperize(string)
7
+ string.upcase
8
+ end
9
+ end
10
+
11
+ on "users" do
12
+ as do
13
+ "I will shout: #{upperize("hello")}"
14
+ end
15
+ end
16
+ end
17
+
18
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/users" }
19
+
20
+ _, _, resp = Cuba.call(env)
21
+
22
+ assert_equal ["I will shout: HELLO"], resp.body
23
+ end
data/test/subdomain.rb ADDED
@@ -0,0 +1,26 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "eval only in a given subdomain" do
4
+ Cuba.define do
5
+ on subdomain("api"), root do
6
+ as do
7
+ "Subdomain"
8
+ end
9
+ end
10
+
11
+ on root do
12
+ as do
13
+ "Main"
14
+ end
15
+ end
16
+ end
17
+
18
+ subdomain_env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/", "HTTP_HOST" => "api.example.com" }
19
+ standard_env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/", "HTTP_HOST" => "example.com" }
20
+
21
+ _, _, sub_resp = Cuba.call(subdomain_env)
22
+ _, _, resp = Cuba.call(standard_env)
23
+
24
+ assert_equal ["Subdomain"], sub_resp.body
25
+ assert_equal ["Main"], resp.body
26
+ end
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.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-20 00:00:00.000000000 -03:00
13
- default_executable:
12
+ date: 2011-12-14 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: cuba
17
- requirement: &70196371413660 !ruby/object:Gem::Requirement
16
+ requirement: &70347365836240 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: '0'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *70196371413660
24
+ version_requirements: *70347365836240
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: json
28
- requirement: &70196371413100 !ruby/object:Gem::Requirement
27
+ requirement: &70347365835400 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ! '>='
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: '0'
34
33
  type: :runtime
35
34
  prerelease: false
36
- version_requirements: *70196371413100
35
+ version_requirements: *70347365835400
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: rack_csrf
39
- requirement: &70196371412560 !ruby/object:Gem::Requirement
38
+ requirement: &70347365834740 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ! '>='
@@ -44,10 +43,10 @@ dependencies:
44
43
  version: '0'
45
44
  type: :runtime
46
45
  prerelease: false
47
- version_requirements: *70196371412560
46
+ version_requirements: *70347365834740
48
47
  - !ruby/object:Gem::Dependency
49
48
  name: cutest
50
- requirement: &70196371412020 !ruby/object:Gem::Requirement
49
+ requirement: &70347365834080 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
53
52
  - - ! '>='
@@ -55,7 +54,7 @@ dependencies:
55
54
  version: '0'
56
55
  type: :development
57
56
  prerelease: false
58
- version_requirements: *70196371412020
57
+ version_requirements: *70347365834080
59
58
  description: Bundled contrib utils to use with cuba
60
59
  email:
61
60
  - yo@brunoaguirre.com
@@ -63,10 +62,20 @@ executables: []
63
62
  extensions: []
64
63
  extra_rdoc_files: []
65
64
  files:
65
+ - .gitignore
66
+ - Gemfile
66
67
  - LICENSE
67
68
  - README.md
69
+ - Rakefile
70
+ - cuba-sugar.gemspec
68
71
  - lib/cuba/sugar.rb
69
- has_rdoc: true
72
+ - test/as.rb
73
+ - test/as_json.rb
74
+ - test/csrf.rb
75
+ - test/fixtures/csrf.erb
76
+ - test/helper.rb
77
+ - test/helpers.rb
78
+ - test/subdomain.rb
70
79
  homepage: http://github.com/elcuervo/cuba-sugar
71
80
  licenses: []
72
81
  post_install_message:
@@ -87,8 +96,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
96
  version: '0'
88
97
  requirements: []
89
98
  rubyforge_project:
90
- rubygems_version: 1.6.2
99
+ rubygems_version: 1.8.10
91
100
  signing_key:
92
101
  specification_version: 3
93
102
  summary: Give Cuba some Sugar!
94
- test_files: []
103
+ test_files:
104
+ - test/as.rb
105
+ - test/as_json.rb
106
+ - test/csrf.rb
107
+ - test/fixtures/csrf.erb
108
+ - test/helper.rb
109
+ - test/helpers.rb
110
+ - test/subdomain.rb