Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support XML and JSON inflector acronyms #416

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/active_resource/formats/json_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ def decode(json)
Formats.remove_root(ActiveSupport::JSON.decode(json))
end
end

JSONFormat = JsonFormat
end
end
2 changes: 2 additions & 0 deletions lib/active_resource/formats/xml_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ def decode(xml)
Formats.remove_root(Hash.from_xml(xml))
end
end

XMLFormat = XmlFormat
end
end
44 changes: 44 additions & 0 deletions test/cases/formats_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require "abstract_unit"

class FormatsTest < ActiveSupport::TestCase
def test_json_format_uses_camelcase
assert_equal ActiveResource::Formats::JsonFormat, ActiveResource::Formats[:json]
end

def test_xml_format_uses_camelcase
assert_equal ActiveResource::Formats::XmlFormat, ActiveResource::Formats[:xml]
end

def test_custom_format_uses_camelcase
klass = Class.new
ActiveResource::Formats.const_set(:MsgpackFormat, klass)

assert_equal klass, ActiveResource::Formats[:msgpack]
ensure
ActiveResource::Formats.send(:remove_const, :MsgpackFormat)
end

def test_unknown_format_raises_not_found_error
assert_raises NameError, match: "uninitialized constant ActiveResource::Formats::MsgpackFormat" do
ActiveResource::Formats[:msgpack]
end
end

def test_json_format_uses_acronym_inflections
ActiveSupport::Inflector.inflections { |inflect| inflect.acronym "JSON" }

assert_equal ActiveResource::Formats::JsonFormat, ActiveResource::Formats[:json]
ensure
ActiveSupport::Inflector.inflections.clear :acronyms
end

def test_xml_format_uses_acronym_inflections
ActiveSupport::Inflector.inflections { |inflect| inflect.acronym "XML" }

assert_equal ActiveResource::Formats::XmlFormat, ActiveResource::Formats[:xml]
ensure
ActiveSupport::Inflector.inflections.clear :acronyms
end
end
Loading