Skip to content

Commit

Permalink
Merge branch 'master' into 0.5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jan 7, 2016
2 parents ece124e + 54cfd9b commit a4747d8
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -12,10 +12,13 @@ rvm:
- 2.1.5
- 2.1.6
- 2.1.7
- 2.1.8
- 2.2.0
- 2.2.1
- 2.2.2
- 2.2.3
- 2.2.4
- 2.3.0
- jruby-9000
- rbx-2
- ruby-head
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
@@ -1,7 +1,7 @@
# Lotus::Router
Rack compatible HTTP router for Ruby

## v0.5.0 - (unreleased)
## v0.5.0 - 2016-01-12
### Added
- [Luca Guidi] Added `Lotus::Router#recognize` as a testing facility. Example `router.recognize('/') # => associated route`
- [Luca Guidi] Added `Lotus::Router.define` in order to wrap routes definitions in `config/routes.rb` when `Lotus::Router` is used outside of Lotus projects
Expand All @@ -14,6 +14,7 @@ Rack compatible HTTP router for Ruby

### Changed
- [Alfonso Uceda Pompa] A failure for body parsers raises a `Lotus::Routing::Parsing::BodyParsingError` exception
- [Karim Tarek] Introduced `Lotus::Router::Error` and let all the framework exceptions to inherit from it.

## v0.4.3 - 2015-09-30
### Added
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
@@ -1,4 +1,4 @@
Copyright © 2014-2015 Luca Guidi
Copyright © 2014-2016 Luca Guidi

MIT License

Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -689,4 +689,4 @@ Thanks to Joshua Hull ([@joshbuddy](https://github.com/joshbuddy)) for his

## Copyright

Copyright © 2014-2015 Luca Guidi – Released under MIT License
Copyright © 2014-2016 Luca Guidi – Released under MIT License
11 changes: 6 additions & 5 deletions lib/lotus/router.rb
Expand Up @@ -3,6 +3,7 @@
require 'lotus/routing/namespace'
require 'lotus/routing/resource'
require 'lotus/routing/resources'
require 'lotus/routing/error'

module Lotus
# Rack compatible, lightweight and fast HTTP Router.
Expand Down Expand Up @@ -75,13 +76,13 @@ class Router
# This error is raised when <tt>#call</tt> is invoked on a non-routable
# recognized route.
#
# @since x.x.x
# @since 0.5.0
#
# @see Lotus::Router#recognize
# @see Lotus::Routing::RecognizedRoute
# @see Lotus::Routing::RecognizedRoute#call
# @see Lotus::Routing::RecognizedRoute#routable?
class NotRoutableEndpointError < ::StandardError
class NotRoutableEndpointError < Lotus::Routing::Error
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
PATH_INFO = 'PATH_INFO'.freeze

Expand Down Expand Up @@ -112,7 +113,7 @@ def initialize(env)
#
# @return [Proc] the given block
#
# @since x.x.x
# @since 0.5.0
#
# @example
# # apps/web/config/routes.rb
Expand Down Expand Up @@ -942,7 +943,7 @@ def call(env)
#
# @return [Lotus::Routing::RecognizedRoute] the recognized route
#
# @since x.x.x
# @since 0.5.0
#
# @see Lotus::Router#env_for
# @see Lotus::Routing::RecognizedRoute
Expand Down Expand Up @@ -1139,7 +1140,7 @@ def inspector
#
# @return [Hash] Rack env
#
# @since x.x.x
# @since 0.5.0
# @api private
#
# @see Lotus::Router#recognize
Expand Down
3 changes: 2 additions & 1 deletion lib/lotus/routing/endpoint.rb
@@ -1,4 +1,5 @@
require 'delegate'
require 'lotus/routing/error'
require 'lotus/utils/class'

module Lotus
Expand All @@ -7,7 +8,7 @@ module Routing
# This is raised when the router fails to load an endpoint at the runtime.
#
# @since 0.1.0
class EndpointNotFound < ::StandardError
class EndpointNotFound < Lotus::Routing::Error
end

# Routing endpoint
Expand Down
7 changes: 7 additions & 0 deletions lib/lotus/routing/error.rb
@@ -0,0 +1,7 @@
module Lotus
module Routing
# @since 0.5.0
class Error < ::StandardError
end
end
end
9 changes: 5 additions & 4 deletions lib/lotus/routing/http_router.rb
Expand Up @@ -4,6 +4,7 @@
require 'lotus/routing/route'
require 'lotus/routing/parsers'
require 'lotus/routing/force_ssl'
require 'lotus/routing/error'
require 'lotus/utils/path_prefix'

Lotus::Utils::IO.silence_warnings do
Expand All @@ -17,7 +18,7 @@ module Routing
# given arguments.
#
# @since 0.1.0
class InvalidRouteException < ::StandardError
class InvalidRouteException < Lotus::Routing::Error
end

# HTTP router
Expand All @@ -32,11 +33,11 @@ class InvalidRouteException < ::StandardError
class HttpRouter < ::HttpRouter
# Script name - rack enviroment variable
#
# @since x.x.x
# @since 0.5.0
# @api private
SCRIPT_NAME = 'SCRIPT_NAME'.freeze

# @since x.x.x
# @since 0.5.0
# @api private
attr_reader :namespace

Expand Down Expand Up @@ -157,7 +158,7 @@ def no_response(request, env)
end

# @api private
# @since x.x.x
# @since 0.5.0
def rewrite_path_info(env, request)
super
env[SCRIPT_NAME] = @prefix + env[SCRIPT_NAME]
Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/routing/parsing/json_parser.rb
Expand Up @@ -16,7 +16,7 @@ def mime_types
#
# @raise [Lotus::Routing::Parsing::BodyParsingError] when the body can't be parsed.
#
# @since x.x.x
# @since 0.2.0
def parse(body)
JSON.parse(body)
rescue JSON::ParserError => e
Expand Down
14 changes: 11 additions & 3 deletions lib/lotus/routing/parsing/parser.rb
@@ -1,23 +1,27 @@
require 'lotus/utils/class'
require 'lotus/utils/string'
require 'lotus/routing/error'

module Lotus
module Routing
module Parsing
# Body parsing error
# This is raised when parser fails to parse the body
#
# @since x.x.x
class BodyParsingError < ::StandardError
# @since 0.5.0
class BodyParsingError < Lotus::Routing::Error
end

class UnknownParserError < ::StandardError
# @since 0.2.0
class UnknownParserError < Lotus::Routing::Error
def initialize(parser)
super("Unknown Parser: `#{ parser }'")
end
end

# @since 0.2.0
class Parser
# @since 0.2.0
def self.for(parser)
case parser
when String, Symbol
Expand All @@ -27,15 +31,19 @@ def self.for(parser)
end
end

# @since 0.2.0
def mime_types
raise NotImplementedError
end

# @since 0.2.0
def parse(body)
Hash.new
end

private
# @since 0.2.0
# @api private
def self.require_parser(parser)
require "lotus/routing/parsing/#{ parser }_parser"

Expand Down
24 changes: 12 additions & 12 deletions lib/lotus/routing/recognized_route.rb
Expand Up @@ -4,27 +4,27 @@ module Lotus
module Routing
# Represents a result of router path recognition.
#
# @since x.x.x
# @since 0.5.0
#
# @see Lotus::Router#recognize
class RecognizedRoute
# @since x.x.x
# @since 0.5.0
# @api private
REQUEST_METHOD = 'REQUEST_METHOD'.freeze

# @since x.x.x
# @since 0.5.0
# @api private
NAMESPACE = '%s::'.freeze

# @since x.x.x
# @since 0.5.0
# @api private
NAMESPACE_REPLACEMENT = ''.freeze

# @since x.x.x
# @since 0.5.0
# @api private
ACTION_PATH_SEPARATOR = '/'.freeze

# @since x.x.x
# @since 0.5.0
# @api public
attr_reader :params

Expand All @@ -36,7 +36,7 @@ class RecognizedRoute
#
# @return [Lotus::Routing::RecognizedRoute]
#
# @since x.x.x
# @since 0.5.0
# @api private
def initialize(response, env, router)
@env = env
Expand All @@ -58,7 +58,7 @@ def initialize(response, env, router)
#
# @raise [Lotus::Router::NotRoutableEndpointError] if not routable
#
# @since x.x.x
# @since 0.5.0
# @api public
#
# @see Lotus::Routing::RecognizedRoute#routable?
Expand All @@ -75,7 +75,7 @@ def call(env)
#
# @return [String]
#
# @since x.x.x
# @since 0.5.0
# @api public
def verb
@env[REQUEST_METHOD]
Expand All @@ -85,7 +85,7 @@ def verb
#
# @return [String]
#
# @since x.x.x
# @since 0.5.0
# @api public
#
# @see Lotus::Router#recognize
Expand Down Expand Up @@ -114,7 +114,7 @@ def action
#
# @return [TrueClass,FalseClass]
#
# @since x.x.x
# @since 0.5.0
# @api public
#
# @see Lotus::Router#recognize
Expand All @@ -134,7 +134,7 @@ def routable?

private

# @since x.x.x
# @since 0.5.0
# @api private
#
# @see Lotus::Routing::Endpoint
Expand Down
14 changes: 14 additions & 0 deletions test/error_test.rb
@@ -0,0 +1,14 @@
require 'test_helper'

describe Lotus::Routing::Error do
it 'inherits from ::StandardError' do
Lotus::Routing::Error.superclass.must_equal StandardError
end

it 'is parent to all custom exception' do
Lotus::Routing::Parsing::BodyParsingError.superclass.must_equal Lotus::Routing::Error
Lotus::Routing::Parsing::UnknownParserError.superclass.must_equal Lotus::Routing::Error
Lotus::Routing::InvalidRouteException.superclass.must_equal Lotus::Routing::Error
Lotus::Routing::EndpointNotFound.superclass.must_equal Lotus::Routing::Error
end
end

0 comments on commit a4747d8

Please sign in to comment.