cb-api 5.1.1 → 5.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,46 +6,34 @@ module Cb
6
6
 
7
7
  class << self
8
8
  def validate(response)
9
- if response.nil? || response.response.body.nil?
10
- return get_empty_json_hash
11
- end
9
+ body = response.response.body rescue nil
10
+ return Hash.new if !body
12
11
 
13
12
  if response.code != 200
14
- # we only handle json or xml responses - html means something bad happened
15
- is_html = response.response.body.include?('<!DOCTYPE html')
16
- return get_empty_json_hash if is_html
13
+ return Hash.new if body.include?('<!DOCTYPE html')
17
14
  end
18
15
 
19
- return get_empty_json_hash if response.response.body.nil?
16
+ try_parse_json(body) || try_parse_xml(body) || {}
17
+ end
18
+
19
+ private
20
20
 
21
- # Try to parse response as JSON. Otherwise, return HTTParty-parsed XML
21
+ def try_parse_json(body)
22
22
  begin
23
- json = JSON.parse(response.response.body)
24
- json.keys.any? ? json : get_empty_json_hash
23
+ JSON.parse(body)
25
24
  rescue JSON::ParserError
26
- handle_parser_error(response.response.body)
25
+ nil
27
26
  end
28
27
  end
29
28
 
30
- def handle_parser_error(response_body)
29
+ def try_parse_xml(body)
31
30
  begin
32
- response_hash = XmlSimple.xml_in(response_body)
33
- # Unless there was an error, return a hash from the xml
34
- if response_hash.respond_to?(:has_key?) && response_hash.has_key?('Errors')
35
- return get_empty_json_hash
36
- else
37
- return response_hash
38
- end
39
- rescue ArgumentError, REXML::ParseException
40
- get_empty_json_hash
31
+ MultiXml.parse(body, KeepRoot: true)
32
+ rescue MultiXml::ParseError
33
+ nil
41
34
  end
42
35
  end
43
-
44
- def get_empty_json_hash
45
- Hash.new
46
- end
47
36
  end
48
37
 
49
- # private_class_method :handle_parser_error, :get_empty_json_hash
50
38
  end
51
39
  end
@@ -1,3 +1,3 @@
1
1
  module Cb
2
- VERSION = '5.1.1'
2
+ VERSION = '5.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cb-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.1
4
+ version: 5.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: