cuba-sugar-cj 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9f506ff8fc03e0d2b3b062560a8ccd3c2c6bc2be
4
+ data.tar.gz: 44522f5a29909b4c2f8dbbeecba8f00d40f4101c
5
+ SHA512:
6
+ metadata.gz: ffd3e45ac89400ae2669b0ebee717527cd66a73bc9e6b95f60e0c3f300de1acd39e2c517b41592e78507793b25b741d7f5dc4ab7a7bc2f279c67591b6f0223e2
7
+ data.tar.gz: a1f4d31a9d67fe11f3c705f5f43e2cf26ee4e1f2b8a3e7cbeee92a1130e9313f4a58573302135776534cfd4d0e7b83e49a5a58d913d4d4f42d1bc539d8cbe930
@@ -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"
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cuba-sugar (0.3.0)
5
+ cuba (~> 3.1.0)
6
+ json (~> 1.7.3)
7
+ rack_csrf (~> 2.4.0)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ cuba (3.1.0)
13
+ rack
14
+ cutest (1.1.3)
15
+ json (1.7.3)
16
+ rack (1.4.1)
17
+ rack_csrf (2.4.0)
18
+ rack (>= 0.9)
19
+ rake (0.9.2.2)
20
+ tilt (1.3.3)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ cuba-sugar!
27
+ cutest (~> 1.1.3)
28
+ rake
29
+ tilt (~> 1.3.3)
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ /*
2
+ * ----------------------------------------------------------------------------
3
+ * "THE BEER-WARE LICENSE" (Revision 42):
4
+ * elcuervo wrote this file. As long as you retain this notice you
5
+ * can do whatever you want with this stuff. If we meet some day, and you think
6
+ * this stuff is worth it, you can buy me a beer in return Bruno Aguirre
7
+ * ----------------------------------------------------------------------------
8
+ */
@@ -0,0 +1,176 @@
1
+ Cuba/Sugar
2
+ ==========
3
+
4
+ ![Build Status](https://secure.travis-ci.org/elcuervo/cuba-sugar.png)
5
+
6
+ _n_. bundled sugar for [cuba](https://github.com/soveran/cuba)
7
+
8
+ !["See this fucking sugar road? I just fucking made it"](http://maynardswitzer.com/library/Cuba/2002CubaSugarCaneCutterInField.A.jpg)
9
+
10
+ Description
11
+ -----------
12
+
13
+ It's like contrib stuff but with a cooler name.
14
+ Provides helpers and utilities to use with cuba.
15
+
16
+ Usage
17
+ -----
18
+
19
+ Like any other cuba app, but provides:
20
+
21
+ You have two choices:
22
+
23
+ * `require` only the tool you want eg: `require 'cuba/sugar/content_for'`
24
+ * `require` all the tools eg: `require 'cuba/sugar'`
25
+
26
+ ### content_for
27
+
28
+ ```ruby
29
+ require "cuba"
30
+ require "cuba/sugar/content_for"
31
+
32
+ Cuba.plugin Cuba::Sugar::ContentFor
33
+ ```
34
+
35
+ In your views to define where the content block will be rendered
36
+
37
+ ```erb
38
+ <% yield_for :menu %>
39
+
40
+ # And when you want to define a content
41
+ <% content_for :menu %>
42
+ <ul id="menu">
43
+ <li class="active">Home</li>
44
+ <li>Users</li>
45
+ </ul>
46
+ <% end %>
47
+ ```
48
+
49
+ ### as
50
+
51
+ ```ruby
52
+ require "cuba"
53
+ require "cuba/sugar/as"
54
+
55
+ Cuba.use Rack::Session::Cookie
56
+
57
+ Cuba.plugin Cuba::Sugar::As
58
+ Cuba.define do
59
+ on post do
60
+ on "users" do
61
+ # create user
62
+ as 201, {"Content-Location" => "http://somewhere.com/users/705"} do
63
+ "user #{user.id} created"
64
+ end
65
+ end
66
+ end
67
+ end
68
+ ```
69
+
70
+ ### as_json
71
+
72
+ ```ruby
73
+ require "cuba"
74
+ require "cuba/sugar/as"
75
+
76
+ Cuba.use Rack::Session::Cookie
77
+
78
+ Cuba.plugin Cuba::Sugar::As
79
+ Cuba.define do
80
+ on get do
81
+ on "weather" do
82
+ as_json do
83
+ {
84
+ city: 'La Habana',
85
+ temperature: '19 °C',
86
+ pressure: '1014 hPa',
87
+ }
88
+ end
89
+ end
90
+ end
91
+ end
92
+ ```
93
+
94
+ ### csrf_tag && csrf_token
95
+
96
+ In the forms:
97
+
98
+ ```ruby
99
+ <%= csrf_tag %>
100
+ ```
101
+
102
+ In the code:
103
+
104
+ ```ruby
105
+ require "cuba"
106
+ require "cuba/sugar/csrf"
107
+
108
+ Cuba.use Rack::Csrf
109
+
110
+ Cuba.plugin Cuba::Sugar::Csrf
111
+ Cuba.define do
112
+ # Automatic csrf validation
113
+ on post
114
+ end
115
+ end
116
+ ```
117
+
118
+ ### helpers
119
+
120
+ ```ruby
121
+ require "cuba"
122
+ require "cuba/sugar"
123
+
124
+ Cuba.plugin Cuba::Sugar
125
+ Cuba.define do
126
+ helpers do
127
+ def now
128
+ Time.now
129
+ end
130
+ end
131
+
132
+ on "time" do
133
+ as { "The current time is: #{now}" }
134
+ end
135
+ end
136
+ ```
137
+
138
+ ### subdomain
139
+
140
+ ```ruby
141
+ require "cuba"
142
+ require "cuba/sugar/routes"
143
+
144
+ Cuba.plugin Cuba::Sugar::Routes
145
+ Cuba.define do
146
+ on subdomain("wsdl") do
147
+ run WSDL
148
+ end
149
+
150
+ on subdomain("api") do
151
+ on root do
152
+ as do
153
+ "Welcome to API"
154
+ end
155
+ end
156
+
157
+ on "users" do
158
+ as_json do
159
+ Users.all.to_json
160
+ end
161
+ end
162
+ end
163
+ end
164
+ ```
165
+
166
+ Contributors
167
+ ------------
168
+
169
+ * [bemurphy](https://github.com/bemurphy) (Brendon Murphy)
170
+
171
+ Installation
172
+ ------------
173
+
174
+ ```bash
175
+ $ gem install cuba-sugar
176
+ ```
@@ -0,0 +1,8 @@
1
+ $: << "test"
2
+
3
+ task :test do
4
+ require "cutest"
5
+ Cutest.run(Dir["test/*.rb"])
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "cuba-sugar-cj"
3
+ s.version = "0.3.1"
4
+ s.summary = "Give Cuba some Sugar!"
5
+ s.description = "Useful stuff 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", ">= 3.1.0")
13
+ s.add_dependency("rack_csrf", ">= 2.4.0")
14
+
15
+ s.add_development_dependency("cutest", ">= 1.1.3")
16
+ s.add_development_dependency("tilt", ">= 1.3.3")
17
+ end
@@ -0,0 +1,28 @@
1
+ require 'cuba/sugar/as'
2
+ require 'cuba/sugar/csrf'
3
+ require 'cuba/sugar/routes'
4
+ require 'cuba/sugar/content_for'
5
+
6
+ module Cuba::Sugar
7
+ include As
8
+ include Csrf
9
+ include Routes
10
+ include ContentFor
11
+
12
+ # Public: Sugar to include helpers
13
+ #
14
+ # *extensions - Modules to be evaluated
15
+ # &block - Methods to be included
16
+ #
17
+ # Examples:
18
+ #
19
+ # helpers do
20
+ # def now
21
+ # Time.now
22
+ # end
23
+ # end
24
+ def helpers(*extensions, &block)
25
+ instance_eval(&block) if block
26
+ extend(*extensions) if extensions.any?
27
+ end
28
+ end
@@ -0,0 +1,38 @@
1
+ module Cuba::Sugar
2
+ module As
3
+ # Public: Sugar to do some common response tasks
4
+ #
5
+ # http_code - Response status code (default: 200)
6
+ # extra_headers - Extra headers hash (default: {})
7
+ #
8
+ # Examples:
9
+ #
10
+ # on post, "users" do
11
+ # as 201 do
12
+ # "User successfully created!"
13
+ # end
14
+ # end
15
+ def as(http_code = 200, extra_headers = {}, &block)
16
+ res.status = http_code
17
+ res.headers.merge! extra_headers
18
+ yield if block
19
+ end
20
+
21
+ # Public: Sugar to do some common response tasks as_json
22
+ #
23
+ # http_code - Response status code (default: 200)
24
+ # extra_headers - Extra headers hash (default: {})
25
+ # Examples:
26
+ #
27
+ # on post, "users" do
28
+ # as_json 201 do
29
+ # "User successfully created!"
30
+ # end
31
+ # end
32
+ def as_json(http_code = 200, extra_headers = {}, &block)
33
+ require 'json'
34
+ extra_headers["Content-Type"] ||= "application/json"
35
+ as(http_code, extra_headers) { yield.to_json if block }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ module Cuba::Sugar
2
+ module ContentFor
3
+ # Public: yields a content in a view
4
+ #
5
+ # symbol - The symbol to be searched
6
+ #
7
+ # Examples:
8
+ #
9
+ # <% yield_for :menu %>
10
+ def yield_for(symbol)
11
+ content_blocks[symbol].map(&:call)
12
+ end
13
+
14
+ # Public: Sets a content for a given symbol
15
+ #
16
+ # symbol - The symbol key
17
+ # &block - Block to be called
18
+ #
19
+ # Examples:
20
+ #
21
+ # <% content_for :menu do %>
22
+ # Home | Admin
23
+ # <% end %>
24
+ def content_for(symbol, &block)
25
+ content_blocks[symbol] << block
26
+ end
27
+
28
+ private
29
+
30
+ # Private: Hash of arrays to store content blocks
31
+ def content_blocks
32
+ @content_blocks ||= Hash.new { |h, k| h[k] = [] }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ require "rack/csrf"
2
+
3
+ module Cuba::Sugar
4
+ module Csrf
5
+ # Public: Sugar to include a csrf tag
6
+ #
7
+ # Examples:
8
+ #
9
+ # <form action="/new">
10
+ # <%= csrf_tag %>
11
+ # <input type="text" />
12
+ # </form>
13
+ def csrf_tag
14
+ Rack::Csrf.tag(env)
15
+ end
16
+
17
+ # Public: Sugar to access the csrf token
18
+ #
19
+ # Examples:
20
+ #
21
+ # <%= csrf_token %>
22
+ def csrf_token
23
+ Rack::Csrf.token(env)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ module Cuba::Sugar
2
+ module Routes
3
+ # Public: Sugar to access an option verb
4
+ #
5
+ # Examples:
6
+ #
7
+ # on options do
8
+ # as_json do
9
+ # API.interaction_methods
10
+ # end
11
+ # end
12
+ def options
13
+ req.options?
14
+ end
15
+
16
+ # Public: Sugar to do match a given subdomain. eg. blog.example.com
17
+ #
18
+ # subdomain - subdomain to be checked
19
+ #
20
+ # Examples:
21
+ #
22
+ # on subdomain("blog") do
23
+ # as do
24
+ # run Blog
25
+ # end
26
+ # end
27
+ def subdomain(sub)
28
+ sub == req.host.split(".").first
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,43 @@
1
+ require "test_helper"
2
+ require "cuba/sugar/as"
3
+
4
+ test "set status and headers through helper" do
5
+ Cuba.plugin Cuba::Sugar::As
6
+ Cuba.define do
7
+ on "users" do
8
+ as 201, {"Content-Location" => "http://somewhere.com/users/705"} do
9
+ "User Created"
10
+ end
11
+ end
12
+ end
13
+
14
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/users" }
15
+
16
+ code, headers, resp = Cuba.call(env)
17
+
18
+ assert_equal 201, code
19
+ assert_equal ["User Created"], resp
20
+ assert_equal "http://somewhere.com/users/705", headers["Content-Location"]
21
+ end
22
+
23
+ test "set a block to return json" do
24
+ rum_and_coke = { "rum" => "hot", "coke" => "sweet" }
25
+
26
+ Cuba.plugin Cuba::Sugar::As
27
+ Cuba.define do
28
+ on "drinks" do
29
+ as_json 201, {"Content-Location" => "http://somewhere.com/drinks/42"} do
30
+ rum_and_coke
31
+ end
32
+ end
33
+ end
34
+
35
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/drinks" }
36
+
37
+ code, headers, resp = Cuba.call(env)
38
+
39
+ assert_equal 201, code
40
+ assert_equal "application/json", headers["Content-Type"]
41
+ assert_equal [rum_and_coke.to_json], resp
42
+ assert_equal "http://somewhere.com/drinks/42", headers["Content-Location"]
43
+ end
@@ -0,0 +1,25 @@
1
+ require "test_helper"
2
+ require "cuba/render"
3
+ require "cuba/sugar/content_for"
4
+
5
+ scope do
6
+ setup do
7
+ Cuba.plugin Cuba::Render
8
+ Cuba.plugin Cuba::Sugar::ContentFor
9
+
10
+ Cuba.settings[:render][:views] = "./test/views"
11
+ Cuba.settings[:render][:template_engine] = "erb"
12
+
13
+ Cuba.define do
14
+ on root do
15
+ res.write view("home")
16
+ end
17
+ end
18
+ end
19
+
20
+ test "content for the menu" do
21
+ _, _, body = Cuba.call({ "PATH_INFO" => "/", "SCRIPT_NAME" => "/" })
22
+
23
+ assert_equal body, [" alpha\nbeta\ngamma\n\n"]
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require "test_helper"
2
+ require "cuba/render"
3
+ require "cuba/sugar/as"
4
+ require "cuba/sugar/csrf"
5
+
6
+ test "set status and headers through helper" do
7
+ Cuba.plugin Cuba::Sugar::As
8
+ Cuba.plugin Cuba::Sugar::Csrf
9
+ Cuba.plugin Cuba::Render
10
+ Cuba.define do
11
+ on "users" do
12
+ as do
13
+ render "test/fixtures/csrf.erb"
14
+ end
15
+ end
16
+ end
17
+
18
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/users", "rack.session" => {} }
19
+
20
+ code, headers, resp = Cuba.call(env)
21
+
22
+ assert env.fetch("rack.session").has_key?("csrf.token")
23
+ assert /_csrf/ =~ resp.first
24
+ assert_equal 44, env.fetch("rack.session").fetch("csrf.token").size
25
+ 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>
@@ -0,0 +1,25 @@
1
+ require "test_helper"
2
+ require "cuba/sugar"
3
+
4
+ test "set status and headers through helpers" do
5
+ Cuba.plugin Cuba::Sugar
6
+ Cuba.define do
7
+ helpers do
8
+ def upperize(string)
9
+ string.upcase
10
+ end
11
+ end
12
+
13
+ on "users" do
14
+ as do
15
+ "I will shout: #{upperize("hello")}"
16
+ end
17
+ end
18
+ end
19
+
20
+ env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/users" }
21
+
22
+ _, _, resp = Cuba.call(env)
23
+
24
+ assert_equal ["I will shout: HELLO"], resp
25
+ end
@@ -0,0 +1,30 @@
1
+ require "test_helper"
2
+ require "cuba/sugar/as"
3
+ require "cuba/sugar/routes"
4
+
5
+ test "eval only in a given subdomain" do
6
+ Cuba.plugin Cuba::Sugar::As
7
+ Cuba.plugin Cuba::Sugar::Routes
8
+ Cuba.define do
9
+ on subdomain("api"), root do
10
+ as do
11
+ "Subdomain"
12
+ end
13
+ end
14
+
15
+ on root do
16
+ as do
17
+ "Main"
18
+ end
19
+ end
20
+ end
21
+
22
+ subdomain_env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/", "HTTP_HOST" => "api.example.com" }
23
+ standard_env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/", "HTTP_HOST" => "example.com" }
24
+
25
+ _, _, sub_resp = Cuba.call(subdomain_env)
26
+ _, _, resp = Cuba.call(standard_env)
27
+
28
+ assert_equal ["Subdomain"], sub_resp
29
+ assert_equal ["Main"], resp
30
+ end
@@ -0,0 +1,6 @@
1
+ $: << "lib"
2
+
3
+ require "cuba"
4
+ require "cutest"
5
+
6
+ prepare { Cuba.reset! }
@@ -0,0 +1,4 @@
1
+ gamma
2
+ <% content_for :menu do %>
3
+ alpha
4
+ <% end %>
@@ -0,0 +1,4 @@
1
+ <% yield_for :menu %>
2
+ <% yield_for :not %>
3
+ beta
4
+ <%= content %>
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cuba-sugar-cj
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ platform: ruby
6
+ authors:
7
+ - elcuervo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cuba
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack_csrf
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.4.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 2.4.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: cutest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.1.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.1.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: tilt
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.3
69
+ description: Useful stuff to use with cuba
70
+ email:
71
+ - yo@brunoaguirre.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - cuba-sugar.gemspec
83
+ - lib/cuba/sugar.rb
84
+ - lib/cuba/sugar/as.rb
85
+ - lib/cuba/sugar/content_for.rb
86
+ - lib/cuba/sugar/csrf.rb
87
+ - lib/cuba/sugar/routes.rb
88
+ - test/as.rb
89
+ - test/content_for.rb
90
+ - test/csrf.rb
91
+ - test/fixtures/csrf.erb
92
+ - test/helpers.rb
93
+ - test/routes.rb
94
+ - test/test_helper.rb
95
+ - test/views/home.erb
96
+ - test/views/layout.erb
97
+ homepage: http://github.com/elcuervo/cuba-sugar
98
+ licenses: []
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.0.14
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Give Cuba some Sugar!
120
+ test_files:
121
+ - test/as.rb
122
+ - test/content_for.rb
123
+ - test/csrf.rb
124
+ - test/fixtures/csrf.erb
125
+ - test/helpers.rb
126
+ - test/routes.rb
127
+ - test/test_helper.rb
128
+ - test/views/home.erb
129
+ - test/views/layout.erb
130
+ has_rdoc: