puuko 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6c2b899f63a81940c060d5f108e61bea1d57f4fed212504f784ec3d8e7eca9d8
4
+ data.tar.gz: f33eb7b50260c2ca1a729fe07f5394e44c5b716a4833009ac4cffc09e2d64114
5
+ SHA512:
6
+ metadata.gz: 5518b44c8e8b6c6f1ea4ca1fe6bbf544aa22c82b1d7413c2762fab84f99b0f2b50cd350c8bd15a95382ad31a8883d83fb40b1ccb41a1f6b055abfd522e4e32cb
7
+ data.tar.gz: 9f8a62edce3d9f1c75db7179c6f450db5d3738d76043e7fcf2dd3b127311b483e4c25b537c4af3ff514e5cbe8f19e0280932ae1c7560314f79c8e124e3da5686
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.16.5
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in puuko.gemspec
6
+ gemspec
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ puuko (0.0.1)
5
+ sinatra (~> 2.0.5)
6
+ sinatra-contrib (~> 2.0.5)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ backports (3.14.0)
12
+ diff-lcs (1.3)
13
+ multi_json (1.13.1)
14
+ mustermann (1.0.3)
15
+ rack (2.0.7)
16
+ rack-protection (2.0.5)
17
+ rack
18
+ rake (10.5.0)
19
+ rspec (3.8.0)
20
+ rspec-core (~> 3.8.0)
21
+ rspec-expectations (~> 3.8.0)
22
+ rspec-mocks (~> 3.8.0)
23
+ rspec-core (3.8.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-expectations (3.8.3)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-mocks (3.8.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.8.0)
31
+ rspec-support (3.8.0)
32
+ sinatra (2.0.5)
33
+ mustermann (~> 1.0)
34
+ rack (~> 2.0)
35
+ rack-protection (= 2.0.5)
36
+ tilt (~> 2.0)
37
+ sinatra-contrib (2.0.5)
38
+ backports (>= 2.8.2)
39
+ multi_json
40
+ mustermann (~> 1.0)
41
+ rack-protection (= 2.0.5)
42
+ sinatra (= 2.0.5)
43
+ tilt (>= 1.3, < 3)
44
+ tilt (2.0.9)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (~> 1.16)
51
+ puuko!
52
+ rake (~> 10.0)
53
+ rspec (~> 3.0)
54
+
55
+ BUNDLED WITH
56
+ 1.16.5
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Sergey Tsvetkov
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.
@@ -0,0 +1,39 @@
1
+ # Puuko
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/puuko`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'puuko'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install puuko
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` 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]/puuko.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "puuko"
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require "ostruct"
2
+
3
+ module Puuko
4
+ end
5
+
6
+ require_relative "puuko/version"
7
+ require_relative "puuko/endpoint"
8
+ require_relative "puuko/router"
9
+ require_relative "puuko/application"
10
+ require_relative "puuko/configuration"
11
+
@@ -0,0 +1,97 @@
1
+ require "json"
2
+
3
+ require_relative "base"
4
+ require_relative "endpoints/not_found"
5
+
6
+ module Puuko
7
+ Route = Struct.new(:verb, :path, :endpoint, :params)
8
+
9
+ class Application
10
+ class << self
11
+ def configure(klass = nil)
12
+ @configuration = klass.new if klass
13
+ yield @configuration if block_given?
14
+ @configuration
15
+ end
16
+
17
+ def logger
18
+ @configuration.logger
19
+ end
20
+
21
+ def configuration
22
+ @configuration
23
+ end
24
+ end
25
+
26
+ def initialize
27
+ @routes = []
28
+ end
29
+
30
+ def get(path, to: Puuko::Endpoints::NotFound, params: {})
31
+ @routes << Puuko::Route.new(:get, path, to, params)
32
+ end
33
+
34
+ def post(path, to: Puuko::Endpoints::NotFound, params: {})
35
+ @routes << Puuko::Route.new(:post, path, to, params)
36
+ end
37
+
38
+ def put(path, to: Puuko::Endpoints::NotFound, params: {})
39
+ @routes << Puuko::Route.new(:put, path, to, params)
40
+ end
41
+
42
+ def patch(path, to: Puuko::Endpoints::NotFound, params: {})
43
+ @routes << Puuko::Route.new(:patch, path, to, params)
44
+ end
45
+
46
+ def delete(path, to: Puuko::Endpoints::NotFound, params: {})
47
+ @routes << Puuko::Route.new(:delete, path, to, params)
48
+ end
49
+
50
+ def configuration
51
+ self.class.configuration
52
+ end
53
+
54
+ def logger
55
+ self.class.logger
56
+ end
57
+
58
+ def router=(router)
59
+ router.apply(self)
60
+ end
61
+
62
+ def rack_app
63
+ klass = Class.new(Puuko::Base) do
64
+ set :protection, except: :json_csrf
65
+
66
+ configure :production, :development do
67
+ enable :logging
68
+ end
69
+ end
70
+
71
+ configuration.before_filters.each do |before_filter|
72
+ klass.before(&before_filter)
73
+ end
74
+
75
+ @routes.each do |route|
76
+ logger.debug "Register #{route.verb} #{route.path} to #{route.endpoint.name}"
77
+
78
+ klass.public_send(route.verb, route.path) do
79
+ handler = route.endpoint.new(request, route.params.merge(params))
80
+ handler.execute
81
+
82
+ [
83
+ handler.response.status,
84
+ handler.response.headers,
85
+ handler.response.body.to_json
86
+ ]
87
+ end
88
+ end
89
+
90
+ configuration.after_filters.each do |after_filter|
91
+ klass.after(&after_filter)
92
+ end
93
+
94
+ klass
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,6 @@
1
+ require "sinatra/base"
2
+
3
+ module Puuko
4
+ class Base < Sinatra::Base
5
+ end
6
+ end
@@ -0,0 +1,22 @@
1
+ module Puuko
2
+ class Configuration
3
+ attr_accessor :logger
4
+ attr_accessor :session_secret
5
+
6
+ attr_reader :before_filters
7
+ attr_reader :after_filters
8
+
9
+ def initialize
10
+ @before_filters = []
11
+ @after_filters = []
12
+ end
13
+
14
+ def before(&block)
15
+ @before_filters << block
16
+ end
17
+
18
+ def after(&block)
19
+ @after_filters << block
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,123 @@
1
+ module Puuko
2
+ Response = Struct.new(:status, :headers, :body)
3
+
4
+ class Endpoint
5
+ class InvalidCallbackKind < StandardError; end
6
+
7
+ CALLBACK_KINDS = [ :before, :after, :rescue ]
8
+
9
+ DEFAULT_HEADERS = {
10
+ "Content-Type" => "application/json"
11
+ }
12
+
13
+ class << self
14
+ def registered_callbacks
15
+ @registered_callbacks ||= begin
16
+ hash = {}
17
+ CALLBACK_KINDS.each do |kind|
18
+ hash[kind] = []
19
+ if self.superclass.respond_to?(:registered_callbacks)
20
+ superclass.registered_callbacks[kind].each { |cb| hash[kind] << cb }
21
+ end
22
+ end
23
+ hash
24
+ end
25
+
26
+ Hash[@registered_callbacks]
27
+ end
28
+
29
+ def register_callback(kind, &block)
30
+ unless Puuko::Endpoint::CALLBACK_KINDS.include?(kind)
31
+ raise InvalidCallbackKind, "Received invalid callback kind: #{kind}"
32
+ end
33
+
34
+ registered_callbacks[kind] << block if block_given?
35
+ end
36
+
37
+ def register_rescue_callback(exception_class, &block)
38
+ register_callback(:rescue) do |e|
39
+ self.instance_exec(&block) if e.is_a?(exception_class)
40
+ end
41
+ end
42
+
43
+ def register_before_callback(&block)
44
+ register_callback(:before, &block)
45
+ end
46
+
47
+ def register_after_callback(&block)
48
+ register_callback(:after, &block)
49
+ end
50
+ end
51
+
52
+ attr_reader :request
53
+ attr_reader :params
54
+ attr_reader :response
55
+
56
+ def initialize(request, params)
57
+ @request = request
58
+ @params = params
59
+ @response = Puuko::Response.new(500, DEFAULT_HEADERS, {})
60
+ @halted = false
61
+ end
62
+
63
+ def execute
64
+ result = nil
65
+
66
+ begin
67
+ execute_callbacks(:before)
68
+ result = handle unless halted?
69
+ execute_callbacks(:after)
70
+ rescue RuntimeError, StandardError => e
71
+ execute_callbacks(:rescue, e)
72
+ raise e unless halted?
73
+ end
74
+
75
+ result
76
+ end
77
+
78
+ def body
79
+ @json ||= read_request_body do |data|
80
+ JSON.parse(data, symbolize_names: false)
81
+ rescue JSON::ParserError
82
+ {}
83
+ end
84
+ end
85
+
86
+ protected def halt!(status: 200, headers: DEFAULT_HEADERS, body: {})
87
+ @response = Puuko::Response.new(status, DEFAULT_HEADERS.merge(headers), body)
88
+ @halted = true
89
+ end
90
+
91
+ protected def halted?
92
+ @halted
93
+ end
94
+
95
+ protected def redirect_to(location)
96
+ render status: 302, headers: { "Location" => location }
97
+ end
98
+
99
+ protected def render(status: 200, headers: DEFAULT_HEADERS, body: {})
100
+ @response = Puuko::Response.new(status, DEFAULT_HEADERS.merge(headers), body)
101
+ end
102
+
103
+ private def execute_callbacks(kind, *args)
104
+ return if halted?
105
+
106
+ unless Puuko::Endpoint::CALLBACK_KINDS.include?(kind)
107
+ raise InvalidCallbackKind, "Received invalid callback kind: #{kind}"
108
+ end
109
+
110
+ self.class.registered_callbacks[kind].each do |callback|
111
+ self.instance_exec(*args, &callback)
112
+ break if halted?
113
+ end
114
+ end
115
+
116
+ private def read_request_body
117
+ request.body.rewind # in case someone already read it
118
+ result = yield(request.body.read)
119
+ request.body.rewind # to allow others read it if they want
120
+ result
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,9 @@
1
+ module Puuko
2
+ module Endpoints
3
+ class NotFound < Puuko::Endpoint
4
+ def handle
5
+ render status: 404
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ module Puuko
2
+ def self.router(&block)
3
+ router_class = Class.new(Puuko::Router)
4
+ router_class.routing = block
5
+ router_class
6
+ end
7
+
8
+ class Router
9
+ class << self
10
+ def routing
11
+ @routing
12
+ end
13
+
14
+ def routing=(block)
15
+ @routing = block
16
+ end
17
+ end
18
+
19
+ def apply(application)
20
+ application.instance_eval(&self.class.routing)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Puuko
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "puuko/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "puuko"
8
+ spec.version = Puuko::VERSION
9
+ spec.authors = ["Sergey Tsvetkov"]
10
+ spec.email = ["sergey.a.tsvetkov@gmail.com"]
11
+
12
+ spec.summary = "Everything I would like to take with me into the next journey through Web with Ruby"
13
+ spec.description = "This library is a set of things collected to simplify development of HTTP microservices using Ruby"
14
+ spec.homepage = "https://github.com/kimrgrey/puuko"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| 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.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+
28
+ spec.add_dependency "sinatra", "~> 2.0.5"
29
+ spec.add_dependency "sinatra-contrib", "~> 2.0.5"
30
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puuko
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Tsvetkov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-06 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: 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
+ - !ruby/object:Gem::Dependency
56
+ name: sinatra
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.5
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.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra-contrib
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.5
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.5
83
+ description: This library is a set of things collected to simplify development of
84
+ HTTP microservices using Ruby
85
+ email:
86
+ - sergey.a.tsvetkov@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/puuko.rb
102
+ - lib/puuko/application.rb
103
+ - lib/puuko/base.rb
104
+ - lib/puuko/configuration.rb
105
+ - lib/puuko/endpoint.rb
106
+ - lib/puuko/endpoints/not_found.rb
107
+ - lib/puuko/router.rb
108
+ - lib/puuko/version.rb
109
+ - puuko.gemspec
110
+ homepage: https://github.com/kimrgrey/puuko
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.7.6
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Everything I would like to take with me into the next journey through Web
134
+ with Ruby
135
+ test_files: []