newark 0.0.1

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: b0754c654539db2a8f0404255dbe15e50dc553b4
4
+ data.tar.gz: 80d3faae39da584b6531678b57536db27bce9ae6
5
+ SHA512:
6
+ metadata.gz: 703ea8e2e0708fba0baa997c8c9c7a1cd536a2bde142eecfeba8883921500de2011ee6b27299248c7167104e1816bd26c3db2ca3e861a7f777bb656c58e98f3b
7
+ data.tar.gz: f54c6f0811fe80b3a10b304c018a006132603e71ccabe6306cb7e84f7175814beb17dbfa7109dd25c290defcf551cfd39810e4d6e5e5a03a70d071b0b0a27366
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in newark.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mike Evans
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,46 @@
1
+ # Newark
2
+
3
+ A Pico Web Framework
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'newark'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install newark
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'newark'
23
+
24
+ class App
25
+
26
+ include Newark
27
+
28
+ before do
29
+ headers['X-Newark-Version'] = Newark::VERSION
30
+ end
31
+
32
+ get '/api/:fu/:bar' do
33
+ "#{params[:fu]}:#{params[:bar]}"
34
+ end
35
+ end
36
+
37
+ run Application.new
38
+ ```
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it ( http://github.com/<my-github-username>/newark/fork )
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.warning = true
8
+ test.verbose = true
9
+ end
10
+
11
+ task :default => :test
data/lib/newark/app.rb ADDED
@@ -0,0 +1,53 @@
1
+ # Base class that represents your Rack app
2
+ module Newark
3
+ module App
4
+
5
+ def self.included(klass)
6
+ klass.instance_variable_set :@routes, []
7
+ klass.instance_variable_set :@before_hooks, []
8
+ klass.instance_variable_set :@after_hooks, []
9
+ klass.extend ClassMethods
10
+ end
11
+
12
+ HTTP_VERBS = [ :delete, :get, :head, :options,
13
+ :patch, :post, :put, :trace ].freeze
14
+
15
+ module ClassMethods
16
+
17
+ HTTP_VERBS.each do |verb|
18
+ define_method verb do |path, options = {}, &block|
19
+ options.merge!(request_method: verb.to_s.upcase)
20
+ define_route(path, options, &block)
21
+ end
22
+ end
23
+
24
+ def define_route(path, options, &block)
25
+ @routes << Route.new(path, options, block)
26
+ end
27
+
28
+ def before(&block)
29
+ @before_hooks << block
30
+ end
31
+
32
+ def after(&block)
33
+ @after_hooks << block
34
+ end
35
+ end
36
+
37
+ def call(env)
38
+ Router.new(self, env).route!
39
+ end
40
+
41
+ def routes
42
+ self.class.instance_variable_get :@routes
43
+ end
44
+
45
+ def before_hooks
46
+ self.class.instance_variable_get :@before_hooks
47
+ end
48
+
49
+ def after_hooks
50
+ self.class.instance_variable_get :@after_hooks
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,32 @@
1
+ require 'active_support/hash_with_indifferent_access'
2
+
3
+ module Newark
4
+ class Request < Rack::Request
5
+
6
+ def initialize(*)
7
+ super
8
+ @body = @env["rack.input"].clone.read
9
+ @params = ActiveSupport::HashWithIndifferentAccess.new(params)
10
+ @headers = original_headers
11
+ end
12
+
13
+ def uri
14
+ URI(base_url + fullpath)
15
+ end
16
+
17
+ def body
18
+ @body
19
+ end
20
+
21
+ protected
22
+
23
+ def original_headers
24
+ {}.tap do |headers|
25
+ env.select { |k, v| k.start_with?('HTTP_') }.each_pair do |k, v|
26
+ header = k.sub(/^HTTP_/, '').split('_').map(&:capitalize).join('-')
27
+ headers[header] = v
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,14 @@
1
+ module Newark
2
+ class Response < Rack::Response
3
+
4
+ def body=(value)
5
+ value = if value.respond_to?(:to_str)
6
+ [ value.to_str ]
7
+ else
8
+ [ value ]
9
+ end
10
+
11
+ super value
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,100 @@
1
+ module Newark
2
+ class Route
3
+
4
+ PARAM_MATCHER = /:(?<param>[^\/]*)/.freeze
5
+
6
+ attr_reader :handler, :params
7
+
8
+ def initialize(path, constraints, handler)
9
+ @constraints = Constraint.load(constraints)
10
+ @handler = handler
11
+ @path = path_matcher(path)
12
+ @params = nil
13
+ end
14
+
15
+ def match?(request)
16
+ path_data = path_match?(request)
17
+ if path_data && constraints_match?(request)
18
+ @params = Hash[ path_data.names.zip( path_data.captures ) ]
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def constraints_match?(request)
25
+ @constraints.all? { |constraint| constraint.match?(request) }
26
+ end
27
+
28
+ def path_match?(request)
29
+ @path.match(request.path)
30
+ end
31
+
32
+ def path_matcher(path)
33
+ return path if path.is_a? Regexp
34
+ /^#{path_params(path.to_s)}$/
35
+ end
36
+
37
+ def path_params(path)
38
+ return path unless path =~ /:/
39
+
40
+ while match = PARAM_MATCHER.match(path)
41
+ path.sub!(/:[^\/]*/, "(?<#{match[:param]}>[^\/]*)")
42
+ end
43
+ path
44
+ end
45
+
46
+ class Constraint
47
+
48
+ attr_reader :field, :matchers
49
+
50
+ # Expects a hash of constraints
51
+ def self.load(constraints)
52
+ fail ArgumentError unless constraints.is_a?(Hash)
53
+ constraints.map { |field, matcher| Constraint.new(field, matcher) }
54
+ end
55
+
56
+ def initialize(field, match_or_matchers)
57
+ @field = field
58
+ @matchers = make_matchers_regexp(match_or_matchers)
59
+ end
60
+
61
+ def match?(request)
62
+ if request.respond_to?(field)
63
+ constrained = request.send(field)
64
+
65
+ if matchers.is_a?(Hash) && constrained.is_a?(Hash)
66
+ hash_match?(request, constrained, matchers)
67
+ else
68
+ constrained =~ matchers
69
+ end
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def hash_match?(request, constrained, matchers)
76
+ matchers.all? { |key, matcher|
77
+ constrained[key] =~ matcher
78
+ }
79
+ end
80
+
81
+ def make_matchers_regexp(match_or_matchers)
82
+ if match_or_matchers.is_a? Hash
83
+ {}.tap do |matchers|
84
+ match_or_matchers.each do |k, v|
85
+ matchers[k] = matcher_to_regex(v)
86
+ end
87
+ end
88
+ else
89
+ matcher_to_regex(match_or_matchers)
90
+ end
91
+ end
92
+
93
+ def matcher_to_regex(matcher)
94
+ return matcher if matcher.is_a? Regexp
95
+ /^#{matcher.to_s}$/
96
+ end
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,57 @@
1
+ module Newark
2
+ class Router
3
+
4
+ FOUR_O_FOUR = [404, {}, []].freeze
5
+
6
+ attr_accessor :request, :response
7
+
8
+ def initialize(app, env)
9
+ @app = app
10
+ @env = env
11
+ @request = Request.new(env)
12
+ @response = Response.new
13
+ end
14
+
15
+ def route!
16
+ exec_before_hooks
17
+ route = matched_route(@app.routes)
18
+ if route
19
+ request.params.merge!(route.params)
20
+ response.body = instance_exec(&route.handler)
21
+ exec_after_hooks
22
+ response.finish
23
+ else
24
+ FOUR_O_FOUR
25
+ end
26
+ end
27
+
28
+ def matched_route(routes)
29
+ routes.find { |route| route.match?(request) }
30
+ end
31
+
32
+ def headers
33
+ response.headers
34
+ end
35
+
36
+ def params
37
+ request.params
38
+ end
39
+
40
+ private
41
+
42
+ def exec_before_hooks
43
+ exec_hooks @app.before_hooks
44
+ end
45
+
46
+ def exec_after_hooks
47
+ exec_hooks @app.after_hooks
48
+ end
49
+
50
+ def exec_hooks(hooks)
51
+ hooks.each do |hook|
52
+ instance_exec(&hook)
53
+ end
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module Newark
2
+ VERSION = '0.0.1'
3
+ end
data/lib/newark.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'rack'
2
+ require 'newark/version'
3
+ require 'newark/app'
4
+ require 'newark/route'
5
+ require 'newark/router'
6
+ require 'newark/request'
7
+ require 'newark/response'
8
+
9
+ module Newark
10
+
11
+ def self.included(klass)
12
+ klass.send :include, App
13
+ end
14
+
15
+ end
data/newark.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 'newark/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'newark'
8
+ spec.version = Newark::VERSION
9
+ spec.authors = ['Mike Evans']
10
+ spec.email = ['mike@urlgonomics.com']
11
+ spec.summary = %q{Pico Web Framework}
12
+ spec.description = %q{Because everyone should write their own framework.}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'rack', '>= 1.5.2'
22
+ spec.add_dependency 'activesupport'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.5'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rack-test'
27
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,7 @@
1
+ # require 'pry'
2
+ require 'newark'
3
+ require 'minitest/autorun'
4
+
5
+ class Minitest::Unit::TestCase
6
+
7
+ end
@@ -0,0 +1,107 @@
1
+ require 'helper'
2
+ require 'rack/test'
3
+
4
+ class App
5
+
6
+ include Newark
7
+
8
+ before do
9
+ headers['X-Newark-Version'] = Newark::VERSION
10
+ end
11
+
12
+ after do
13
+ headers['X-Newark-Done'] = 'true'
14
+ end
15
+
16
+ get '/', params: { user: 'frank' } do
17
+ 'hello frank'
18
+ end
19
+
20
+ get '/' do
21
+ 'hello'
22
+ end
23
+
24
+ get(/\/regexp/) do
25
+ 'regexp'
26
+ end
27
+
28
+ get '/create' do
29
+ 'whoops'
30
+ end
31
+
32
+ post '/create' do
33
+ 'created'
34
+ end
35
+
36
+ get '/request_and_response' do
37
+ request && response
38
+ headers && params
39
+ 'ok'
40
+ end
41
+
42
+ get '/variables/:a/:b' do
43
+ "#{params[:a]}:#{params[:b]}"
44
+ end
45
+
46
+ end
47
+
48
+ class TestRouter < Minitest::Unit::TestCase
49
+
50
+ include Rack::Test::Methods
51
+
52
+ def app
53
+ App.new
54
+ end
55
+
56
+ def test_gets_root
57
+ get '/'
58
+ assert last_response.ok?
59
+ assert_equal 'hello', last_response.body
60
+ end
61
+
62
+ def test_gets_root_with_param_constraint
63
+ get '/', user: 'frank'
64
+ assert last_response.ok?
65
+ assert_equal 'hello frank', last_response.body
66
+ end
67
+
68
+ def test_gets_404
69
+ get '/not_found'
70
+ refute last_response.ok?
71
+ assert last_response.not_found?
72
+ end
73
+
74
+ def test_gets_by_regexp
75
+ get '/regexp'
76
+ assert last_response.ok?
77
+ assert_equal 'regexp', last_response.body
78
+ end
79
+
80
+ def test_post
81
+ post '/create'
82
+ assert last_response.ok?
83
+ assert_equal 'created', last_response.body
84
+ end
85
+
86
+ def test_has_access_to_request_and_response
87
+ get '/request_and_response'
88
+ assert last_response.ok?
89
+ assert_equal 'ok', last_response.body
90
+ end
91
+
92
+ def test_before_hook
93
+ get '/'
94
+ assert_equal Newark::VERSION, last_response.header['X-Newark-Version']
95
+ end
96
+
97
+ def test_after_hook
98
+ get '/'
99
+ assert_equal 'true', last_response.header['X-Newark-Done']
100
+ end
101
+
102
+ def test_variable_globbing
103
+ get '/variables/fu/bar'
104
+ assert last_response.ok?
105
+ assert_equal 'fu:bar', last_response.body
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: newark
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Because everyone should write their own framework.
84
+ email:
85
+ - mike@urlgonomics.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/newark.rb
96
+ - lib/newark/app.rb
97
+ - lib/newark/request.rb
98
+ - lib/newark/response.rb
99
+ - lib/newark/route.rb
100
+ - lib/newark/router.rb
101
+ - lib/newark/version.rb
102
+ - newark.gemspec
103
+ - test/helper.rb
104
+ - test/test_router.rb
105
+ homepage: ''
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Pico Web Framework
129
+ test_files:
130
+ - test/helper.rb
131
+ - test/test_router.rb