hotchkiss 0.0.1

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.txt ADDED
@@ -0,0 +1,24 @@
1
+ * Copyright (c) 2012, Thibaut Deloffre
2
+ * All rights reserved.
3
+ * Redistribution and use in source and binary forms, with or without
4
+ * modification, are permitted provided that the following conditions are met:
5
+ *
6
+ * * Redistributions of source code must retain the above copyright
7
+ * notice, this list of conditions and the following disclaimer.
8
+ * * Redistributions in binary form must reproduce the above copyright
9
+ * notice, this list of conditions and the following disclaimer in the
10
+ * documentation and/or other materials provided with the distribution.
11
+ * * Neither the name of RocknRoot nor the names of its contributors may
12
+ * be used to endorse or promote products derived from this software
13
+ * without specific prior written permission.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY
16
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
19
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/bin/hk ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'thor'
4
+ require 'rack'
5
+ require 'tilt'
6
+ require 'hotchkiss'
7
+
8
+ class Generator < Thor
9
+
10
+ TEMPLATE_DIR = Gem::Specification.find_by_name("hotchkiss").gem_dir +
11
+ "/lib/hotchkiss/templates/"
12
+
13
+ desc "new NAME", "Create new hotchkiss application directories with skel files."
14
+ def new(name)
15
+ dir = Dir.pwd + '/' + name
16
+ FileUtils.mkdir(dir)
17
+ FileUtils.cp_r(TEMPLATE_DIR + "new/.", dir)
18
+ end
19
+
20
+ desc "server", "Run development server"
21
+ def server(port = nil)
22
+ port ||= 3000
23
+ host ||= "127.0.0.1"
24
+ [:INT, :TERM].each { |sig| trap(sig) { exit } }
25
+ options = {}
26
+ options[:Host] = host
27
+ options[:Port] = port
28
+ options[:config] = "config.ru"
29
+ options[:server] = :webrick
30
+ HK::Server.start!(options)
31
+ end
32
+
33
+ end # Generator
34
+
35
+ Generator.start(ARGV)
@@ -0,0 +1,62 @@
1
+ require 'rack/utils'
2
+
3
+ module HK
4
+
5
+ class Application
6
+
7
+ @@router = nil
8
+ @@root = nil
9
+ @@favicon = nil
10
+
11
+ attr_accessor :router, :root
12
+
13
+ def self.router=(router)
14
+ @@router = router if @@router.nil?
15
+ end
16
+
17
+ def self.root=(root)
18
+ @@root = root if @@root.nil?
19
+ begin
20
+ @@favicon = File.read(File.join("#{root}/public/favicon.ico"))
21
+ rescue
22
+ @@favicon = nil
23
+ end
24
+ end
25
+
26
+ def self.root
27
+ @@root
28
+ end
29
+
30
+ def self.favicon
31
+ @@favicon
32
+ end
33
+
34
+ def self.burnbabyburn!
35
+ lambda do |env|
36
+ begin
37
+ route = @@router.match?(env)
38
+ if !route.nil? && route.has_key?(:special)
39
+ env['hk.controller'] = route[:controller]
40
+ elsif !route.nil?
41
+ env['hk.controller'] = "#{route[:controller].capitalize}Controller"
42
+ else
43
+ raise Exception, "Unknown route for path: #{env['REQUEST_PATH']}"
44
+ end
45
+ env['hk.action'] = route[:action]
46
+ Object.const_get(env['hk.controller']).new.call(env)
47
+ rescue Exception => e
48
+ env['hk.action'] = "on_exception"
49
+ env['hk.exception'] = e
50
+ if Object.const_get("ApplicationController").instance_methods(false).include?(:on_exception)
51
+ env['hk.controller'] = :ApplicationController
52
+ else
53
+ env['hk.controller'] = :FastResponder
54
+ end
55
+ Object.const_get(env['hk.controller']).new.call(env)
56
+ end
57
+ end
58
+ end
59
+
60
+ end # Application
61
+
62
+ end ## HK
@@ -0,0 +1,48 @@
1
+ module HK
2
+
3
+ class Controller
4
+
5
+ def call(env)
6
+ @request = Rack::Request.new(env)
7
+ @response = Rack::Response.new()
8
+ resp = self.send(env['hk.action'])
9
+ @response.write(resp)
10
+ @response.finish
11
+ end
12
+
13
+ private
14
+
15
+ def params
16
+ @request.params
17
+ end
18
+
19
+ def redirect_to(url)
20
+ @response.redirect(url)
21
+ end
22
+
23
+ def render(tpl_name = nil)
24
+ full_ctrl_name = @request.env['hk.controller']
25
+ # "ApplicationController" -> "Application".
26
+ full_ctrl_name = full_ctrl_name[0..-11]
27
+ tpl_name ||= @request.env['hk.action']
28
+ tpl_name = '/' + tpl_name.to_s + '.html.erb'
29
+ controller_dir = full_ctrl_name.downcase
30
+ view_dir = HK::Application.root + "/code/app/views/"
31
+ tpl_path = view_dir + controller_dir + tpl_name
32
+ layout_path = view_dir + "application.html.erb"
33
+ Tilt.new(layout_path).render(self) do
34
+ Tilt.new(tpl_path).render(self)
35
+ end
36
+ end
37
+
38
+ def request
39
+ @request
40
+ end
41
+
42
+ def status(code)
43
+ @response.status = code.to_i
44
+ end
45
+
46
+ end # Controller
47
+
48
+ end ## HK
File without changes
@@ -0,0 +1,18 @@
1
+ class FastResponder < HK::Controller
2
+
3
+ def favicon
4
+ @favicon = HK::Application.favicon
5
+ status @favicon.nil? ? 500 : 200
6
+ @favicon
7
+ end
8
+
9
+ def on_exception
10
+ e = request.env["hk.exception"]
11
+ status 500
12
+ @class = e.class
13
+ @message = e.message
14
+ @backtrace = e.backtrace
15
+ "<html><title>ERROR</title><body>#{@class} : #{@message}<br />#{@backtrace.join("<br />")}</body><html>"
16
+ end
17
+
18
+ end
@@ -0,0 +1,52 @@
1
+ module HK
2
+
3
+ class Router
4
+
5
+ def initialize
6
+ @routes = []
7
+ yield(self)
8
+ finish
9
+ compute
10
+ end
11
+
12
+ def bind(path, infos)
13
+ route = {}
14
+ route[:path] = path
15
+ route = route.merge!(infos)
16
+ @routes << route
17
+ end
18
+
19
+ def root(infos)
20
+ raise TypeError unless infos.is_a?(Hash)
21
+ bind("/", infos)
22
+ end
23
+
24
+ def match?(env)
25
+ method = env['REQUEST_METHOD']
26
+ path = env['REQUEST_PATH'].eql?("/") ? "/" : env['REQUEST_PATH'].gsub(/\/$/, '')
27
+ @routes.each do |route|
28
+ if path.match(route[:regexp])
29
+ return route
30
+ end
31
+ end
32
+ return nil
33
+ end
34
+
35
+ private
36
+
37
+ def compute
38
+ @routes.each do |route|
39
+ computed_route = route[:path].gsub!(/((:\w+)|\*)/, /(\w+)/.to_s)
40
+ computed_route = "^#{route[:path]}$" if computed_route.nil?
41
+ computed_route = Regexp.new(computed_route)
42
+ route[:regexp] = computed_route
43
+ end
44
+ end
45
+
46
+ def finish
47
+ @routes << {:path => "/favicon.ico", :action => "favicon", :controller => :FastResponder, :special => true}
48
+ end
49
+
50
+ end # Router
51
+
52
+ end ## HK
@@ -0,0 +1,27 @@
1
+ module HK
2
+
3
+ class Server < Rack::Server
4
+
5
+ def initialize(options)
6
+ @options = options
7
+ end
8
+
9
+ def self.start!(options)
10
+ new(options).start
11
+ end
12
+
13
+ def options
14
+ @options
15
+ end
16
+
17
+ def start
18
+ time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
19
+ puts "[#{time}] => Hotchkiss - #{HK::VERSION} version"
20
+ puts "[#{time}] => It rolls on http://#{options[:Host]}:#{options[:Port]}"
21
+ [:INT, :TERM].each { |sig| trap(sig) { exit } }
22
+ super
23
+ end
24
+
25
+ end # Server
26
+
27
+ end ## HK
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rack'
4
+ gem 'thor'
5
+ gem 'tilt'
6
+ gem 'hotchkiss'
7
+
@@ -0,0 +1,3 @@
1
+ class ApplicationController < HK::Controller
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ HK::Application.router = HK::Router.new do |route|
2
+ # Your routes.
3
+ end
@@ -0,0 +1,11 @@
1
+ <?xml version="1.0" encoding="UTF-8" ?>
2
+ <!DOCTYPE html>
3
+ <html>
4
+ <head>
5
+ <title>
6
+ </title>
7
+ </head>
8
+ <body>
9
+ <%= yield %>
10
+ </body>
11
+ </html>
@@ -0,0 +1 @@
1
+ # In 0.0.2
@@ -0,0 +1,8 @@
1
+
2
+ Dir["#{HK::Application.root}/code/lib/**/*.rb"].each { |file| require file }
3
+ Dir["#{HK::Application.root}/code/app/**/*.rb"].each { |file| require file }
4
+
5
+ # Add your custom requires.
6
+ #
7
+ # require HK::Application.root + '/a/path'
8
+
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rack'
4
+ require 'tilt'
5
+ require 'hotchkiss'
6
+
7
+ HK::Application.root = ::File.expand_path(::File.dirname(__FILE__))
8
+ require HK::Application.root + '/config/init'
9
+
10
+ run HK::Application.burnbabyburn!
data/lib/hotchkiss.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'hotchkiss/router'
2
+ require 'hotchkiss/controller'
3
+ require 'hotchkiss/fast_responder'
4
+ require 'hotchkiss/application'
5
+ require 'hotchkiss/server'
6
+
7
+ module HK
8
+
9
+ VERSION = '0.0.1'
10
+
11
+ end ## HK
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hotchkiss
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thibaut Deloffre
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: thor
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rack
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: tilt
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Ruby web framework
95
+ email: tib@rocknroot.org
96
+ executables:
97
+ - hk
98
+ extensions: []
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ files:
102
+ - lib/hotchkiss/fast_responder.rb
103
+ - lib/hotchkiss/templates/new/Gemfile
104
+ - lib/hotchkiss/templates/new/public/favicon.ico
105
+ - lib/hotchkiss/templates/new/config.ru
106
+ - lib/hotchkiss/templates/new/code/app/routes.rb
107
+ - lib/hotchkiss/templates/new/code/app/controllers/application_controller.rb
108
+ - lib/hotchkiss/templates/new/code/app/views/application.html.erb
109
+ - lib/hotchkiss/templates/new/config/database.rb
110
+ - lib/hotchkiss/templates/new/config/init.rb
111
+ - lib/hotchkiss/errors.rb
112
+ - lib/hotchkiss/server.rb
113
+ - lib/hotchkiss/application.rb
114
+ - lib/hotchkiss/controller.rb
115
+ - lib/hotchkiss/router.rb
116
+ - lib/hotchkiss.rb
117
+ - LICENSE.txt
118
+ - bin/hk
119
+ homepage: https://github.com/RocknRoot/hotchkiss
120
+ licenses:
121
+ - BSD
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 1.8.24
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: It rolls !
144
+ test_files: []