Skip to content

Commit

Permalink
introduces request and response format
Browse files Browse the repository at this point in the history
and deprecates default_format
  • Loading branch information
Thiago Felippe committed Sep 15, 2015
1 parent 8625819 commit 8a97bf7
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ end
gem 'lotus-utils', '~> 0.5', require: false, github: 'lotus/utils', branch: '0.5.x'
gem 'lotus-validations', '~> 0.3', require: false, github: 'lotus/validations', branch: '0.3.x'
gem 'lotus-router', '~> 0.4', require: false, github: 'lotus/router', branch: '0.4.x'
gem 'lotus-controller', '~> 0.4', require: false, github: 'lotus/controller', branch: '0.4.x'
gem 'lotus-controller', '~> 0.4', require: false, github: 'theocodes/controller', branch: 'response-format-improvements'
# gem 'lotus-controller', '~> 0.4', require: false, github: 'lotus/controller', branch: '0.4.x'
gem 'lotus-view', '~> 0.4', require: false, github: 'lotus/view', branch: '0.4.x'
gem 'lotus-model', '~> 0.4', require: false, github: 'lotus/model', branch: '0.4.x'
gem 'lotus-helpers', '~> 0.2', require: false, github: 'lotus/helpers', branch: '0.2.x'
Expand Down
85 changes: 75 additions & 10 deletions lib/lotus/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'lotus/utils/kernel'
require 'lotus/utils/deprecation'
require 'lotus/environment'
require 'lotus/config/framework_configuration'
require 'lotus/config/load_paths'
Expand Down Expand Up @@ -958,18 +959,18 @@ def adapter(options = {})
# an argument, it will set the corresponding instance variable. When
# called without, it will return the already set value, or the default.
#
# @overload default_format(format)
# @overload default_request_format(format)
# Sets the given value
# @param format [#to_sym] the symbol format
# @raise [TypeError] if it cannot be coerced to a symbol
#
# @overload default_format
# @overload default_request_format
# Gets the value
# @return [Symbol]
#
# @since 0.1.0
#
# @see http://rdoc.info/gems/lotus-controller/Lotus/Controller/Configuration#default_format
# @see http://rdoc.info/gems/lotus-controller/Lotus/Controller/Configuration#default_request_format
#
# @example Getting the value
# require 'lotus'
Expand All @@ -979,28 +980,92 @@ def adapter(options = {})
# end
# end
#
# Bookshelf::Application.configuration.default_format # => :html
# Bookshelf::Application.configuration.default_request_format # => :html
#
# @example Setting the value
# require 'lotus'
#
# module Bookshelf
# class Application < Lotus::Application
# configure do
# default_format :json
# default_request_format :json
# end
# end
# end
#
# Bookshelf::Application.configuration.default_format # => :json
def default_format(format = nil)
# Bookshelf::Application.configuration.default_request_format # => :json
def default_request_format(format = nil)
if format
@default_format = Utils::Kernel.Symbol(format)
@default_request_format = Utils::Kernel.Symbol(format)
else
@default_format || :html
@default_request_format || :html
end
end

# Set a format to be used for all responses regardless of the request type.
#
# The given format must be coercible to a symbol, and be a valid mime type
# alias. If it isn't, at the runtime the framework will raise a
# `Lotus::Controller::UnknownFormatError`.
#
# By default this value is `:html`.
#
# This is part of a DSL, for this reason when this method is called with
# an argument, it will set the corresponding instance variable. When
# called without, it will return the already set value, or the default.
#
# @overload default_response_format(format)
# Sets the given value
# @param format [#to_sym] the symbol format
# @raise [TypeError] if it cannot be coerced to a symbol
#
# @overload default_response_format
# Gets the value
# @return [Symbol,nil]
#
# @example Getting the value
# require 'lotus'
#
# module Bookshelf
# class Application < Lotus::Application
# end
# end
#
# Bookshelf::Application.configuration.default_response_format # => :html
#
# @example Setting the value
# require 'lotus'
#
# module Bookshelf
# class Application < Lotus::Application
# configure do
# default_response_format :json
# end
# end
# end
#
# Bookshelf::Application.configuration.default_request_format # => :json
#
# @since 0.5.0 TODO: Confirm?
def default_response_format(format = nil)
if format
@default_response_format = Utils::Kernel.Symbol(format)
else
@default_response_format
end
end

# Set a format as default fallback for all the requests without a strict
# requirement for the mime type.
#
# @since 0.1.0
#
# @deprecated Use {#default_request_format} instead.
def default_format(format = nil)
Lotus::Utils::Deprecation.new('default_format is deprecated, please use default_request_format')
default_request_format(format)
end

# The URI scheme for this application.
# This is used by the router helpers to generate absolute URLs.
#
Expand Down Expand Up @@ -1577,7 +1642,7 @@ def model
# module Bookshelf
# class Application < Lotus::Application
# configure do
# controller.default_format :json
# controller.default_request_format :json
# end
# end
# end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ module <%= config[:classified_app_name] %>
# Default format for the requests that don't specify an HTTP_ACCEPT header
# Argument: A symbol representation of a mime type, default to :html
#
# default_format :html
# default_request_format :html

# Default format for responses that doesn't take into account the request format
# Argument: A symbol representation of a mime type, default to :html
#
# default_response_format :html

# HTTP Body parsers
# Parse non GET responses body for a specific mime type
Expand Down
7 changes: 6 additions & 1 deletion lib/lotus/generators/slice/application.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ module <%= config[:classified_slice_name] %>
# Default format for the requests that don't specify an HTTP_ACCEPT header
# Argument: A symbol representation of a mime type, default to :html
#
# default_format :html
# default_request_format :html

# Default format for responses that doesn't take into account the request format
# Argument: A symbol representation of a mime type, default to :html
#
# default_response_format :html

# HTTP Body parsers
# Parse non GET responses body for a specific mime type
Expand Down
3 changes: 2 additions & 1 deletion lib/lotus/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def _configure_controller_framework!
unless namespace.const_defined?('Controller')
controller = Lotus::Controller.duplicate(namespace) do
handle_exceptions config.handle_exceptions
default_format config.default_format
default_request_format config.default_request_format
default_response_format config.default_response_format
default_headers({
Lotus::Config::Security::X_FRAME_OPTIONS_HEADER => config.security.x_frame_options,
Lotus::Config::Security::CONTENT_SECURITY_POLICY_HEADER => config.security.content_security_policy
Expand Down
40 changes: 33 additions & 7 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -605,25 +605,47 @@ class FileSystem
end
end

describe '#default_format' do
describe '#default_request_format' do
describe "when not previously set" do
it 'returns :html' do
@configuration.default_request_format.must_equal :html
end
end

describe "when set" do
before do
@configuration.default_request_format :json
end

it 'returns the value' do
@configuration.default_request_format.must_equal :json
end
end

it 'raises an error if the given format cannot be coerced into symbol' do
-> { @configuration.default_request_format(23) }.must_raise TypeError
end
end

describe '#default_response_format' do
describe "when not previously set" do
it 'returns nil' do
@configuration.default_format.must_equal :html
@configuration.default_response_format.must_equal nil
end
end

describe "when set" do
before do
@configuration.default_format :json
@configuration.default_response_format :json
end

it 'returns the value' do
@configuration.default_format.must_equal :json
@configuration.default_response_format.must_equal :json
end
end

it 'raises an error if the given format cannot be coerced into symbol' do
-> { @configuration.default_format(23) }.must_raise TypeError
-> { @configuration.default_response_format(23) }.must_raise TypeError
end
end

Expand Down Expand Up @@ -774,8 +796,12 @@ class FileSystem
-> { @configuration.view_pattern('%{controller}::%{action}') }.must_raise RuntimeError
end

it 'it raises error when try to mutate the default_format' do
-> { @configuration.default_format(:xml) }.must_raise RuntimeError
it 'it raises error when try to mutate the default_request_format' do
-> { @configuration.default_request_format(:xml) }.must_raise RuntimeError
end

it 'it raises error when try to mutate the default_response_format' do
-> { @configuration.default_response_format(:xml) }.must_raise RuntimeError
end

it 'it raises error when try to mutate the scheme' do
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Application < Lotus::Application
load_paths.clear
templates 'app/templates'

default_response_format :html

scheme 'https'
host 'lotus-coffeeshop.org'

Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/configurable/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Application < Lotus::Application
model.adapter type: :memory, uri: 'memory://localhost'
model.mapping { }

controller.default_format :xml
controller.default_request_format :xml
controller.default_response_format :json
controller.default_charset 'koi8-r'
controller.prepare do
include Authentication
Expand Down
3 changes: 2 additions & 1 deletion test/integration/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def request

describe "controller configuration" do
it 'forwards settings' do
Configurable::Controller.configuration.default_format.must_equal :xml
Configurable::Controller.configuration.default_request_format.must_equal :xml
Configurable::Controller.configuration.default_response_format.must_equal :json
Configurable::Controller.configuration.default_charset.must_equal 'koi8-r'
end
end
Expand Down
8 changes: 6 additions & 2 deletions test/loader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@
CoffeeShop::Controller.configuration.action_module.must_equal(CoffeeShop::Action)
end

it 'configures controller to use default format' do
CoffeeShop::Controller.configuration.default_format.must_equal(:html)
it 'configures controller to use default request format' do
CoffeeShop::Controller.configuration.default_request_format.must_equal(:html)
end

it 'configures controller to use default response format' do
CoffeeShop::Controller.configuration.default_response_format.must_equal(:html)
end

it 'generates controllers namespace' do
Expand Down

0 comments on commit 8a97bf7

Please sign in to comment.