Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Body parsing refactoring. Allow custom parsers.
- Loading branch information
Showing
6 changed files
with
133 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'rack/utils/okjson' | ||
|
||
module Lotus | ||
module Routing | ||
module Parsing | ||
class JsonParser < Parser | ||
def mime_types | ||
['application/json'] | ||
end | ||
|
||
def parse(body) | ||
Rack::Utils::OkJson.decode(body) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'lotus/utils/class' | ||
require 'lotus/utils/string' | ||
|
||
module Lotus | ||
module Routing | ||
module Parsing | ||
class UnknownParserError < ::StandardError | ||
def initialize(parser) | ||
super("Unknown Parser: `#{ parser }'") | ||
end | ||
end | ||
|
||
class Parser | ||
def self.for(parser) | ||
case parser | ||
when String, Symbol | ||
require_parser(parser) | ||
else | ||
parser | ||
end | ||
end | ||
|
||
def mime_types | ||
raise NotImplementedError | ||
end | ||
|
||
def parse(body) | ||
Hash.new | ||
end | ||
|
||
private | ||
def self.require_parser(parser) | ||
require "lotus/routing/parsing/#{ parser }_parser" | ||
|
||
parser = Utils::String.new(parser).classify | ||
Utils::Class.load!("Lotus::Routing::Parsing::#{ parser }Parser").new | ||
rescue LoadError, NameError | ||
raise UnknownParserError.new(parser) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,40 @@ | ||
require 'test_helper' | ||
require 'json' | ||
|
||
describe 'Body parsing' do | ||
before do | ||
json_endpoint = ->(env) { | ||
[200, {}, [JSON.generate(env['router.params'])]] | ||
endpoint = ->(env) { | ||
[200, {}, [env['router.params']]] | ||
} | ||
|
||
@routes = Lotus::Router.new(parsers: [:json]) { | ||
patch '/books/:id', to: json_endpoint | ||
@routes = Lotus::Router.new(parsers: [:json, XmlParser.new]) { | ||
patch '/books/:id', to: endpoint | ||
patch '/authors/:id', to: endpoint | ||
} | ||
|
||
@app = Rack::MockRequest.new(@routes) | ||
end | ||
|
||
it 'is successful (JSON)' do | ||
body = StringIO.new(JSON.generate({published: 'true'})) | ||
body = StringIO.new( %({"published":"true"}) ) | ||
response = @app.patch('/books/23', 'CONTENT_TYPE' => 'application/json', 'rack.input' => body) | ||
|
||
response.status.must_equal 200 | ||
response.body.must_equal %({"published":"true","id":"23"}) | ||
response.body.must_equal %({"published"=>"true", :id=>"23"}) | ||
end | ||
|
||
it 'is successful (XML)' do | ||
body = StringIO.new( %(<name>LG</name>) ) | ||
response = @app.patch('/authors/23', 'CONTENT_TYPE' => 'application/xml', 'rack.input' => body) | ||
|
||
response.status.must_equal 200 | ||
response.body.must_equal %({"name"=>"LG", :id=>"23"}) | ||
end | ||
|
||
it 'is successful (XML aliased mime)' do | ||
body = StringIO.new( %(<name>MGF</name>) ) | ||
response = @app.patch('/authors/15', 'CONTENT_TYPE' => 'text/xml', 'rack.input' => body) | ||
|
||
response.status.must_equal 200 | ||
response.body.must_equal %({"name"=>"MGF", :id=>"15"}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters