Skip to content

Commit

Permalink
Lotus::Action::Mime no longer depends on Request
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jan 31, 2014
1 parent 9b7f524 commit 9a44477
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 51 deletions.
32 changes: 29 additions & 3 deletions lib/lotus/action/mime.rb
Expand Up @@ -5,7 +5,10 @@ module Action
# @since 0.1.0
module Mime
CONTENT_TYPE = 'Content-Type'.freeze
ENV_CONTENT_TYPE = 'CONTENT_TYPE'.freeze
ENV_HTTP_ACCEPT = 'HTTP_ACCEPT'.freeze
DEFAULT_CONTENT_TYPE = 'application/octet-stream'.freeze
CONTENT_TYPE_REGEX = /\s*[;,]\s*/.freeze

protected
# Finalize the response by setting the current content type
Expand Down Expand Up @@ -54,8 +57,6 @@ def content_type=(content_type)
#
# @since 0.1.0
#
# @see Rack::Request#media_type
# @see Lotus::HTTP::Request#accepts
# @see Lotus::Action::Mime#content_type=
#
# @example
Expand All @@ -70,7 +71,32 @@ def content_type=(content_type)
# end
# end
def content_type
@content_type || @_request.media_type || @_request.accepts || DEFAULT_CONTENT_TYPE
@content_type || media_type || accepts || DEFAULT_CONTENT_TYPE
end

private
def _raw_content_type
content_type = @_env[ENV_CONTENT_TYPE]
content_type.nil? || content_type.empty? ? nil : content_type
end

def media_type
_raw_content_type && _raw_content_type.split(CONTENT_TYPE_REGEX, 2).first.downcase
end

# FIXME I don't have the time to fix this hack now.
# FIXME I'm not sure I want to use this API at all.
def accepts
accept == '*/*' ? nil : accept
end

# TODO order according mime type weight (eg. q=0.8)
def accept
if _accept = @_env[ENV_HTTP_ACCEPT]
_accept.split(',').first
else
'*/*'
end
end
end
end
Expand Down
16 changes: 0 additions & 16 deletions lib/lotus/http/request.rb
Expand Up @@ -3,22 +3,6 @@
module Lotus
module HTTP
class Request < ::Rack::Request
HTTP_ACCEPT = 'HTTP_ACCEPT'.freeze

# TODO order according mime type weight (eg. q=0.8)
def accept
if _accept = env[HTTP_ACCEPT]
_accept.split(',').first
else
'*/*'
end
end

# FIXME I don't have the time to fix this hack now.
# FIXME I'm not sure I want to use this API at all.
def accepts
accept == '*/*' ? nil : accept
end
end
end
end
32 changes: 0 additions & 32 deletions test/http/request_test.rb
Expand Up @@ -4,36 +4,4 @@
it 'inheriths from Rack::Request' do
Lotus::HTTP::Request.new({}).must_be_kind_of(::Rack::Request)
end

describe '#accept' do
it 'returns "*/*" when HTTP_ACCEPT is not set' do
request = Lotus::HTTP::Request.new({})
request.accept.must_equal '*/*'
end

it 'returns "*/*" when HTTP_ACCEPT is "*/*"' do
request = Lotus::HTTP::Request.new({'HTTP_ACCEPT' => '*/*'})
request.accept.must_equal '*/*'
end

it 'returns "text/plain" when HTTP_ACCEPT is "text/plain"' do
request = Lotus::HTTP::Request.new({'HTTP_ACCEPT' => 'text/plain'})
request.accept.must_equal 'text/plain'
end

it 'returns "application/xml" when HTTP_ACCEPT is "application/xml,application/xhtml+xml"' do
request = Lotus::HTTP::Request.new({'HTTP_ACCEPT' => 'application/xml,application/xhtml+xml'})
request.accept.must_equal 'application/xml'
end

it 'returns "text/html" when HTTP_ACCEPT is "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"' do
request = Lotus::HTTP::Request.new({'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'})
request.accept.must_equal 'text/html'
end

# it 'returns "text/html" when HTTP_ACCEPT is "application/xml,application/xhtml+xml;q=0.8,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.6"' do
# request = Lotus::HTTP::Request.new({'HTTP_ACCEPT' => 'application/xml,application/xhtml+xml;q=0.8,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.6'})
# request.accept.must_equal 'text/html'
# end
end
end

0 comments on commit 9a44477

Please sign in to comment.