Skip to content

Commit

Permalink
Merge pull request #18 from zlw/15_action_format
Browse files Browse the repository at this point in the history
Implement Action#format
  • Loading branch information
jodosha committed Jun 19, 2014
2 parents c8b3e90 + 26e35be commit 6eda53d
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 8 deletions.
28 changes: 21 additions & 7 deletions lib/lotus/action/mime.rb
Expand Up @@ -89,17 +89,17 @@ def finish
headers.merge! CONTENT_TYPE => content_type
end

# Sets the given content type
# Sets the given format and corresponding content type
#
# Lotus::Action sets the proper content type automatically, this method
# is designed to override that value.
#
# @param content_type [String] the content type
# @param format [Symbol] the format
# @return [void]
#
# @since 0.1.0
# @since x.x.x
#
# @see Lotus::Action::Mime#content_type
# @see Lotus::Action::Mime#format
#
# @example
# require 'lotus/controller'
Expand All @@ -109,11 +109,16 @@ def finish
#
# def call(params)
# # ...
# self.content_type = 'application/json'
# self.format = :json
# end
# end
def content_type=(content_type)
@content_type = content_type
def format=(format)
@format = format.to_sym
@content_type = format_to_mime_type(format)
end

def format
@format || default_format
end

# The content type that will be automatically set in the response.
Expand Down Expand Up @@ -183,6 +188,7 @@ def accept?(mime_type)
end

private

def accept
@accept ||= @_env[HTTP_ACCEPT] || DEFAULT_ACCEPT
end
Expand All @@ -192,6 +198,14 @@ def accepts
::Rack::Utils.best_q_match(accept, ::Rack::Mime::MIME_TYPES.values)
end
end

def default_format
::Rack::Mime::MIME_TYPES.key(content_type).gsub(/\A\./, '').to_sym
end

def format_to_mime_type(format)
::Rack::Mime.mime_type(".#{format}")
end
end
end
end
109 changes: 109 additions & 0 deletions test/action/format_test.rb
@@ -0,0 +1,109 @@
require 'test_helper'

describe Lotus::Action do
class FormatController
include Lotus::Controller

action 'Default' do
def call(params)
end
end

action 'Custom' do
def call(params)
self.format = params[:format]
end
end
end

before do
@mime = ::Rack::Mime::MIME_TYPES
end

describe '#format' do
before do
@action = FormatController::Default.new
end

it 'lookup to #content_type if was not explicitly set (default: application/octet-stream)' do
@action.call({})
@action.send(:format).must_equal :a
end
end

describe '#format=' do
before do
@action = FormatController::Custom.new
end

describe 'html' do
before do
@action.call({ format: :html })
end

it 'sets format' do
@action.send(:format).must_equal :html
end

it 'sets content-type' do
@action.send(:content_type).must_equal 'text/html'
end
end

describe 'json' do
before do
@action.call({ format: :json })
end

it 'sets format' do
@action.send(:format).must_equal :json
end

it 'sets content-type' do
@action.send(:content_type).must_equal 'application/json'
end
end

describe 'json' do
before do
@action.call({ format: :xml })
end

it 'sets format' do
@action.send(:format).must_equal :xml
end

it 'sets content-type' do
@action.send(:content_type).must_equal 'application/xml'
end
end

describe 'atom' do
before do
@action.call({ format: :atom })
end

it 'sets format' do
@action.send(:format).must_equal :atom
end

it 'sets content-type' do
@action.send(:content_type).must_equal 'application/atom+xml'
end
end

describe 'js' do
before do
@action.call({ format: :js })
end

it 'sets format' do
@action.send(:format).must_equal :js
end

it 'sets content-type' do
@action.send(:content_type).must_equal 'application/javascript'
end
end
end
end
2 changes: 1 addition & 1 deletion test/integration/mime_type_test.rb
Expand Up @@ -18,7 +18,7 @@ def call(params)

action 'Custom' do
def call(params)
self.content_type = 'application/xml'
self.format = :xml
end
end

Expand Down

0 comments on commit 6eda53d

Please sign in to comment.