-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from tarcieri/feature/mime-type-parse
Brings back MIME-type parser/emiter
- Loading branch information
Showing
13 changed files
with
217 additions
and
6 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
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
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,76 @@ | ||
module HTTP | ||
# MIME type encode/decode adapters | ||
module MimeType | ||
class << self | ||
# Associate MIME type with adapter | ||
# | ||
# @example | ||
# | ||
# module JsonAdapter | ||
# class << self | ||
# def encode(obj) | ||
# # encode logic here | ||
# end | ||
# | ||
# def decode(str) | ||
# # decode logic here | ||
# end | ||
# end | ||
# end | ||
# | ||
# HTTP::MimeType.register_adapter 'application/json', MyJsonAdapter | ||
# | ||
# @param [#to_s] type | ||
# @param [#encode, #decode] adapter | ||
# @return [void] | ||
def register_adapter(type, adapter) | ||
adapters[type.to_s] = adapter | ||
end | ||
|
||
# Returns adapter associated with MIME type | ||
# | ||
# @param [#to_s] type | ||
# @raise [Error] if no adapter found | ||
# @return [void] | ||
def [](type) | ||
adapters[normalize type] || fail(Error, "Unknown MIME type: #{type}") | ||
end | ||
|
||
# Register a shortcut for MIME type | ||
# | ||
# @example | ||
# | ||
# HTTP::MimeType.register_alias 'application/json', :json | ||
# | ||
# @param [#to_s] type | ||
# @param [#to_sym] shortcut | ||
# @return [void] | ||
def register_alias(type, shortcut) | ||
aliases[shortcut.to_sym] = type.to_s | ||
end | ||
|
||
# Resolves type by shortcut if possible | ||
# | ||
# @param [#to_s] type | ||
# @return [String] | ||
def normalize(type) | ||
aliases.fetch type, type.to_s | ||
end | ||
|
||
private | ||
|
||
# :nodoc: | ||
def adapters | ||
@adapters ||= {} | ||
end | ||
|
||
# :nodoc: | ||
def aliases | ||
@aliases ||= {} | ||
end | ||
end | ||
end | ||
end | ||
|
||
# built-in mime types | ||
require 'http/mime_type/json' |
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,24 @@ | ||
require 'forwardable' | ||
require 'singleton' | ||
|
||
module HTTP | ||
module MimeType | ||
# Base encode/decode MIME type adapter | ||
class Adapter | ||
include Singleton | ||
|
||
class << self | ||
extend Forwardable | ||
def_delegators :instance, :encode, :decode | ||
end | ||
|
||
%w{ encode decode }.each do |operation| | ||
class_eval <<-RUBY, __FILE__, __LINE__ | ||
def #{operation}(*) | ||
fail Error, "\#{self.class} does not supports ##{operation}" | ||
end | ||
RUBY | ||
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,23 @@ | ||
require 'json' | ||
require 'http/mime_type/adapter' | ||
|
||
module HTTP | ||
module MimeType | ||
# JSON encode/decode MIME type adapter | ||
class JSON < Adapter | ||
# Encodes object to JSON | ||
def encode(obj) | ||
return obj.to_json if obj.respond_to?(:to_json) | ||
::JSON.dump obj | ||
end | ||
|
||
# Decodes JSON | ||
def decode(str) | ||
::JSON.load str | ||
end | ||
end | ||
|
||
register_adapter 'application/json', JSON | ||
register_alias 'application/json', :json | ||
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
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 'spec_helper' | ||
|
||
describe HTTP::Options, 'json' do | ||
|
||
let(:opts) { HTTP::Options.new } | ||
|
||
it 'defaults to nil' do | ||
expect(opts.json).to be nil | ||
end | ||
|
||
it 'may be specified with with_json data' do | ||
opts2 = opts.with_json(:foo => 42) | ||
expect(opts.json).to be nil | ||
expect(opts2.json).to eq(:foo => 42) | ||
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