class Interpreter
Help interpret the general type of a particular object
Attributes
json_errors[RW]
@return [Error] JSON Errors found in interpreting response
xml_errors[RW]
@return [Error] XML Errors found in interpreting response
Public Class Methods
diagnose_error()
click to toggle source
@return [String] Description of error
# File lib/soaspec/interpreter.rb, line 47 def diagnose_error return xml_errors if looks_like_xml? return json_errors if looks_like_json? '' end
html?()
click to toggle source
@return [Boolean] Whether valid HTML. Must include at least one tag
# File lib/soaspec/interpreter.rb, line 65 def html? Nokogiri::HTML(@response) do |config| config.options = Nokogiri::XML::ParseOptions::DEFAULT_HTML end @response.include?('<') && @response.include?('>') rescue Nokogiri::XML::SyntaxError => e self.xml_errors = e false end
json?()
click to toggle source
@return [Boolean] Whether valid JSON
# File lib/soaspec/interpreter.rb, line 76 def json? JSON.parse(@response) rescue JSON::ParserError => e self.json_errors = e false end
looks_like_json?()
click to toggle source
@return [Boolean] Whether response has bracket like syntax similar to JSON. Could be a syntax error occurred
# File lib/soaspec/interpreter.rb, line 42 def looks_like_json? @response[0] == '{' && @response[-1] == '}' end
looks_like_xml?()
click to toggle source
@return [Boolean] Whether response has tag like syntax similar to XML. Could be a syntax error occurred
# File lib/soaspec/interpreter.rb, line 37 def looks_like_xml? @response[0] == '<' && @response[-1] == '>' end
response_type_for(response)
click to toggle source
@param [Object] response API response @return [Symbol] Type of provided response
# File lib/soaspec/interpreter.rb, line 14 def response_type_for(response) @xml_errors = nil @json_errors = nil @response = response case @response when String if xml? :xml elsif json? :json elsif html? :html else :string end when Hash then :hash when Nokogiri::XML::NodeSet, Nokogiri::XML::Document, Savon::Response then :xml else :unknown end end
xml?()
click to toggle source
@return [Boolean] Whether valid XML
# File lib/soaspec/interpreter.rb, line 56 def xml? Nokogiri::XML(@response) { |config| config.options = Nokogiri::XML::ParseOptions::STRICT } true rescue Nokogiri::XML::SyntaxError => e self.xml_errors = e false end