Class: Interpreter

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

Overview

Help interpret the general type of a particular object

Class Method Summary collapse

Class Method Details

.json?Boolean

Returns Whether valid JSON.

Returns:

  • (Boolean)

    Whether valid JSON



35
36
37
38
39
# File 'lib/soaspec/interpreter.rb', line 35

def json?
  JSON.parse(@response)
rescue JSON::ParserError
  false
end

.response_type_for(response) ⇒ Symbol

Returns Type of provided response.

Parameters:

  • response (Object)

    API response

Returns:

  • (Symbol)

    Type of provided response



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/soaspec/interpreter.rb', line 8

def response_type_for(response)
  @response = response
  if @response.is_a? String
    if xml?
      :xml
    elsif json?
      :json
    else
      :unknown
    end
  elsif response.is_a? Hash
    :hash
  elsif response.is_a? Nokogiri::XML::NodeSet
    :xml
  else
    :unknown
  end
end

.xml?Boolean

Returns Whether valid XML.

Returns:

  • (Boolean)

    Whether valid XML



28
29
30
31
32
# File 'lib/soaspec/interpreter.rb', line 28

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