arthurgeek-nyane 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Arthur Zapparoli
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ 'Software'), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,108 @@
1
+ Nyane
2
+ ===
3
+
4
+ * [http://github.com/arthurgeek/nyane](http://github.com/arthurgeek/nyane)
5
+
6
+ DESCRIPTION:
7
+ ------------
8
+
9
+ Nyane means insignificant in [Sesotho](http://en.wikipedia.org/wiki/Sesotho), a [Bantu Language](http://en.wikipedia.org/wiki/Bantu_languages).
10
+
11
+ As it's name says, Nyane is an insignificant framework. It was build for educational purposes only. It's not intended to be used in production.
12
+
13
+ Nyane is based on [Invisible](http://github.com/macournoyer/invisible/) (a nice pico-framework Rack-based) but way simpler.
14
+
15
+ INSTALLATION:
16
+ -------------
17
+
18
+ git clone git://github.com/arthurgeek/nyane.git
19
+ cd nyane
20
+ gem build nyane.gemspec
21
+ sudo gem install nyane --local
22
+
23
+ or
24
+
25
+ sudo gem install arthurgeek-nyane --source=http://gems.github.com
26
+
27
+ DEMO APP:
28
+ ------
29
+
30
+ require "rubygems"
31
+ require "nyane"
32
+
33
+ app = Nyane.new do
34
+ get "/" do
35
+ "Hello!"
36
+ end
37
+
38
+ get "/bye" do
39
+ "Goodbye"
40
+ end
41
+
42
+ get "/echo/(.*)" do |text|
43
+ "#{text}"
44
+ end
45
+
46
+ end
47
+
48
+ run app
49
+
50
+ ERB SUPPORT:
51
+ -------
52
+
53
+ Nyane supports ERB, but you need to explicitly require it
54
+
55
+ require "rubygems"
56
+ require "nyane"
57
+ require "nyane/erb"
58
+
59
+ app = Nyane.new do
60
+ get "/" do
61
+ erb :index
62
+ end
63
+
64
+ get "/foo" do
65
+ erb :foo, :layout => false
66
+ end
67
+
68
+ end
69
+
70
+ run app
71
+
72
+ The views need to be at `views/` or you can specify it with `app.views_directory = "directory_of_views"`. The layout used is `views/layout.erb`. To not render a layout, use `erb :template, :layout => false`. You can also tell which layout to use with `erb :template, :layout => :layout`
73
+
74
+ RUNNING THE APP:
75
+ -------
76
+
77
+ The app itself is a Rack config file, so, you can run with: `rackup demo.ru` and view it at `http://localhost:9292` or with Thin: `thin start -R demo.ru`
78
+
79
+ The extension needs to be .ru (rack config file)
80
+
81
+ MAINTAINER
82
+ ----------
83
+
84
+ * Arthur Zapparoli ([http://arthurgeek.net](http://arthurgeek.net))
85
+
86
+ LICENSE:
87
+ --------
88
+
89
+ (The MIT License)
90
+
91
+ Permission is hereby granted, free of charge, to any person obtaining
92
+ a copy of this software and associated documentation files (the
93
+ 'Software'), to deal in the Software without restriction, including
94
+ without limitation the rights to use, copy, modify, merge, publish,
95
+ distribute, sublicense, and/or sell copies of the Software, and to
96
+ permit persons to whom the Software is furnished to do so, subject to
97
+ the following conditions:
98
+
99
+ The above copyright notice and this permission notice shall be
100
+ included in all copies or substantial portions of the Software.
101
+
102
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
103
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
104
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
105
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
106
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
107
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
108
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,9 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'spec/rake/spectask'
4
+
5
+ Spec::Rake::SpecTask.new do |t|
6
+ t.spec_opts = %w(-fs -c)
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,43 @@
1
+ $:.unshift File.dirname(__FILE__) + "/../lib"
2
+
3
+ require "nyane"
4
+ require "nyane/erb"
5
+
6
+ app = Nyane.new do
7
+ get "/" do
8
+ "Hello!"
9
+ end
10
+
11
+ get "/bye" do
12
+ "Goodbye"
13
+ end
14
+
15
+ get "/redirect" do
16
+ redirect_to "/"
17
+ end
18
+
19
+ get "/time" do
20
+ @now = Time.now
21
+ erb :time
22
+ end
23
+
24
+ get "/welcome" do
25
+ erb :welcome, :layout => false
26
+ end
27
+
28
+ post '/greeter' do
29
+ @name = @params["name"]
30
+ erb :greeter
31
+ end
32
+
33
+ get "/echo/(.*)" do |text|
34
+ "#{text}"
35
+ end
36
+
37
+ get "/say/(.*)/to/(.*)" do |message, person|
38
+ "#{message} #{person}"
39
+ end
40
+
41
+ end
42
+
43
+ run app
@@ -0,0 +1 @@
1
+ <h2>Welcome <%= @name %>!<h2>
@@ -0,0 +1,10 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5
+ <title>Nyane App</title>
6
+ </head>
7
+ <body>
8
+ <%= yield %>
9
+ </body>
10
+ </html>
@@ -0,0 +1,2 @@
1
+ <h1>Hello!</h1>
2
+ <p>It's <%= @now %> now!</p>
@@ -0,0 +1,8 @@
1
+ <h1>Welcome!</h1>
2
+
3
+ <form action="greeter" method="post">
4
+ <label for="name">What's your name?</label>
5
+ <input type="text" name="name" id="name">
6
+
7
+ <p><input type="submit" value="Continue"></p>
8
+ </form>
@@ -0,0 +1,50 @@
1
+ require "rack"
2
+
3
+ class Nyane
4
+
5
+ attr_reader :request, :response
6
+
7
+ def initialize(&block)
8
+ @actions = []
9
+ @root = File.dirname(eval("__FILE__", block.binding))
10
+ @app = self
11
+ instance_eval(&block)
12
+ end
13
+
14
+ def get(route, &block)
15
+ @actions << [route, :get, block]
16
+ end
17
+
18
+ def post(route, &block)
19
+ @actions << [route, :post, block]
20
+ end
21
+
22
+ def redirect_to(path)
23
+ @response.status = 302
24
+ @response.headers["Location"] = path
25
+ end
26
+
27
+ def load(file)
28
+ path = File.join(@root, file) + ".rb"
29
+ eval(File.read(path), binding, path)
30
+ end
31
+
32
+ def call(env)
33
+ @request = Rack::Request.new(env)
34
+ @response = Rack::Response.new
35
+
36
+ @params = @request.params
37
+ path_info = nil
38
+ action = @actions.detect { |route, method, block| @request.request_method == method.to_s.upcase! && path_info = @request.path_info.match(Regexp.new("^\/?#{route}\/?$")) }
39
+
40
+ if action
41
+ @response.write(action.last.call(path_info[1..-1]))
42
+ else
43
+ @response.write("Not found")
44
+ @response.status = 404
45
+ end
46
+
47
+ @response.finish
48
+ end
49
+
50
+ end
@@ -0,0 +1,31 @@
1
+ begin
2
+ require "erubis"
3
+ rescue LoadError
4
+ require "erb"
5
+ end
6
+
7
+ require "nyane/renderer"
8
+
9
+ class Nyane
10
+
11
+ def erb(template, options={})
12
+ file = read_template_file(:erb, template)
13
+ layout = read_layout_file(:erb, options)
14
+
15
+ result = process(file)
16
+ result = process(layout) { result } if layout
17
+
18
+ result
19
+ end
20
+
21
+ private
22
+ def process(template)
23
+ begin
24
+ body = Erubis::Eruby.new(template).src
25
+ rescue NameError
26
+ body = ERB.new(template).src
27
+ end
28
+
29
+ eval(body,binding)
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ class Nyane
2
+
3
+ attr_accessor :views_directory
4
+
5
+ private
6
+
7
+ def read_template_file(renderer, template, layout=false)
8
+ self.views_directory ||= "views"
9
+ path = File.join(@root, self.views_directory,"/#{template.to_s}.#{renderer.to_s}")
10
+
11
+ if File.exists?(path)
12
+ File.read(path)
13
+ else
14
+ raise Errno::ENOENT.new(path) unless layout
15
+ end
16
+ end
17
+
18
+ def read_layout_file(renderer, options)
19
+ return if options[:layout] == false
20
+ layout_from_options = options[:layout] || :layout
21
+ read_template_file(renderer, layout_from_options, true)
22
+ end
23
+
24
+ end
@@ -0,0 +1,33 @@
1
+ Gem::Specification.new do |s|
2
+ # s.date = "Wed Dec 20 04:44:00 -0300 2008"
3
+ s.authors = ["Arthur Zapparoli"]
4
+ s.require_paths = ["lib"]
5
+ s.required_rubygems_version = ">= 0"
6
+ s.has_rdoc = false
7
+ s.files = ["nyane.gemspec",
8
+ "LICENSE",
9
+ "Rakefile",
10
+ "README.markdown",
11
+ "lib/nyane.rb",
12
+ "lib/nyane/renderer.rb",
13
+ "lib/nyane/erb.rb",
14
+ "demo/example.ru",
15
+ "demo/views/time.erb",
16
+ "demo/views/layout.erb",
17
+ "demo/views/welcome.erb",
18
+ "demo/views/greeter.erb",
19
+ "spec/app_spec.rb",
20
+ "spec/erb_spec.rb",
21
+ "spec/routing_spec.rb",
22
+ "spec/spec_helper.rb",
23
+ "spec/fixtures/with_layout/layout.erb",
24
+ "spec/fixtures/with_layout/my_layout.erb",
25
+ "spec/fixtures/with_layout/text.erb",
26
+ "spec/fixtures/without_layout/index.erb"]
27
+ s.email = ["arthurzap@gmail.com"]
28
+ s.version = "0.0.4"
29
+ s.homepage = "http://github.com/arthurgeek/nyane"
30
+ s.name = "nyane"
31
+ s.summary = "An insignificant rack-based framework"
32
+ s.add_dependency "rack", ">= 0"
33
+ end
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe "GET" do
4
+ before do
5
+ @app = Nyane.new do
6
+ get "/" do
7
+ nil
8
+ end
9
+
10
+ get "/text" do
11
+ "Text"
12
+ end
13
+ end
14
+ end
15
+
16
+ it "should handle result of nil" do
17
+ @app.mock.get("/").body.should == ""
18
+ end
19
+
20
+ it "should render any text" do
21
+ @app.mock.get("/text").body.should == "Text"
22
+ end
23
+ end
24
+
25
+ describe "POST" do
26
+ before do
27
+ @app = Nyane.new do
28
+ post "/" do
29
+ "#{@params.inspect}"
30
+ end
31
+ end
32
+ end
33
+
34
+ it "should capture POST parameters" do
35
+ @app.mock.post("/", :input => "text=Nyane").body.should == { "text" => 'Nyane' }.inspect
36
+ end
37
+ end
38
+
39
+ describe "redirect" do
40
+ before do
41
+ @app = Nyane.new do
42
+ get "/" do
43
+ "/"
44
+ end
45
+
46
+ get "/redirect" do
47
+ redirect_to "/"
48
+ end
49
+ end
50
+ end
51
+
52
+ it "should redirect to path" do
53
+ response = @app.mock.get("/redirect")
54
+ response.status.should == 302
55
+ response.location.should == "/"
56
+ response.body.should == "/"
57
+ end
58
+ end
@@ -0,0 +1,60 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+ require "nyane/erb"
3
+
4
+ describe "erb" do
5
+
6
+ describe "with default layout file" do
7
+
8
+ before do
9
+ @app = Nyane.new do
10
+ get "/" do
11
+ @var = "my_var"
12
+ erb :text, :layout => false
13
+ end
14
+
15
+ get "/layout" do
16
+ @var = "my_var"
17
+ erb :text
18
+ end
19
+
20
+ get "/my_layout" do
21
+ @var = "my_var"
22
+ erb :text, :layout => :my_layout
23
+ end
24
+ end
25
+
26
+ @app.views_directory = "fixtures/with_layout"
27
+
28
+ end
29
+
30
+ it "should render erb template with layout option as false" do
31
+ @app.mock.get("/").body.should == "my_var"
32
+ end
33
+
34
+ it "should render erb template with default layout" do
35
+ @app.mock.get("/layout").body.should == "<h1>my_var</h1>"
36
+ end
37
+
38
+ it "should render erb template with specified layout" do
39
+ @app.mock.get("/my_layout").body.should == "<h2>my_var</h2>"
40
+ end
41
+ end
42
+
43
+ describe "without default layout file" do
44
+
45
+ before do
46
+ @app = Nyane.new do
47
+ get "/" do
48
+ @var = "Nyane"
49
+ erb :index
50
+ end
51
+ end
52
+
53
+ @app.views_directory = "fixtures/without_layout"
54
+ end
55
+
56
+ it "should render erb template" do
57
+ @app.mock.get("/").body.should == "Nyane"
58
+ end
59
+ end
60
+ end
@@ -0,0 +1 @@
1
+ <h1><%= yield %></h1>
@@ -0,0 +1 @@
1
+ <h2><%= @var %></h2>
@@ -0,0 +1 @@
1
+ <%= @var %>
@@ -0,0 +1 @@
1
+ <%= @var %>
@@ -0,0 +1,65 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe "routing" do
4
+ before do
5
+ @app = Nyane.new do
6
+ get "/path" do
7
+ "/path"
8
+ end
9
+
10
+ get "/param/(.*)" do |name|
11
+ "#{name}"
12
+ end
13
+
14
+ get "/" do
15
+ "/"
16
+ end
17
+
18
+ get "no/slash" do
19
+ "no-slash"
20
+ end
21
+
22
+ get "/echo/(.*)" do |text|
23
+ "#{text}"
24
+ end
25
+
26
+ post "/post" do
27
+ "post"
28
+ end
29
+ end
30
+ end
31
+
32
+ it "should route /" do
33
+ @app.mock.get("/").body.should == "/"
34
+ end
35
+
36
+ it "should return 404 when no route" do
37
+ @app.mock.get("/no-route").status.should == 404
38
+ @app.mock.get("/no-route").body.should == "Not found"
39
+ end
40
+
41
+ it "should route /path" do
42
+ @app.mock.get("/path").body.should == "/path"
43
+ end
44
+
45
+ it "should route /post" do
46
+ @app.mock.post("/post").body.should == "post"
47
+ end
48
+
49
+ it "should route /param/:param" do
50
+ @app.mock.get("/param/hello").body.should == "hello"
51
+ end
52
+
53
+ it "should route with no leading slash" do
54
+ @app.mock.get("/no/slash").body.should == "no-slash"
55
+ end
56
+
57
+ it "should route ignore trailing slash" do
58
+ @app.mock.get("/no/slash/").body.should == "no-slash"
59
+ end
60
+
61
+ it "should match parameters from route" do
62
+ @app.mock.get("/echo/hello").body.should == "hello"
63
+ @app.mock.get("/echo/any-text").body.should == "any-text"
64
+ end
65
+ end
@@ -0,0 +1,10 @@
1
+ $:.unshift File.dirname(__FILE__) + "/../lib"
2
+ require "rubygems"
3
+ require "nyane"
4
+ require "spec"
5
+
6
+ class Nyane
7
+ def mock
8
+ @mock ||= Rack::MockRequest.new(@app)
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arthurgeek-nyane
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Arthur Zapparoli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-15 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description:
25
+ email:
26
+ - arthurzap@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - nyane.gemspec
35
+ - LICENSE
36
+ - Rakefile
37
+ - README.markdown
38
+ - lib/nyane.rb
39
+ - lib/nyane/renderer.rb
40
+ - lib/nyane/erb.rb
41
+ - demo/example.ru
42
+ - demo/views/time.erb
43
+ - demo/views/layout.erb
44
+ - demo/views/welcome.erb
45
+ - demo/views/greeter.erb
46
+ - spec/app_spec.rb
47
+ - spec/erb_spec.rb
48
+ - spec/routing_spec.rb
49
+ - spec/spec_helper.rb
50
+ - spec/fixtures/with_layout/layout.erb
51
+ - spec/fixtures/with_layout/my_layout.erb
52
+ - spec/fixtures/with_layout/text.erb
53
+ - spec/fixtures/without_layout/index.erb
54
+ has_rdoc: false
55
+ homepage: http://github.com/arthurgeek/nyane
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.2.0
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: An insignificant rack-based framework
80
+ test_files: []
81
+