Class: Interpreter
- Inherits:
-
Object
- Object
- Interpreter
- Defined in:
- lib/soaspec/interpreter.rb
Overview
Help interpret the general type of a particular object
Class Attribute Summary collapse
-
.json_errors ⇒ Error
JSON Errors found in interpreting response.
-
.xml_errors ⇒ Error
XML Errors found in interpreting response.
Class Method Summary collapse
-
.diagnose_error ⇒ String
Description of error.
-
.json? ⇒ Boolean
Whether valid JSON.
-
.looks_like_json? ⇒ Boolean
Whether response has bracket like syntax similar to JSON.
-
.looks_like_xml? ⇒ Boolean
Whether response has tag like syntax similar to XML.
-
.response_type_for(response) ⇒ Symbol
Type of provided response.
-
.xml? ⇒ Boolean
Whether valid XML.
Class Attribute Details
.json_errors ⇒ Error
Returns JSON Errors found in interpreting response.
8 9 10 |
# File 'lib/soaspec/interpreter.rb', line 8 def json_errors @json_errors end |
.xml_errors ⇒ Error
Returns XML Errors found in interpreting response.
5 6 7 |
# File 'lib/soaspec/interpreter.rb', line 5 def xml_errors @xml_errors end |
Class Method Details
.diagnose_error ⇒ String
Returns Description of error.
44 45 46 47 48 49 50 |
# File 'lib/soaspec/interpreter.rb', line 44 def diagnose_error return xml_errors if looks_like_xml? return json_errors if looks_like_json? '' end |
.json? ⇒ Boolean
Returns Whether valid JSON.
61 62 63 64 65 66 |
# File 'lib/soaspec/interpreter.rb', line 61 def json? JSON.parse(@response) rescue JSON::ParserError=> json_error self.json_errors = json_error false end |
.looks_like_json? ⇒ Boolean
Returns Whether response has bracket like syntax similar to JSON. Could be a syntax error occurred.
39 40 41 |
# File 'lib/soaspec/interpreter.rb', line 39 def looks_like_json? @response[0] == '{' && @response[-1] == '}' end |
.looks_like_xml? ⇒ Boolean
Returns Whether response has tag like syntax similar to XML. Could be a syntax error occurred.
34 35 36 |
# File 'lib/soaspec/interpreter.rb', line 34 def looks_like_xml? @response[0] == '<' && @response[-1] == '>' end |
.response_type_for(response) ⇒ Symbol
Returns Type of provided response.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/soaspec/interpreter.rb', line 12 def response_type_for(response) @xml_errors = nil @json_errors = nil @response = response if @response.is_a? String if xml? :xml elsif json? :json else :string end elsif response.is_a? Hash :hash elsif response.is_a?(Nokogiri::XML::NodeSet) || response.is_a?(Nokogiri::XML::Document) :xml else :unknown end end |
.xml? ⇒ Boolean
Returns Whether valid XML.
53 54 55 56 57 58 |
# File 'lib/soaspec/interpreter.rb', line 53 def xml? Nokogiri::XML(@response) { |config| config. = Nokogiri::XML::ParseOptions::STRICT } rescue Nokogiri::XML::SyntaxError => xml_error self.xml_errors = xml_error false end |