galago-router 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 12e8872da7d883013a2edaa73193d11f9ebe00a6
4
- data.tar.gz: 27e804be87b65711ab84cd2fe7c674f6cda45efb
3
+ metadata.gz: 24dc9a36ea2bca4e8d75e2722b5ae5a78010d19f
4
+ data.tar.gz: 39497251884dfba4e67bc8746029734b13a87642
5
5
  SHA512:
6
- metadata.gz: 05eadb6f0ad527e2a73d8087bc1a6c3724d894d5a75340b20118c4ac80e2b7e58c8536f1e41bf864b493e50967953284a618ce558acea7a27f3cd12ee3e9829c
7
- data.tar.gz: 920319b3d504dd72f4f5dea98ec94cca38a661798cc7219e24f113773d0a5e77e1a461329f0e842999930166508f784fcdfb250d580acd75729310cfa0b301f7
6
+ metadata.gz: 7cba7c52f27784cb3fe98ab0b199f8d88dfd911808bdeece4039d969fa44eec49feefc99f9b908505a5003268a58aa82780daea0a197b722dcbcf2791a372286
7
+ data.tar.gz: 41bf1fdb47a22e7cdd7a873c3f8209c5ceedabaa60387bf37e2f0eb1e9a229c028dc7065383b4fa3a9b1cebaf34f1a3bf3dd289e4111e442e299f938794f56ef
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ *.swp
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
data/README.md CHANGED
@@ -1,13 +1,12 @@
1
- # Galago
1
+ # Galago Router
2
2
 
3
- Galago is an API micro framework for Ruby.
4
- I am building it to gain more appreciation for existing tools.
3
+ A rack router.
5
4
 
6
5
  ## Installation
7
6
 
8
7
  Add this line to your application's Gemfile:
9
8
 
10
- gem 'galago'
9
+ gem 'galago-router'
11
10
 
12
11
  And then execute:
13
12
 
@@ -15,39 +14,24 @@ And then execute:
15
14
 
16
15
  Or install it yourself as:
17
16
 
18
- $ gem install galago
17
+ $ gem install galago-router
19
18
 
20
19
  ## Usage
21
20
 
22
21
  ```ruby
23
- module GitHub::Application < Galago::Application
24
- routes do
25
- get '/users' do
26
- users = User.all
27
- end
28
-
29
- post '/users' do
30
- user = User.create! params['user']
31
- end
32
-
33
- patch '/users/:name' do
34
- user = User.find_by_name params['name']
35
- user.update! params['user']
36
- end
37
-
38
- put '/users/:name' do
39
- user = User.find_by_name params['name']
40
- user.update! params['user']
41
- end
42
-
43
- delete '/users/:name' do
44
- user = User.find_by_name params['name']
45
- user.destroy!
46
- end
47
- end
22
+ # config.ru
23
+ require 'galago/router'
24
+ require 'rack/lobster'
25
+
26
+ router = Galago::Router.new do
27
+ get '/lobsters', to: Rack::Lobster.new
28
+ post '/lobsters', to: Rack::Lobster.new
29
+ patch '/lobsters/:name', to: Rack::Lobster.new
30
+ put '/lobsters/:name', to: Rack::Lobster.new
31
+ delete '/lobsters/:name', to: Rack::Lobster.new
48
32
  end
49
33
 
50
- run GitHub::Application
34
+ run router
51
35
  ```
52
36
 
53
37
  ## Contributing
@@ -0,0 +1,19 @@
1
+ require 'galago/router'
2
+
3
+ EchoParams = lambda { |env|
4
+ params = Rack::Request.new(env).params
5
+ Rack::Response.new(params).finish
6
+ }
7
+
8
+ router = Galago::Router.new do
9
+ get '/echo_params', to: EchoParams
10
+ get '/echo_params/:name', to: EchoParams
11
+
12
+ post '/echo_params', to: EchoParams
13
+ put '/echo_params/:name', to: EchoParams
14
+
15
+ delete '/echo_params', to: EchoParams
16
+ end
17
+
18
+ run router
19
+
@@ -0,0 +1,9 @@
1
+ require 'galago/router'
2
+ require 'rack/lobster'
3
+
4
+ router = Galago::Router.new do
5
+ get '/lobster', to: Rack::Lobster.new
6
+ end
7
+
8
+ run router
9
+
File without changes
@@ -1,28 +1,30 @@
1
1
  module Galago
2
- class Router::DSL
3
- def initialize(router, block)
4
- @router = router
5
- instance_eval(&block)
6
- end
2
+ class Router
3
+ class DSL
4
+ def initialize(router, block)
5
+ @router = router
6
+ instance_eval(&block)
7
+ end
7
8
 
8
- def get(path, options)
9
- @router.add_route("GET", path, options[:to])
10
- end
9
+ def get(path, options)
10
+ @router.add_route("GET", path, options[:to])
11
+ end
11
12
 
12
- def patch(path, options)
13
- @router.add_route("PATCH", path, options[:to])
14
- end
13
+ def patch(path, options)
14
+ @router.add_route("PATCH", path, options[:to])
15
+ end
15
16
 
16
- def post(path, options)
17
- @router.add_route("POST", path, options[:to])
18
- end
17
+ def post(path, options)
18
+ @router.add_route("POST", path, options[:to])
19
+ end
19
20
 
20
- def put(path, options)
21
- @router.add_route("PUT", path, options[:to])
22
- end
21
+ def put(path, options)
22
+ @router.add_route("PUT", path, options[:to])
23
+ end
23
24
 
24
- def delete(path, options)
25
- @router.add_route("DELETE", path, options[:to])
25
+ def delete(path, options)
26
+ @router.add_route("DELETE", path, options[:to])
27
+ end
26
28
  end
27
29
  end
28
30
  end
@@ -1,21 +1,23 @@
1
1
  module Galago
2
- class Router::ActionInvalid < ArgumentError
3
- def initialize(action)
4
- @action = action
5
- end
2
+ class Router
3
+ class ActionInvalid < ArgumentError
4
+ def initialize(action)
5
+ @action = action
6
+ end
6
7
 
7
- def message
8
- "Action does not have a call method: #{@action.inspect}"
8
+ def message
9
+ "Action does not have a call method: #{@action.inspect}"
10
+ end
9
11
  end
10
- end
11
12
 
12
- class Router::RequestMethodInvalid < ArgumentError
13
- def initialize(request_method)
14
- @request_method = request_method
15
- end
13
+ class RequestMethodInvalid < ArgumentError
14
+ def initialize(request_method)
15
+ @request_method = request_method
16
+ end
16
17
 
17
- def message
18
- "Got: #{@request_method}\nExpected one of: #{REQUEST_METHODS}"
18
+ def message
19
+ "Got: #{@request_method}\nExpected one of: #{REQUEST_METHODS}"
20
+ end
19
21
  end
20
22
  end
21
23
  end
@@ -1,44 +1,49 @@
1
1
  module Galago
2
- class Router::Path
2
+ class Router
3
+ class Path
3
4
 
4
- def initialize(path)
5
- @path = path.to_s
6
- end
5
+ def initialize(path)
6
+ @path = path.to_s
7
+ end
7
8
 
8
- def recognizes?(request_path)
9
- request_path =~ regex
10
- end
9
+ def recognizes?(request_path)
10
+ request_path =~ regex
11
+ end
11
12
 
12
- def named_parameters
13
- @path_parameters ||= @path.scan(/\:(\w+)/).flatten
14
- end
13
+ def named_parameters
14
+ @path_parameters ||= @path.scan(/\:(\w+)/).flatten
15
+ end
16
+
17
+ def add_path_params_to_env(env)
18
+ request = Rack::Request.new(env)
15
19
 
16
- def add_path_params_to_env(env)
17
- request = Rack::Request.new(env)
18
- identify_params_in_path(request.path).each do |key, value|
19
- request.update_param(key, value)
20
+ if path_params = identify_params_in_path(request.path)
21
+ path_params.each { |key, value| request.update_param(key, value) }
22
+ end
20
23
  end
21
- end
22
24
 
23
- def identify_params_in_path(request_path)
24
- values = regex.match(request_path).captures
25
- Hash[named_parameters.zip(values)]
26
- end
25
+ def to_s
26
+ @path
27
+ end
27
28
 
28
- def to_s
29
- @path
30
- end
29
+ private
31
30
 
32
- private
31
+ def regex
32
+ @regex_path ||= convert_path_to_regex(@path)
33
+ end
33
34
 
34
- def regex
35
- @regex_path ||= convert_path_to_regex(@path)
36
- end
35
+ def convert_path_to_regex(path)
36
+ regexp = path.to_s.gsub(/\:\w+/, '([\w-]+)')
37
+ Regexp.new("^#{regexp}$")
38
+ end
37
39
 
38
- def convert_path_to_regex(path)
39
- regexp = path.to_s.gsub(/\:\w+/, '([\w-]+)')
40
- Regexp.new("^#{regexp}$")
41
- end
40
+ def identify_params_in_path(path)
41
+ if match = regex.match(path)
42
+ named_parameters.zip(match.captures)
43
+ end
44
+ end
42
45
 
46
+ end
43
47
  end
44
48
  end
49
+
@@ -1,36 +1,39 @@
1
1
  module Galago
2
- class Router::Route
2
+ class Router
3
+ class Route
3
4
 
4
- attr_reader :request_method, :path, :action
5
+ attr_reader :request_method, :path, :action
5
6
 
6
- def initialize(request_method, path, action)
7
- @request_method = request_method.to_s.upcase
8
- @path = Router::Path.new(path)
9
- @action = action
7
+ def initialize(request_method, path, action)
8
+ @request_method = request_method.to_s.upcase
9
+ @path = Router::Path.new(path)
10
+ @action = action
10
11
 
11
- validate_action!
12
- validate_request_method!
13
- end
12
+ validate_action!
13
+ validate_request_method!
14
+ end
14
15
 
15
- def recognizes_path?(request_path)
16
- @path.recognizes?(request_path)
17
- end
16
+ def recognizes_path?(request_path)
17
+ @path.recognizes?(request_path)
18
+ end
18
19
 
19
- def call(env)
20
- @path.add_path_params_to_env(env)
21
- action.call(env)
22
- end
20
+ def call(env)
21
+ @path.add_path_params_to_env(env)
22
+ action.call(env)
23
+ end
23
24
 
24
- private
25
+ private
25
26
 
26
- def validate_action!
27
- action.respond_to?(:call) or raise Router::ActionInvalid.new(action)
28
- end
27
+ def validate_action!
28
+ action.respond_to?(:call) or raise Router::ActionInvalid.new(action)
29
+ end
29
30
 
30
- def validate_request_method!
31
- unless Router::REQUEST_METHODS.include?(request_method)
32
- raise Router::RequestMethodInvalid.new(request_method)
31
+ def validate_request_method!
32
+ unless Router::REQUEST_METHODS.include?(request_method)
33
+ raise Router::RequestMethodInvalid.new(request_method)
34
+ end
33
35
  end
34
36
  end
35
37
  end
36
38
  end
39
+
@@ -1,5 +1,5 @@
1
1
  module Galago
2
2
  class Router
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
data/lib/galago/router.rb CHANGED
@@ -1,22 +1,11 @@
1
+ require 'galago/router/dsl'
2
+ require 'galago/router/errors'
3
+ require 'galago/router/path'
4
+ require 'galago/router/route'
5
+ require 'galago/router/version'
6
+
1
7
  module Galago
2
8
  class Router
3
- require 'galago/router/dsl'
4
- require 'galago/router/errors'
5
- require 'galago/router/path'
6
- require 'galago/router/route'
7
- require 'galago/router/version'
8
-
9
- def self.call(env)
10
- router.process_request(env)
11
- end
12
-
13
- def self.routes(&block)
14
- Router::DSL.new(router, block)
15
- end
16
-
17
- def self.router
18
- @router ||= Router.new
19
- end
20
9
 
21
10
  REQUEST_METHODS = [
22
11
  "GET",
@@ -28,11 +17,13 @@ module Galago
28
17
 
29
18
  attr_reader :routes
30
19
 
31
- def initialize
20
+ def initialize(&block)
32
21
  @routes = REQUEST_METHODS.each_with_object({}) do |request_method, routes|
33
22
  routes[request_method] = []
34
23
  routes
35
24
  end
25
+
26
+ Router::DSL.new(self, block) if block_given?
36
27
  end
37
28
 
38
29
  def add_route(request_method, path, application)
@@ -44,7 +35,7 @@ module Galago
44
35
  find_route(request_method, path) ? true : false
45
36
  end
46
37
 
47
- def process_request(env)
38
+ def call(env)
48
39
  if route = find_route(env['REQUEST_METHOD'], env['PATH_INFO'])
49
40
  route.call(env)
50
41
  else
@@ -68,22 +68,5 @@ module Galago
68
68
  end
69
69
  end
70
70
 
71
- describe "#identify_params_in_path" do
72
- it "identifies each param and its value" do
73
- path = Router::Path.new('/users/:user_id/posts/:id')
74
- path_params = path.identify_params_in_path('/users/1/posts/2')
75
-
76
- expect(path_params).to eql({
77
- 'user_id' => '1',
78
- 'id' => '2'
79
- })
80
- end
81
-
82
- it "returns empty when no path params are present" do
83
- path = Router::Path.new('/users')
84
- expect(path.identify_params_in_path('/users')).to be_empty
85
- end
86
- end
87
-
88
71
  end
89
72
  end
@@ -2,6 +2,30 @@ require 'spec_helper'
2
2
 
3
3
  module Galago
4
4
  describe Router::Route do
5
+ context "#call" do
6
+ let(:env) do
7
+ { 'PATH_INFO' => '/foo' }
8
+ end
9
+
10
+ let(:action) do
11
+ lambda { |env| "foo" }
12
+ end
13
+
14
+ it "adds the path's params to the env" do
15
+ route = Router::Route.new('GET', '/foo', action)
16
+ expect(route.path).to receive(:add_path_params_to_env).with(env)
17
+
18
+ route.call(env)
19
+ end
20
+
21
+ it "calls the route's action" do
22
+ route = Router::Route.new('GET', '/bar', action)
23
+ expect(action).to receive(:call).with(env)
24
+
25
+ route.call(env)
26
+ end
27
+ end
28
+
5
29
  context "#initialize" do
6
30
  context "request method" do
7
31
  it "errors when an invalid request method is provided" do
@@ -41,25 +65,6 @@ module Galago
41
65
  end
42
66
  end
43
67
 
44
- context "#identify_params_in_path" do
45
- it "does not find named parameters when the path has none" do
46
- path = Router::Path.new('/accounts')
47
- identified_parameters = path.identify_params_in_path('/accounts')
48
-
49
- expect(identified_parameters).to be_empty
50
- end
51
-
52
- it "maps the names for params in the path" do
53
- path = Router::Path.new('/accounts/:account_id/users/:id')
54
- identified_parameters = path.identify_params_in_path('/accounts/42/users/11')
55
-
56
- expect(identified_parameters).to eql({
57
- 'account_id' => '42',
58
- 'id' => '11'
59
- })
60
- end
61
- end
62
-
63
68
  context "path" do
64
69
  it "remembers the path" do
65
70
  route = Router::Route.new('GET', '/foo', lambda {})
@@ -2,8 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  module Galago
4
4
  describe 'Router::VERSION' do
5
- it "0.0.1" do
6
- expect(Router::VERSION).to eql '0.0.1'
5
+ it "current" do
6
+ expect(Router::VERSION).to eql '0.0.2'
7
7
  end
8
8
  end
9
9
  end
@@ -2,18 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  module Galago
4
4
  describe Router do
5
- let(:router) { Class.new(Router) }
6
-
7
- describe '.call' do
8
- it 'tells the router to process the request' do
9
- expect(router.router).to receive(:process_request).with('env')
10
- router.call('env')
11
- end
12
- end
13
-
14
5
  describe '.routes' do
15
6
  it 'adds the specified routes' do
16
- router.routes do
7
+ router = Router.new do
17
8
  get '/foo' , to: lambda { |env| 'bar' }
18
9
  post '/foo' , to: lambda { |env| 'bar' }
19
10
  patch '/foo' , to: lambda { |env| 'bar' }
@@ -21,22 +12,11 @@ module Galago
21
12
  delete '/foo' , to: lambda { |env| 'bar' }
22
13
  end
23
14
 
24
- expect(router.router).to have_route(:get, '/foo')
25
- expect(router.router).to have_route(:post, '/foo')
26
- expect(router.router).to have_route(:patch, '/foo')
27
- expect(router.router).to have_route(:put, '/foo')
28
- expect(router.router).to have_route(:delete, '/foo')
29
- end
30
- end
31
-
32
- describe '.router' do
33
- it 'builds an instance if the router' do
34
- expect(router.router).to be_a Router
35
- end
36
-
37
- it 'remembers the router that was built' do
38
- router_id = router.router.object_id
39
- expect(router.router.object_id).to eql router_id
15
+ expect(router).to have_route(:get, '/foo')
16
+ expect(router).to have_route(:post, '/foo')
17
+ expect(router).to have_route(:patch, '/foo')
18
+ expect(router).to have_route(:put, '/foo')
19
+ expect(router).to have_route(:delete, '/foo')
40
20
  end
41
21
  end
42
22
 
@@ -83,12 +63,12 @@ module Galago
83
63
  end
84
64
  end
85
65
 
86
- describe "process_request" do
66
+ describe "#call" do
87
67
  it "calls the rack app when the route is found" do
88
68
  router = Router.new
89
69
  router.add_route(:get, '/foo', lambda { |env| [200, {}, 'bar'] })
90
70
 
91
- response = router.process_request({
71
+ response = router.call({
92
72
  'REQUEST_METHOD' => 'GET',
93
73
  'PATH_INFO' => '/foo'
94
74
  })
@@ -99,7 +79,7 @@ module Galago
99
79
  it "returns 404 when no route matchs the path" do
100
80
  router = Router.new
101
81
 
102
- response = router.process_request({
82
+ response = router.call({
103
83
  'REQUEST_METHOD' => 'GET',
104
84
  'PATH_INFO' => '/bar'
105
85
  })
data/spec/spec_helper.rb CHANGED
@@ -7,12 +7,4 @@ RSpec.configure do |config|
7
7
  config.formatter = :documentation
8
8
 
9
9
  config.include Rack::Test::Methods
10
-
11
- def application(&block)
12
- let(:app) do
13
- klass = Class.new(Galago::Router)
14
- klass.class_eval(&block)
15
- klass
16
- end
17
- end
18
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: galago-router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Karayusuf
@@ -106,8 +106,9 @@ files:
106
106
  - LICENSE.txt
107
107
  - README.md
108
108
  - Rakefile
109
- - galago.gemspec
110
- - lib/galago.rb
109
+ - examples/echo_param.ru
110
+ - examples/lobster.ru
111
+ - galago-router.gemspec
111
112
  - lib/galago/router.rb
112
113
  - lib/galago/router/dsl.rb
113
114
  - lib/galago/router/errors.rb
data/lib/galago.rb DELETED
@@ -1,2 +0,0 @@
1
- module Galago
2
- end