Skip to content

Commit f95fd4f

Browse files
authored
Merge pull request #446 from seanpdoyle/error-parsing-decode
Validations: Decode errors with XmlFormat or JsonFormat
2 parents aef92ec + 07e544a commit f95fd4f

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

lib/active_resource/formats/json_format.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ def encode(resource, options = nil)
1919
resource.to_json(options)
2020
end
2121

22-
def decode(json)
22+
def decode(json, remove_root = true)
2323
return nil if json.nil?
24-
Formats.remove_root(ActiveSupport::JSON.decode(json))
24+
hash = ActiveSupport::JSON.decode(json)
25+
remove_root ? Formats.remove_root(hash) : hash
2526
end
2627
end
2728
end

lib/active_resource/formats/xml_format.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ def encode(resource, options = {})
1919
resource.to_xml(options)
2020
end
2121

22-
def decode(xml)
23-
Formats.remove_root(Hash.from_xml(xml))
22+
def decode(xml, remove_root = true)
23+
hash = Hash.from_xml(xml)
24+
remove_root ? Formats.remove_root(hash) : hash
2425
end
2526
end
2627
end

lib/active_resource/validations.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ def from_hash(messages, save_cache = false)
5454

5555
# Grabs errors from a json response.
5656
def from_json(json, save_cache = false)
57-
decoded = ActiveSupport::JSON.decode(json) || {} rescue {}
57+
decoded = Formats[:json].decode(json, false) || {} rescue {}
5858
errors = decoded["errors"] || {}
5959
from_hash errors, save_cache
6060
end
6161

6262
# Grabs errors from an XML response.
6363
def from_xml(xml, save_cache = false)
64-
array = Array.wrap(Hash.from_xml(xml)["errors"]["error"]) rescue []
64+
array = Array.wrap(Formats[:xml].decode(xml, false)["errors"]["error"]) rescue []
6565
from_array array, save_cache
6666
end
6767
end

0 commit comments

Comments
 (0)