kingfisher 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +15 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/kingfisher.gemspec +27 -0
- data/lib/kingfisher.rb +4 -0
- data/lib/kingfisher/app.rb +41 -0
- data/lib/kingfisher/controller.rb +63 -0
- data/lib/kingfisher/csrf.rb +41 -0
- data/lib/kingfisher/database_backends/postgresql.rb +47 -0
- data/lib/kingfisher/database_backends/sqlite3.rb +47 -0
- data/lib/kingfisher/middleware.rb +27 -0
- data/lib/kingfisher/middlewares/file_logger.rb +23 -0
- data/lib/kingfisher/model.rb +14 -0
- data/lib/kingfisher/repo.rb +27 -0
- data/lib/kingfisher/route_set.rb +85 -0
- data/lib/kingfisher/router.rb +45 -0
- data/lib/kingfisher/sqlite3_backend.rb +40 -0
- data/lib/kingfisher/version.rb +3 -0
- data/lib/kingfisher/view.rb +35 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 96ea6b666ef0a71463260c13c70d02f4362432bd
|
4
|
+
data.tar.gz: 41af6c0139e78cd221d4147b8bb442d46ef97615
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8634da7fd4c0176e59d5364e620a9d70fb69386a9de7bc705d625d387aaaebd9484c60ae9819a2f3e674a0eb02e943d07e113106dc8f5d51f18be1ace809c128
|
7
|
+
data.tar.gz: a551fa15a79e8dff367fa70bd4c204b07b3cb9adb33452cea80fee4da602c136f7d16c6f80670711518700b0840391dcdd122d2b0d54342e1db9204d6f8da912
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at halogenandtoast@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Matthew Mongeau
|
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
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "kingfisher"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/kingfisher.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "kingfisher/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "kingfisher"
|
8
|
+
spec.version = Kingfisher::VERSION
|
9
|
+
spec.authors = ["Matthew Mongeau"]
|
10
|
+
spec.email = ["halogenandtoast@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A modular web framework with opinions}
|
13
|
+
spec.description = %q{A modular web framework with opinions}
|
14
|
+
spec.homepage = "https://github.com/halogenandtoast/kingfisher"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
data/lib/kingfisher.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Kingfisher
|
2
|
+
class App
|
3
|
+
def initialize(router, config)
|
4
|
+
@router = router
|
5
|
+
@config = config
|
6
|
+
@builder = Rack::Builder.new
|
7
|
+
|
8
|
+
config.middlewares.each do |middleware|
|
9
|
+
builder.use middleware.klass, *middleware.args, &middleware.block
|
10
|
+
end
|
11
|
+
|
12
|
+
builder.run(router)
|
13
|
+
|
14
|
+
run_initializers
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
request = Rack::Request.new(env)
|
19
|
+
request.env["repo"] = repo
|
20
|
+
|
21
|
+
builder.call(env)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
attr_reader :builder, :router, :config
|
26
|
+
|
27
|
+
def root
|
28
|
+
config.root
|
29
|
+
end
|
30
|
+
|
31
|
+
def repo
|
32
|
+
@_repo ||= config.repo.new(config.backend)
|
33
|
+
end
|
34
|
+
|
35
|
+
def run_initializers
|
36
|
+
Dir.glob("#{root}/config/initializers/*.rb").each do |file|
|
37
|
+
require file
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Kingfisher
|
2
|
+
class NullRepo
|
3
|
+
def all(_)
|
4
|
+
[]
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class Redirect
|
9
|
+
DEFAULTS = {
|
10
|
+
status_code: 302,
|
11
|
+
body: ["You are being redirected"],
|
12
|
+
headers: { "Content-Type" => "plain/text" }
|
13
|
+
}
|
14
|
+
def initialize(to:, **options)
|
15
|
+
@path = to
|
16
|
+
@options = DEFAULTS.merge(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def status_code
|
20
|
+
options[:status_code]
|
21
|
+
end
|
22
|
+
|
23
|
+
def headers
|
24
|
+
options[:headers].merge("Location" => path)
|
25
|
+
end
|
26
|
+
|
27
|
+
def body
|
28
|
+
options[:body]
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
attr_reader :path, :options
|
33
|
+
end
|
34
|
+
|
35
|
+
class Controller
|
36
|
+
def initialize(env)
|
37
|
+
@env = env
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
attr_reader :env
|
42
|
+
|
43
|
+
def request
|
44
|
+
@_request ||= Rack::Request.new(env)
|
45
|
+
end
|
46
|
+
|
47
|
+
def view(view_class, locals: {})
|
48
|
+
view_class.new(request, locals: locals)
|
49
|
+
end
|
50
|
+
|
51
|
+
def repo
|
52
|
+
request.env.fetch("repo") { NullRepo.new }
|
53
|
+
end
|
54
|
+
|
55
|
+
def params
|
56
|
+
request.params
|
57
|
+
end
|
58
|
+
|
59
|
+
def redirect(path)
|
60
|
+
Redirect.new(path)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Kingfisher
|
2
|
+
class CSRF
|
3
|
+
def initialize(request)
|
4
|
+
@request = request
|
5
|
+
end
|
6
|
+
|
7
|
+
def token
|
8
|
+
session[:csrf_token] ||= SecureRandom.base64(32)
|
9
|
+
end
|
10
|
+
|
11
|
+
def reset!
|
12
|
+
session.delete(:csrf_token)
|
13
|
+
end
|
14
|
+
|
15
|
+
def safe?
|
16
|
+
return true if request.get? || request.head?
|
17
|
+
return true if request[:csrf_token] == token
|
18
|
+
|
19
|
+
request.env["HTTP_X_CSRF_TOKEN"] == token
|
20
|
+
end
|
21
|
+
|
22
|
+
def unsafe?
|
23
|
+
!safe?
|
24
|
+
end
|
25
|
+
|
26
|
+
def form_input
|
27
|
+
%Q(<input type="hidden" name="csrf_token" value="#{token}">)
|
28
|
+
end
|
29
|
+
|
30
|
+
def meta_tag
|
31
|
+
%Q(<meta name="csrf_token" content="#{token}">)
|
32
|
+
end
|
33
|
+
|
34
|
+
def session
|
35
|
+
request.env["rack.session"]
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
attr_reader :request
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "sequel"
|
2
|
+
|
3
|
+
module Kingfisher
|
4
|
+
module DatabaseBackends
|
5
|
+
class PostgreSQL
|
6
|
+
def initialize(database_url:)
|
7
|
+
@database_url = database_url
|
8
|
+
@db = open_db
|
9
|
+
end
|
10
|
+
|
11
|
+
def all(model)
|
12
|
+
db[table_name(model)].all
|
13
|
+
end
|
14
|
+
|
15
|
+
def create(model, params)
|
16
|
+
id = db[table_name(model)].insert(params)
|
17
|
+
attributes = symbolize_keys(params).merge(id: id)
|
18
|
+
model.new(attributes)
|
19
|
+
end
|
20
|
+
|
21
|
+
def find(model, id)
|
22
|
+
find_by(model, id: id)
|
23
|
+
end
|
24
|
+
|
25
|
+
def find_by(model, attributes)
|
26
|
+
db[table_name(model)][**attributes]
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
attr_reader :db, :database_url
|
31
|
+
|
32
|
+
def open_db
|
33
|
+
Sequel.connect database_url
|
34
|
+
end
|
35
|
+
|
36
|
+
def table_name(model)
|
37
|
+
model.name.downcase.to_sym
|
38
|
+
end
|
39
|
+
|
40
|
+
def symbolize_keys(hash)
|
41
|
+
hash.each_with_object({}) do |(key, value), new_hash|
|
42
|
+
new_hash[key.to_sym] = value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "sequel"
|
2
|
+
|
3
|
+
module Kingfisher
|
4
|
+
module DatabaseBackends
|
5
|
+
class SqlLite3
|
6
|
+
def initialize(database_url:)
|
7
|
+
@database_url = database_url
|
8
|
+
@db = open_db
|
9
|
+
end
|
10
|
+
|
11
|
+
def all(model)
|
12
|
+
db[table_name(model)].all
|
13
|
+
end
|
14
|
+
|
15
|
+
def create(model, params)
|
16
|
+
id = db[table_name(model)].insert(params)
|
17
|
+
attributes = symbolize_keys(params).merge(id: id)
|
18
|
+
model.new(attributes)
|
19
|
+
end
|
20
|
+
|
21
|
+
def find(model, id)
|
22
|
+
find_by(model, id: id)
|
23
|
+
end
|
24
|
+
|
25
|
+
def find_by(model, attributes)
|
26
|
+
db[table_name(model)][**attributes]
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
attr_reader :db, :database_url
|
31
|
+
|
32
|
+
def open_db
|
33
|
+
Sequel.connect database_url
|
34
|
+
end
|
35
|
+
|
36
|
+
def table_name(model)
|
37
|
+
model.name.downcase.to_sym
|
38
|
+
end
|
39
|
+
|
40
|
+
def symbolize_keys(hash)
|
41
|
+
hash.each_with_object({}) do |(key, value), new_hash|
|
42
|
+
new_hash[key.to_sym] = value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Kingfisher
|
2
|
+
class Middleware
|
3
|
+
attr_reader :klass, :args, :block
|
4
|
+
def initialize(klass, args, block)
|
5
|
+
@klass = klass
|
6
|
+
@args = args
|
7
|
+
@block = block
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class MiddlewareStack
|
12
|
+
def initialize
|
13
|
+
@middlewares = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def each
|
17
|
+
middlewares.each { |x| yield x }
|
18
|
+
end
|
19
|
+
|
20
|
+
def use(klass, *args, &block)
|
21
|
+
middlewares.push(Middleware.new(klass, args, block))
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
attr_reader :middlewares
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Kingfisher
|
2
|
+
module Middlewares
|
3
|
+
class FileLogger
|
4
|
+
def initialize(app, args)
|
5
|
+
@app = app
|
6
|
+
@filename = args[:file]
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
app.call(env).tap do |response|
|
11
|
+
log(response.inspect)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
attr_reader :app, :filename
|
17
|
+
|
18
|
+
def log(message)
|
19
|
+
File.open(filename, "a+") { |file| file.puts message }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Kingfisher
|
2
|
+
class Repo
|
3
|
+
def initialize(backend)
|
4
|
+
@backend = backend
|
5
|
+
end
|
6
|
+
|
7
|
+
def all(model)
|
8
|
+
backend.all(model)
|
9
|
+
end
|
10
|
+
|
11
|
+
def find(model, id)
|
12
|
+
backend.find(model, id)
|
13
|
+
end
|
14
|
+
|
15
|
+
def create(model, params)
|
16
|
+
backend.create(model, params)
|
17
|
+
end
|
18
|
+
|
19
|
+
def find_by(model, attributes)
|
20
|
+
backend.find_by(model, attributes)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
attr_reader :backend
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module Kingfisher
|
2
|
+
class RouteSet
|
3
|
+
def initialize
|
4
|
+
@routes = []
|
5
|
+
end
|
6
|
+
|
7
|
+
def match(request)
|
8
|
+
mroute = @routes.find { |route| route.match?(request) }
|
9
|
+
Operation.either(->(_){ raise NoRouteError, "#{request.request_method} #{request.path}" }, mroute).result
|
10
|
+
end
|
11
|
+
|
12
|
+
def get(url, controller, action)
|
13
|
+
@routes << Route.new(:get, url, controller, action)
|
14
|
+
end
|
15
|
+
|
16
|
+
def post(url, controller, action)
|
17
|
+
@routes << Route.new(:post, url, controller, action)
|
18
|
+
end
|
19
|
+
|
20
|
+
def delete(url, controller, action)
|
21
|
+
@routes << Route.new(:delete, url, controller, action)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Route
|
26
|
+
def initialize(verb, url, app, action)
|
27
|
+
@verb = verb
|
28
|
+
@url = url
|
29
|
+
@app = app
|
30
|
+
@action = action
|
31
|
+
end
|
32
|
+
|
33
|
+
def match?(request)
|
34
|
+
request.request_method.downcase.to_sym == verb && request.path == url
|
35
|
+
end
|
36
|
+
|
37
|
+
def call(env)
|
38
|
+
response = app.new(env).public_send(action)
|
39
|
+
[response.status_code, response.headers, response.body]
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
attr_reader :verb, :url, :app, :action
|
44
|
+
end
|
45
|
+
|
46
|
+
module Operation
|
47
|
+
def self.either(failure, maybe)
|
48
|
+
if maybe.nil?
|
49
|
+
Failure.new(failure)
|
50
|
+
else
|
51
|
+
Success.new(maybe)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Failure
|
57
|
+
attr_reader :failure
|
58
|
+
def initialize(failure)
|
59
|
+
@failure = failure
|
60
|
+
end
|
61
|
+
|
62
|
+
def result
|
63
|
+
@failure
|
64
|
+
end
|
65
|
+
|
66
|
+
def success?
|
67
|
+
false
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class Success
|
72
|
+
attr_reader :success
|
73
|
+
def initialize(success)
|
74
|
+
@success = success
|
75
|
+
end
|
76
|
+
|
77
|
+
def result
|
78
|
+
@success
|
79
|
+
end
|
80
|
+
|
81
|
+
def success?
|
82
|
+
true
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Kingfisher
|
2
|
+
NoRouteError = Class.new(StandardError)
|
3
|
+
|
4
|
+
class Router
|
5
|
+
def call(env)
|
6
|
+
request = Rack::Request.new(env)
|
7
|
+
|
8
|
+
if csrf(request).unsafe?
|
9
|
+
forbidden
|
10
|
+
else
|
11
|
+
route(request).call(env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def route_set
|
18
|
+
@_route_set ||= RouteSet.new
|
19
|
+
end
|
20
|
+
|
21
|
+
def route(request)
|
22
|
+
route_set.match(request)
|
23
|
+
end
|
24
|
+
|
25
|
+
def get(url, controller, action)
|
26
|
+
route_set.get(url, controller, action)
|
27
|
+
end
|
28
|
+
|
29
|
+
def post(url, controller, action)
|
30
|
+
route_set.post(url, controller, action)
|
31
|
+
end
|
32
|
+
|
33
|
+
def delete(url, controller, action)
|
34
|
+
route_set.delete(url, controller, action)
|
35
|
+
end
|
36
|
+
|
37
|
+
def forbidden
|
38
|
+
[403, {"Content-Type" => "text/plain"}, ["Forbidden"]]
|
39
|
+
end
|
40
|
+
|
41
|
+
def csrf(request)
|
42
|
+
@_csrf ||= CSRF.new(request)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "sequel"
|
2
|
+
|
3
|
+
module Kingfisher
|
4
|
+
class SqlLite3Backend
|
5
|
+
def initialize(file)
|
6
|
+
@db = Sequel.connect("sqlite://#{file}")
|
7
|
+
end
|
8
|
+
|
9
|
+
def all(model)
|
10
|
+
db[table_name(model)].to_a
|
11
|
+
end
|
12
|
+
|
13
|
+
def create(model, params)
|
14
|
+
id = db[table_name(model)].insert(params)
|
15
|
+
attributes = symbolize_keys(params).merge(id: id)
|
16
|
+
model.new(attributes)
|
17
|
+
end
|
18
|
+
|
19
|
+
def find(model, id)
|
20
|
+
find_by(model, id: id)
|
21
|
+
end
|
22
|
+
|
23
|
+
def find_by(model, attributes)
|
24
|
+
model.new(db[table_name(model)][**attributes])
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
attr_reader :db
|
29
|
+
|
30
|
+
def table_name(model)
|
31
|
+
model.name.downcase.to_sym
|
32
|
+
end
|
33
|
+
|
34
|
+
def symbolize_keys(hash)
|
35
|
+
hash.each_with_object({}) do |(key, value), new_hash|
|
36
|
+
new_hash[key.to_sym] = value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "slim"
|
2
|
+
|
3
|
+
module Kingfisher
|
4
|
+
class View
|
5
|
+
def initialize(request, locals: {})
|
6
|
+
@request = request
|
7
|
+
@locals = locals
|
8
|
+
end
|
9
|
+
|
10
|
+
def status_code
|
11
|
+
@_status_code ||= 200
|
12
|
+
end
|
13
|
+
|
14
|
+
def headers
|
15
|
+
@_headers ||= { "Content-Type" => "text/html" }
|
16
|
+
end
|
17
|
+
|
18
|
+
def body
|
19
|
+
[render]
|
20
|
+
end
|
21
|
+
|
22
|
+
def render
|
23
|
+
Slim::Template.
|
24
|
+
new("web/templates/#{template}.slim").
|
25
|
+
render(self)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
attr_reader :locals, :request
|
30
|
+
|
31
|
+
def template
|
32
|
+
self.class.name.gsub(/View$/, "").gsub(/([a-z])([A-Z])/, '\1_\2').downcase
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kingfisher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Mongeau
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-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.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A modular web framework with opinions
|
56
|
+
email:
|
57
|
+
- halogenandtoast@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- kingfisher.gemspec
|
73
|
+
- lib/kingfisher.rb
|
74
|
+
- lib/kingfisher/app.rb
|
75
|
+
- lib/kingfisher/controller.rb
|
76
|
+
- lib/kingfisher/csrf.rb
|
77
|
+
- lib/kingfisher/database_backends/postgresql.rb
|
78
|
+
- lib/kingfisher/database_backends/sqlite3.rb
|
79
|
+
- lib/kingfisher/middleware.rb
|
80
|
+
- lib/kingfisher/middlewares/file_logger.rb
|
81
|
+
- lib/kingfisher/model.rb
|
82
|
+
- lib/kingfisher/repo.rb
|
83
|
+
- lib/kingfisher/route_set.rb
|
84
|
+
- lib/kingfisher/router.rb
|
85
|
+
- lib/kingfisher/sqlite3_backend.rb
|
86
|
+
- lib/kingfisher/version.rb
|
87
|
+
- lib/kingfisher/view.rb
|
88
|
+
homepage: https://github.com/halogenandtoast/kingfisher
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
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.5.1
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: A modular web framework with opinions
|
112
|
+
test_files: []
|