jails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/jails.gemspec +36 -0
- data/lib/jails.rb +17 -0
- data/lib/jails/controller.rb +68 -0
- data/lib/jails/hacktive_record.rb +71 -0
- data/lib/jails/routing.rb +43 -0
- data/lib/jails/support.rb +8 -0
- data/lib/jails/version.rb +3 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f53ada8cf3d1d775d9f47993c8139a883211f4ab
|
4
|
+
data.tar.gz: ea8af3753aea35be1c8a300f99b81c56a9ba776e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7024db4c3466b3f4f2f0b23257db233155b6ca586f6e256a269bd46ddfa846c5ea3a9e36feccff5e15d25ca01e0f0d7dad5d59ebb14155862d13bcc0f076d163
|
7
|
+
data.tar.gz: '0483d20143deba21b48bd844eae76cfb80307b1a017aaac9bb61c19592fd6236a113cde9609771f85ab3b73a170041a9b5e488070525b3ee604bdf6d39c38370'
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Steve Carey
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Jails
|
2
|
+
|
3
|
+
This gem is for learning purposes only. It is the companion to the tutorial Build Your Own Rails. Full explanation of the code in this gem and how to use it with a web application are at:
|
4
|
+
https://www.techandstartup.org/tutorials/build-your-own-rails
|
5
|
+
This is a very simplified Web framework using the Model View Controller architecture, following as much of the feel of Rails as possible while keeping it very simple.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'jails'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install jails
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
This Gem is for learning purposes only and it is not meant to be used for a real project.
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jails.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/jails.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "jails/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jails"
|
8
|
+
spec.version = Jails::VERSION
|
9
|
+
spec.authors = ["Steve Carey"]
|
10
|
+
spec.email = ["steve981cr@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{For-learning-purposes-only web application framework.}
|
13
|
+
spec.description = %q{Ruby in Jails is a cheap, primitive, amateur attempt at a Web MVC framework strictly for learning purposes.}
|
14
|
+
spec.homepage = "https://www.techandstartup.org/tutorials/build-your-own-rails"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = Dir["LICENSE.txt","Rakefile","README.md","jails.gemspec","lib/**/*"]
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
34
|
+
|
35
|
+
spec.add_dependency "rack", "~> 2.0"
|
36
|
+
end
|
data/lib/jails.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require('jails/version.rb')
|
2
|
+
require('jails/support.rb')
|
3
|
+
require('jails/routing.rb')
|
4
|
+
require('jails/controller.rb')
|
5
|
+
require('jails/hacktive_record.rb')
|
6
|
+
|
7
|
+
module Jails
|
8
|
+
class Application
|
9
|
+
# Instantiate a Request object, instantiate Routing object and call dispatch method, ultimately return response.
|
10
|
+
def call(env)
|
11
|
+
request = Rack::Request.new(env)
|
12
|
+
puts("\n\nStarted #{request.request_method} \"#{request.path}\" for #{request.ip} #{Time.now}")
|
13
|
+
response = Routing.new(request).dispatch
|
14
|
+
return response
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
class Controller
|
2
|
+
include Support
|
3
|
+
|
4
|
+
# Set request object as an attribute accessible with @request variable.
|
5
|
+
def initialize(request)
|
6
|
+
@request = request
|
7
|
+
puts("\s Parameters #{@request.params}")
|
8
|
+
end
|
9
|
+
|
10
|
+
# helper method, params returns @request.params
|
11
|
+
def params
|
12
|
+
@request.params
|
13
|
+
end
|
14
|
+
|
15
|
+
# Return Rack response object with the erb template.
|
16
|
+
def render
|
17
|
+
resource = params[:resource]
|
18
|
+
action = params[:action]
|
19
|
+
template = "#{resource}/#{action}.html.erb"
|
20
|
+
file = File.join('app', 'views', template)
|
21
|
+
if File.exist?(file)
|
22
|
+
puts("\s Rendering #{template}") # Prints in log
|
23
|
+
render_template = ERB.new(File.read(file)).result(binding)
|
24
|
+
puts "\s Rendered #{template}"
|
25
|
+
response = Rack::Response.new([render_template], 200, {"Content-Type" => "text/html"})
|
26
|
+
puts "Completed 200 OK"
|
27
|
+
return response
|
28
|
+
else
|
29
|
+
puts("\s Missing Template #{template}.")
|
30
|
+
response = Rack::Response.new(["Nothing found"], 404, {"Content-Type" => "text/html"})
|
31
|
+
return response
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Return rendered partial template to be inserted into the parent template that called it.
|
36
|
+
def render_partial(template)
|
37
|
+
file = File.join('app', 'views', template)
|
38
|
+
if File.exists?(file)
|
39
|
+
rendered_partial = ERB.new(File.read(file)).result(binding)
|
40
|
+
puts "\s Rendered #{template}"
|
41
|
+
return rendered_partial
|
42
|
+
else
|
43
|
+
puts("\s Missing Template #{template}.")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Return Rack response with header field Location assigned to path in argument.
|
48
|
+
def redirect_to(path)
|
49
|
+
response = Rack::Response.new([], 302, {"Location" => path})
|
50
|
+
return response
|
51
|
+
end
|
52
|
+
|
53
|
+
# Set cookie with parameter key:value, then redirect back.
|
54
|
+
def set_new_cookie(key, value)
|
55
|
+
Rack::Response.new do |response|
|
56
|
+
response.set_cookie(key, value)
|
57
|
+
response.redirect(@request.referer || @request.path)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Delete cookie with parameter key and redirect back.
|
62
|
+
def delete_the_cookie(key)
|
63
|
+
Rack::Response.new do |response|
|
64
|
+
response.delete_cookie("greet")
|
65
|
+
response.redirect(@request.referer || @request.path)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "sqlite3"
|
2
|
+
|
3
|
+
# Simplistic ORM library using the Active Record pattern
|
4
|
+
module HacktiveRecord
|
5
|
+
class Base
|
6
|
+
DB = SQLite3::Database.new("db/development.sqlite3")
|
7
|
+
|
8
|
+
# Return table name string by transforming the model class's name to lower case plural.
|
9
|
+
def self.table
|
10
|
+
table_name = self.name.downcase + "s"
|
11
|
+
return table_name
|
12
|
+
end
|
13
|
+
|
14
|
+
# Return array of DB column names converted to symbols.
|
15
|
+
def self.columns
|
16
|
+
columns = DB.table_info(table).map { |info| info["name"].to_sym }
|
17
|
+
return columns
|
18
|
+
end
|
19
|
+
|
20
|
+
# Return number of rows by executing a count query on the database for the resource.
|
21
|
+
def self.count
|
22
|
+
rows_count = DB.execute("SELECT COUNT(*) FROM #{table}")[0][0]
|
23
|
+
return rows_count
|
24
|
+
end
|
25
|
+
|
26
|
+
# Return array of all rows in queried from the database table, converted to objects of the resource.
|
27
|
+
def self.all
|
28
|
+
rows = DB.execute("SELECT * FROM #{table}")
|
29
|
+
objects = rows.map do |row|
|
30
|
+
attributes = Hash[columns.zip(row)]
|
31
|
+
self.new(attributes)
|
32
|
+
end
|
33
|
+
return objects
|
34
|
+
end
|
35
|
+
|
36
|
+
# Return an object by querying the database for the requested row searching by id.
|
37
|
+
def self.find(id)
|
38
|
+
row = DB.execute("SELECT * FROM #{table} WHERE id = ? LIMIT 1", id).first
|
39
|
+
attributes = Hash[columns.zip(row)]
|
40
|
+
object = self.new(attributes)
|
41
|
+
return object
|
42
|
+
end
|
43
|
+
|
44
|
+
# Save object as a new row to the database table, returning the object with the new attribute's id value.
|
45
|
+
def save
|
46
|
+
new_object = self
|
47
|
+
columns = new_object.class.columns
|
48
|
+
columns.delete(:id)
|
49
|
+
placeholders = (["?"] * (columns.size)).join(",")
|
50
|
+
values = columns.map { |key| self.send(key) }
|
51
|
+
columns = columns.join(",")
|
52
|
+
DB.execute("INSERT INTO #{self.class.table} (#{columns}) VALUES (#{placeholders})", values)
|
53
|
+
new_object.id = DB.execute("SELECT last_insert_rowid()")[0][0]
|
54
|
+
return new_object
|
55
|
+
end
|
56
|
+
|
57
|
+
# Modify row in database.
|
58
|
+
def update(attributes={})
|
59
|
+
columns = attributes.keys
|
60
|
+
columns = columns.map { |column| "#{column}=?" }.join(",")
|
61
|
+
values = attributes.values
|
62
|
+
values << id
|
63
|
+
DB.execute("UPDATE #{self.class.table} SET #{columns} WHERE id = ?", values)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Delete row from database.
|
67
|
+
def destroy
|
68
|
+
DB.execute("DELETE FROM #{self.class.table} WHERE id = ?", id)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Routing
|
2
|
+
|
3
|
+
# Set request object as an attribute. Declare a routes attribute. Call the draw method to populate it with a hash of routes.
|
4
|
+
def initialize(request)
|
5
|
+
@request = request
|
6
|
+
@routes = {}
|
7
|
+
draw(Application.routes)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Build the @routes hash by evaluating the block passed in as a proc object from the Application.routes method.
|
11
|
+
def draw(block)
|
12
|
+
instance_eval(&block)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Add route to @routes hash with the url's path as the key and target as the value.
|
16
|
+
def match(path, target)
|
17
|
+
@routes["#{path}"] = target
|
18
|
+
end
|
19
|
+
|
20
|
+
# Match request path to @routes hash. If match, create controller object and call the action.
|
21
|
+
def dispatch
|
22
|
+
path = @request.path
|
23
|
+
id = nil
|
24
|
+
segments = path.split("/").reject { |segment| segment.empty? }
|
25
|
+
if segments[1] =~ /^\d+$/
|
26
|
+
id = segments[1]
|
27
|
+
segments[1] = ":id"
|
28
|
+
path = "/#{segments.join('/')}"
|
29
|
+
end
|
30
|
+
if @routes.key?(path)
|
31
|
+
target = @routes[path]
|
32
|
+
resource_name, action_name = target.split('#')
|
33
|
+
@request.params.merge!(resource: resource_name, action: action_name, id: id)
|
34
|
+
klass = Object.const_get("#{resource_name.capitalize}Controller")
|
35
|
+
puts("Processing by #{klass}##{action_name}")
|
36
|
+
klass.new(@request).send(action_name)
|
37
|
+
else
|
38
|
+
Rack::Response.new(["Nothing found"], 404, {"Content-Type" => "text/html"})
|
39
|
+
end
|
40
|
+
rescue Exception => error
|
41
|
+
Rack::Response.new(["Internal error"], 500, {"Content-Type" => "text/html"})
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
module Support
|
2
|
+
# Transform string, downcase all words, capitalize each not on list, capitalize first word.
|
3
|
+
def titleize(string)
|
4
|
+
words_not_to_capitalize = ["the","a","an","and","but","for","of","to","at","by","from"]
|
5
|
+
s = string.split.each{|word| word.capitalize! unless words_not_to_capitalize.include? (word.downcase) }
|
6
|
+
s[0].capitalize + " " + s[1..-1].join(" ")
|
7
|
+
end
|
8
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Carey
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rack
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
69
|
+
description: Ruby in Jails is a cheap, primitive, amateur attempt at a Web MVC framework
|
70
|
+
strictly for learning purposes.
|
71
|
+
email:
|
72
|
+
- steve981cr@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- LICENSE.txt
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- jails.gemspec
|
81
|
+
- lib/jails.rb
|
82
|
+
- lib/jails/controller.rb
|
83
|
+
- lib/jails/hacktive_record.rb
|
84
|
+
- lib/jails/routing.rb
|
85
|
+
- lib/jails/support.rb
|
86
|
+
- lib/jails/version.rb
|
87
|
+
homepage: https://www.techandstartup.org/tutorials/build-your-own-rails
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata:
|
91
|
+
allowed_push_host: https://rubygems.org
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.6.13
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: For-learning-purposes-only web application framework.
|
112
|
+
test_files: []
|