busker 0.4.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 129bb0b9aea148dfb5c808d94dc6ebb3af56199f
4
+ data.tar.gz: 09a568f551d8acfbe486539c31803981f5906b8a
5
+ SHA512:
6
+ metadata.gz: a206b4814b6e6855b212c3edee57ec77b6005019b7d1c5f31555fd7c19d52d1833ccd45566990b176e22252d01cb83456b7b7d73ab719aafa57a6ad60d130d77
7
+ data.tar.gz: 53a951d8510e1381be474c58a2f0732d490a3a894d6f6f5509dc577e62c277da2fb710d9747c7add770f976afcc923036d756da426a22f3f61ae14e16f8e0766
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 pachacamac
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # Busker
2
+
3
+ An extremely simple web framework. It's called Busker as a reference to
4
+ Sinatra. It mimics Sinatra in some aspects while still trying to stay a
5
+ true wanderer of the streets.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'busker'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install busker
20
+
21
+ ## Usage
22
+
23
+ require 'busker'
24
+
25
+ Busker::Busker.new do
26
+ # minimal route definition
27
+ route '/' do
28
+ "Busker version: #{Busker::VERSION}"
29
+ end
30
+
31
+ # respond to multiple HTTP methods, overwrite response content_type
32
+ route '/info', [:GET, :POST, :PUT, :DELETE] do |params, request, response|
33
+ response.content_type = 'text/plain'
34
+ request.inspect
35
+ end
36
+
37
+ # usage of URL params, render template with variable
38
+ route '/template', :GET do |params|
39
+ @title = params[:title] || 'no title'
40
+ if params[:external]
41
+ render './template.erb'
42
+ else
43
+ render :template
44
+ end
45
+ end
46
+
47
+ # usage of dynamic route params
48
+ route '/item/:id' do |params|
49
+ "requested item with id: #{params[:id]}"
50
+ end
51
+
52
+ # list all defined routes
53
+ route '/routes' do |params, request, response|
54
+ response.content_type = 'text/plain'
55
+ @_[:routes].keys.map{|e| e.join("\n")}.join("\n\n")
56
+ end
57
+ end.start
58
+
59
+ __END__
60
+ @@ template
61
+ <h1><%= @title %></h1>
62
+
63
+ ## Design principles
64
+
65
+ * Small code base that is easy to understandable and hackable
66
+ * Such a tiny code base that you can just copy it into your one-file-project without the need to require a Gem
67
+ * No dependencies except what is in the Ruby Standard Lib
68
+ * Backward compatibility to older Ruby versions
69
+ * Some minor resemblance to Sinatra
70
+ * Ease of use
71
+ * It's not meant as a complete web framework like Rails but concentrates on the basics
72
+
73
+ ## TODO / Ideas
74
+
75
+ * Improve render method, allow yield etc
76
+ * Improve error handling, honor production/development environment?
77
+ * Auto reload?
78
+ * Anything cool that doesn't break the design principles ...
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create a new Pull Request
87
+
88
+ Or just use GitHubs on page editing ...
89
+ it will do all of the above for you and is reasonable given the size of the source.
90
+ Make sure to add an explanation though!
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/busker.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'busker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "busker"
8
+ spec.version = Busker::VERSION
9
+ spec.authors = ["pachacamac"]
10
+ spec.email = ["pachacamac@users.noreply.github.com"]
11
+ spec.summary = %q{An extremely simple web framework}
12
+ spec.description = %q{An extremely simple web framework. It mimics Sinatra in some aspects while still trying to stay a true wanderer of the streets.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ end
data/lib/busker.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'busker/version'
2
+ require 'webrick'
3
+ require 'cgi'
4
+ require 'erb'
5
+
6
+ module Busker
7
+ class Busker
8
+ def initialize(opts={}, &block)
9
+ @_ = {:routes => {}}
10
+ (block.arity < 1 ? instance_eval(&block) : block.call(self)) if block_given?
11
+ opts[:Port] ||= opts.delete(:port) || 8080
12
+ opts[:DocumentRoot] ||= opts.delete(:document_root) || File.expand_path('./')
13
+ @_[:server] = WEBrick::HTTPServer.new(opts)
14
+ @_[:server].mount_proc '' do |rq, rs| #request, response
15
+ begin
16
+ rs.status, rs.content_type, method = nil, 'text/html', rq.request_method.tr('-', '_').upcase
17
+ route, handler = @_[:routes].find{|k,v| k.first.include?(method) && k.last.match(rq.path_info)}
18
+ params = Hash[ CGI::parse(rq.query_string||'').map{|k,v| [k.to_sym,v[0]]} + #url params
19
+ ($~ ? $~.names.map(&:to_sym).zip($~.captures) : []) ] #dynamic route params
20
+ rs.status, rs.body = route ? [rs.status || 200, handler[:block].call(params, rq, rs)] : [404, 'not found']
21
+ rescue => e
22
+ @_[:server].logger.error "#{e.message}\n#{e.backtrace.map{|line| "\t#{line}"}.join("\n")}"
23
+ rs.status, rs.body = 500, "#{e}"
24
+ end
25
+ end
26
+ end
27
+
28
+ def route(path, methods = ['GET'], opts={}, &block)
29
+ methods = (methods.is_a?(Array) ? methods : [methods]).map{|e| e.to_s.tr('-', '_').upcase}
30
+ matcher = Regexp.new("\\A#{path.gsub(/(:\w+)/){|m| "(?<#{$1[1..-1]}>\\w+)"}}\\Z")
31
+ @_[:routes][[methods, path, matcher]] = {:opts => opts, :block => block}
32
+ end
33
+
34
+ def render(name)
35
+ @_[:templates] ||= (Hash[DATA.read.split(/^@@\s*(.*\S)\s*$/)[1..-1].map(&:strip).each_slice(2).to_a] rescue {})
36
+ ERB.new(@_[:templates][name.to_s] || File.read(name)).result(binding)
37
+ end
38
+
39
+ def start
40
+ @_[:server].start ensure @_[:server].shutdown
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Busker
2
+ VERSION = "0.4.0"
3
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: busker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - pachacamac
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-11 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: An extremely simple web framework. It mimics Sinatra in some aspects
42
+ while still trying to stay a true wanderer of the streets.
43
+ email:
44
+ - pachacamac@users.noreply.github.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - busker.gemspec
55
+ - lib/busker.rb
56
+ - lib/busker/version.rb
57
+ homepage: ''
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.2
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: An extremely simple web framework
81
+ test_files: []