Module: Cb::ResponseValidator
- Defined in:
- lib/cb/utils/validator.rb
Class Method Summary collapse
- .get_empty_json_hash ⇒ Object
- .handle_parser_error(response) ⇒ Object
- .nori ⇒ Object
- .validate(response) ⇒ Object
Class Method Details
.get_empty_json_hash ⇒ Object
42 43 44 |
# File 'lib/cb/utils/validator.rb', line 42 def self.get_empty_json_hash Hash.new end |
.handle_parser_error(response) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cb/utils/validator.rb', line 25 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 = nori.parse(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 |
.nori ⇒ Object
46 47 48 |
# File 'lib/cb/utils/validator.rb', line 46 def self.nori @nori ||= Nori.new end |
.validate(response) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cb/utils/validator.rb', line 6 def self.validate(response) if response.blank? || response.response.body.blank? return self.get_empty_json_hash end if response.code != 200 # we only handle json or xml responses - html means something bad happened is_html = response.response.body.include?('<!DOCTYPE html') return self.get_empty_json_hash if is_html end begin json = JSON.parse(response.response.body) json.keys.any? ? json : self.get_empty_json_hash rescue JSON::ParserError self.handle_parser_error(response.response.body) end end |