Class: Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/soaspec/interpreter.rb

Overview

Help interpret the general type of a particular object

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.json_errorsError

Returns JSON Errors found in interpreting response.

Returns:

  • (Error)

    JSON Errors found in interpreting response



10
11
12
# File 'lib/soaspec/interpreter.rb', line 10

def json_errors
  @json_errors
end

.xml_errorsError

Returns XML Errors found in interpreting response.

Returns:

  • (Error)

    XML Errors found in interpreting response



7
8
9
# File 'lib/soaspec/interpreter.rb', line 7

def xml_errors
  @xml_errors
end

Class Method Details

.diagnose_errorString

Returns Description of error.

Returns:

  • (String)

    Description of error



48
49
50
51
52
53
54
# File 'lib/soaspec/interpreter.rb', line 48

def diagnose_error
  return xml_errors if looks_like_xml?

  return json_errors if looks_like_json?

  ''
end

.html?Boolean

Returns Whether valid HTML. Must include at least one tag.

Returns:

  • (Boolean)

    Whether valid HTML. Must include at least one tag



66
67
68
69
70
71
72
73
74
# File 'lib/soaspec/interpreter.rb', line 66

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?Boolean

Returns Whether valid JSON.

Returns:

  • (Boolean)

    Whether valid JSON



77
78
79
80
81
82
# File 'lib/soaspec/interpreter.rb', line 77

def json?
  JSON.parse(@response)
rescue JSON::ParserError => e
  self.json_errors = e
  false
end

.looks_like_json?Boolean

Returns Whether response has bracket like syntax similar to JSON. Could be a syntax error occurred.

Returns:

  • (Boolean)

    Whether response has bracket like syntax similar to JSON. Could be a syntax error occurred



43
44
45
# File 'lib/soaspec/interpreter.rb', line 43

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.

Returns:

  • (Boolean)

    Whether response has tag like syntax similar to XML. Could be a syntax error occurred



38
39
40
# File 'lib/soaspec/interpreter.rb', line 38

def looks_like_xml?
  @response[0] == '<' && @response[-1] == '>'
end

.response_type_for(response) ⇒ Symbol

Returns Type of provided response.

Parameters:

  • response (Object)

    API response

Returns:

  • (Symbol)

    Type of provided response



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/soaspec/interpreter.rb', line 14

def response_type_for(response)
  @xml_errors = nil
  @json_errors = nil
  @response = response
  if @response.is_a? String
    if xml?
      :xml
    elsif json?
      :json
    elsif html?
      :html
    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.

Returns:

  • (Boolean)

    Whether valid XML



57
58
59
60
61
62
63
# File 'lib/soaspec/interpreter.rb', line 57

def xml?
  Nokogiri::XML(@response) { |config| config.options = Nokogiri::XML::ParseOptions::STRICT }
  true
rescue Nokogiri::XML::SyntaxError => e
  self.xml_errors = e
  false
end