Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements to api_response for formatting and blank responses; remo…
…ve nil for api_response payload; better error handling; improved tests.
- Loading branch information
1 parent
a0d8e2b
commit 702e9e1
Showing
14 changed files
with
148 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ rvm: | |
- 3.0.0 | ||
jobs: | ||
allow_failures: | ||
- rvm: 2.7.2 | ||
- rvm: 3.0.0 | ||
script: | ||
- bundle exec rake test |
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
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,26 @@ | ||
# Top-level class for all REST Framework errors. | ||
class RESTFramework::Error < StandardError | ||
end | ||
|
||
class RESTFramework::NilPassedToAPIResponseError < RESTFramework::Error | ||
def message | ||
return <<~MSG.split("\n").join(' ') | ||
Payload of `nil` was passed to `api_response`; this is unsupported. If you want a blank | ||
response, pass `''` (an empty string) as the payload. If this was the result of a `find_by` | ||
(or similar Active Record method) not finding a record, you should use the bang version (e.g., | ||
`find_by!`) to raise `ActiveRecord::RecordNotFound`, which the REST controller will catch and | ||
return an appropriate error response. | ||
MSG | ||
end | ||
end | ||
|
||
class RESTFramework::UnserializableError < RESTFramework::Error | ||
def initialize(object) | ||
@object = object | ||
return super | ||
end | ||
|
||
def message | ||
return "Unable to serialize `#{@object.inspect}` (of type `#{@object.class}`)." | ||
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,37 @@ | ||
require_relative '../base' | ||
|
||
|
||
class Api2::RootControllerTest < ActionController::TestCase | ||
def test_nil_fails | ||
assert_raise RESTFramework::NilPassedToAPIResponseError do | ||
get :nil | ||
end | ||
end | ||
|
||
def test_nil_json_fails | ||
assert_raise RESTFramework::NilPassedToAPIResponseError do | ||
get :nil, as: :json | ||
end | ||
end | ||
|
||
def test_nil_xml_fails | ||
assert_raise RESTFramework::NilPassedToAPIResponseError do | ||
get :nil, as: :xml | ||
end | ||
end | ||
|
||
def test_blank | ||
get :blank | ||
assert_response :success | ||
end | ||
|
||
def test_blank_json | ||
get :blank, as: :json | ||
assert_response :success | ||
end | ||
|
||
def test_blank_xml | ||
get :blank, as: :xml | ||
assert_response :success | ||
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