Module: Cb::ResponseValidator
- Defined in:
- lib/cb/utils/validator.rb
Class Method Summary collapse
Class Method Details
.get_empty_json_hash ⇒ Object
41 42 43 |
# File 'lib/cb/utils/validator.rb', line 41 def self.get_empty_json_hash return JSON.parse('{}') end |
.handle_parser_error(response) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cb/utils/validator.rb', line 24 def self.handle_parser_error(response) # if it's not JSON, try XML xml = Nokogiri::XML.parse(response).elements.to_s if xml.blank? # if i wasn't xml either, give up and return an empty json hash return self.get_empty_json_hash else # if it was, return a hash from the xml UNLESS it was a generic xml_hash = Hash.from_xml(xml.to_s) if xml_hash.has_key?('Response') && xml_hash['Response'].has_key?('Errors') return self.get_empty_json_hash else return xml_hash end end end |
.validate(response) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/cb/utils/validator.rb', line 7 def self.validate(response) if response.blank? || response.response.body.blank? return self.get_empty_json_hash end begin json = JSON.parse(response.response.body) if json.keys.any? return json else return self.get_empty_json_hash end rescue JSON::ParserError return self.handle_parser_error(response.response.body) end end |