cb-api 5.1.1 → 5.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/cb/utils/validator.rb +14 -26
- data/lib/cb/version.rb +1 -1
- metadata +1 -1
data/lib/cb/utils/validator.rb
CHANGED
@@ -6,46 +6,34 @@ module Cb
|
|
6
6
|
|
7
7
|
class << self
|
8
8
|
def validate(response)
|
9
|
-
|
10
|
-
|
11
|
-
end
|
9
|
+
body = response.response.body rescue nil
|
10
|
+
return Hash.new if !body
|
12
11
|
|
13
12
|
if response.code != 200
|
14
|
-
|
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
|
-
|
16
|
+
try_parse_json(body) || try_parse_xml(body) || {}
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
20
|
|
21
|
-
|
21
|
+
def try_parse_json(body)
|
22
22
|
begin
|
23
|
-
|
24
|
-
json.keys.any? ? json : get_empty_json_hash
|
23
|
+
JSON.parse(body)
|
25
24
|
rescue JSON::ParserError
|
26
|
-
|
25
|
+
nil
|
27
26
|
end
|
28
27
|
end
|
29
28
|
|
30
|
-
def
|
29
|
+
def try_parse_xml(body)
|
31
30
|
begin
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
data/lib/cb/version.rb
CHANGED