rack-app 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 32f29dd3c7583a4e67a8d7d120490ab60d8525aa
4
+ data.tar.gz: 7b038445705c0b9f610b0b1d43eef0238ddd18b1
5
+ SHA512:
6
+ metadata.gz: 6ed668e4f0f109ed99554c18f99d2e0a73869498d0e4632d9ba63817599c0fa33e2e5a81f914b6563338621f68dbc468d430c33b5ef957d67378d9111db1faae
7
+ data.tar.gz: e2c2d8b39453daaeaaf3646b56bb41879372f0caad64c578ce54c388e20d26b7312e7e7c358d78546be41c6c0ef8f0f734c014dbf811166c1b4114bf5b41f775
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /pkg/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ gem 'grape'
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Rack::App
2
+
3
+ Super bare bone Rack::App for writing minimalist/masochist rack apps
4
+
5
+ The idea behind is simple.
6
+ Have a little framework that can allow you write pure rack apps,
7
+ that will do nothing more than what you defined.
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'rack-app'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install rack-app
25
+
26
+ ## Usage
27
+
28
+ config.ru
29
+ ```ruby
30
+
31
+ require 'rack/app'
32
+
33
+ class YourAwesomeApp < Rack::APP
34
+
35
+ get '/hello' do
36
+ 'Hello World!'
37
+ end
38
+
39
+ get '/nope' do
40
+ request.env
41
+ response.write 'some response body'
42
+
43
+ end
44
+
45
+ post '/lol_post' do
46
+ status 500
47
+ 'LOL'
48
+ end
49
+
50
+ end
51
+
52
+ run YourAwesomeApp
53
+ ```
54
+
55
+ ## TODO
56
+
57
+ * benchmark for rails, padrino, sinatra, grape etc to prove awesomeness
58
+ * more verbose readme
59
+ * drink less coffee
60
+
61
+ ## Development
62
+
63
+ 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.
64
+
65
+ 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).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rack-app. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
70
+
data/Rakefile ADDED
@@ -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
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/example/config.ru ADDED
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__),'..','lib'))
2
+ require 'rack/app'
3
+
4
+ require_relative 'first_controller'
5
+
6
+ class SampleApp < Rack::APP
7
+
8
+ mount FirstController
9
+
10
+ get '/hello' do
11
+ 'Hello World!'
12
+ end
13
+
14
+ get '/nope' do
15
+ response.write 'nope nope nope...'
16
+
17
+ end
18
+
19
+ end
20
+
21
+ run SampleApp
@@ -0,0 +1,11 @@
1
+ class FirstController < Rack::APP
2
+
3
+ get '/first' do
4
+ first
5
+ end
6
+
7
+ def first
8
+ 'some text'
9
+ end
10
+
11
+ end
data/lib/rack/app.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'rack'
2
+ require 'rack/request'
3
+ require 'rack/response'
4
+ class Rack::APP
5
+
6
+ require 'rack/app/version'
7
+
8
+ require 'rack/app/endpoint'
9
+ require 'rack/app/endpoint/not_found'
10
+
11
+ require 'rack/app/runner'
12
+
13
+ require 'rack/app/syntax_sugar'
14
+ extend Rack::APP::SyntaxSugar
15
+
16
+ require 'rack/app/request_helpers'
17
+
18
+ include Rack::APP::RequestHelpers
19
+
20
+ attr_reader :request, :response
21
+
22
+ def initialize(request, response)
23
+ @response = response
24
+ @request = request
25
+ end
26
+
27
+ def self.call(request_env)
28
+ Rack::APP::Runner.response_for(self,request_env)
29
+ end
30
+
31
+ end
@@ -0,0 +1,37 @@
1
+ class Rack::APP::Endpoint
2
+
3
+ def initialize(api_class, properties={}, &logic_block)
4
+ @properties = properties
5
+ @logic_block = logic_block
6
+ @api_class = api_class
7
+ end
8
+
9
+ def execute(request_env)
10
+
11
+ request = Rack::Request.new(request_env)
12
+ response = Rack::Response.new
13
+
14
+ request_handler = @api_class.new(request, response)
15
+ call_return = request_handler.instance_exec(&@logic_block)
16
+
17
+ return call_return if is_a_rack_response_finish?(call_return)
18
+ add_response_body_if_missing(call_return, response)
19
+
20
+ return response.finish
21
+
22
+ end
23
+
24
+ protected
25
+
26
+ def add_response_body_if_missing(call_return, response)
27
+ response.write(call_return.to_s) if response.body.empty?
28
+ end
29
+
30
+ def is_a_rack_response_finish?(call_return)
31
+ call_return.is_a?(Array) and
32
+ call_return.length == 3 and
33
+ call_return[0].is_a?(Integer) and
34
+ call_return[1].is_a?(Hash)
35
+ end
36
+
37
+ end
@@ -0,0 +1,6 @@
1
+ api_class = Class.new(Rack::APP)
2
+ Rack::APP::Endpoint::NOT_FOUND = Rack::APP::Endpoint.new(api_class) do
3
+ response.status= 404
4
+ response.write '404 Not Found'
5
+ response.finish
6
+ end
@@ -0,0 +1,22 @@
1
+ require 'cgi'
2
+ module Rack::APP::RequestHelpers
3
+
4
+ def params
5
+ @__request_params__ ||= CGI.parse(request.env['QUERY_STRING']).freeze.reduce({}) do |params_collection, (k, v)|
6
+ if v.is_a?(Array) and v.length === 1
7
+ params_collection[k]= v[0]
8
+ else
9
+ params_collection[k]= v
10
+ end
11
+
12
+ params_collection
13
+ end
14
+ end
15
+
16
+ def status(new_status=nil)
17
+ response.status= new_status.to_i unless new_status.nil?
18
+
19
+ response.status
20
+ end
21
+
22
+ end
@@ -0,0 +1,15 @@
1
+ module Rack::APP::Runner
2
+ extend self
3
+
4
+ def response_for(api_class, request_env)
5
+ endpoint = fetch_endpoint(api_class,request_env['REQUEST_METHOD'],request_env['REQUEST_PATH'])
6
+ endpoint.execute(request_env)
7
+ end
8
+
9
+ protected
10
+
11
+ def fetch_endpoint(api_class, request_method, request_path)
12
+ api_class.endpoints[[request_method, request_path]] || Rack::APP::Endpoint::NOT_FOUND
13
+ end
14
+
15
+ end
@@ -0,0 +1,67 @@
1
+ module Rack::APP::SyntaxSugar
2
+
3
+ def description(*description_texts)
4
+ @last_description = description_texts.join("\n")
5
+ end
6
+
7
+ def get(path = '/', &block)
8
+ add_route('GET', path, &block)
9
+ end
10
+
11
+ def post(path = '/', &block)
12
+ add_route('POST', path, &block)
13
+ end
14
+
15
+ def put(path = '/', &block)
16
+ add_route('PUT', path, &block)
17
+ end
18
+
19
+ def head(path = '/', &block)
20
+ add_route('HEAD', path, &block)
21
+ end
22
+
23
+ def delete(path = '/', &block)
24
+ add_route('DELETE', path, &block)
25
+ end
26
+
27
+ def options(path = '/', &block)
28
+ add_route('OPTIONS', path, &block)
29
+ end
30
+
31
+ def patch(path = '/', &block)
32
+ add_route('PATCH', path, &block)
33
+ end
34
+
35
+ def add_route(request_method, request_path, &block)
36
+ request_key = [request_method, request_path]
37
+
38
+ endpoint = endpoints[request_key]= Rack::APP::Endpoint.new(
39
+ self,
40
+ {
41
+ request_method: request_method,
42
+ request_path: request_path,
43
+ description: @last_description
44
+ },
45
+ &block
46
+ )
47
+
48
+ @last_description = nil
49
+ endpoint
50
+ end
51
+
52
+ def endpoints
53
+ @endpoints ||= {}
54
+ end
55
+
56
+ def mount(api_class)
57
+
58
+ unless api_class.is_a?(Class) and api_class <= Rack::APP
59
+ raise(ArgumentError, 'Invalid class given for mount, must be a Rack::APP')
60
+ end
61
+
62
+ endpoints.merge!(api_class.endpoints)
63
+
64
+ nil
65
+ end
66
+
67
+ end
@@ -0,0 +1,3 @@
1
+ Rack ||= Module.new
2
+ Rack::APP ||= Class.new
3
+ Rack::APP::VERSION = File.read(File.join(File.dirname(__FILE__), '..', '..', '..', 'VERSION')).strip
data/rack-app.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/app/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+
8
+ spec.name = "rack-app"
9
+ spec.version = Rack::APP::VERSION
10
+ spec.authors = ["Adam Luzsi"]
11
+ spec.email = ["adamluzsi@gmail.com"]
12
+
13
+ spec.summary = %q{Bare bone minimalistic (masochistic) pico framework for building rack apps}
14
+ spec.description = %q{Bare bone minimalistic (masochistic) pico framework for building rack apps}
15
+ spec.homepage = "https://github.com/adamluzsi/rack-app.rb"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+
26
+ spec.add_dependency "rack"
27
+
28
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-app
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Luzsi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-03 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '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: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Bare bone minimalistic (masochistic) pico framework for building rack
70
+ apps
71
+ email:
72
+ - adamluzsi@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - VERSION
84
+ - bin/setup
85
+ - example/config.ru
86
+ - example/first_controller.rb
87
+ - lib/rack/app.rb
88
+ - lib/rack/app/endpoint.rb
89
+ - lib/rack/app/endpoint/not_found.rb
90
+ - lib/rack/app/request_helpers.rb
91
+ - lib/rack/app/runner.rb
92
+ - lib/rack/app/syntax_sugar.rb
93
+ - lib/rack/app/version.rb
94
+ - rack-app.gemspec
95
+ homepage: https://github.com/adamluzsi/rack-app.rb
96
+ licenses: []
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.2.2
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Bare bone minimalistic (masochistic) pico framework for building rack apps
118
+ test_files: []