Module: Cb::ResponseValidator
- Defined in:
- lib/cb/utils/validator.rb
Class Method Summary collapse
- .get_empty_json_hash ⇒ Object
- .handle_parser_error(response_body) ⇒ Object
- .validate(response) ⇒ Object
Class Method Details
.get_empty_json_hash ⇒ Object
44 45 46 |
# File 'lib/cb/utils/validator.rb', line 44 def get_empty_json_hash Hash.new end |
.handle_parser_error(response_body) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cb/utils/validator.rb', line 30 def handle_parser_error(response_body) begin response_hash = XmlSimple.xml_in(response_body) # Unless there was an error, return a hash from the xml if response_hash.respond_to?(:has_key?) && response_hash.has_key?('Errors') return get_empty_json_hash else return response_hash end rescue ArgumentError, REXML::ParseException get_empty_json_hash end end |
.validate(response) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cb/utils/validator.rb', line 8 def validate(response) if response.nil? || response.response.body.nil? return 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 get_empty_json_hash if is_html end return get_empty_json_hash if response.response.body.nil? # Try to parse response as JSON. Otherwise, return HTTParty-parsed XML begin json = JSON.parse(response.response.body) json.keys.any? ? json : get_empty_json_hash rescue JSON::ParserError handle_parser_error(response.response.body) end end |