Sha256: 0da670435316224d273bbd304e302ebd55eff239d390705be1a15f6a3e09797e

Contents?: true

Size: 902 Bytes

Versions: 28

Compression:

Stored size: 902 Bytes

Contents

# Help interpret the general type of a particular object
class Interpreter

  class << self
    # @param [Object] response API response
    # @return [Symbol] Type of provided response
    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

    # @return [Boolean] Whether valid XML
    def xml?
      Nokogiri::XML(@response) { |config| config.options = Nokogiri::XML::ParseOptions::STRICT }
    rescue Nokogiri::XML::SyntaxError
      false
    end

    # @return [Boolean] Whether valid JSON
    def json?
      JSON.parse(@response)
    rescue JSON::ParserError
      false
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
soaspec-0.2.8 lib/soaspec/interpreter.rb
soaspec-0.2.7 lib/soaspec/interpreter.rb
soaspec-0.2.5 lib/soaspec/interpreter.rb
soaspec-0.2.4 lib/soaspec/interpreter.rb
soaspec-0.2.3 lib/soaspec/interpreter.rb
soaspec-0.2.2 lib/soaspec/interpreter.rb
soaspec-0.2.1 lib/soaspec/interpreter.rb
soaspec-0.2.0 lib/soaspec/interpreter.rb
soaspec-0.1.18 lib/soaspec/interpreter.rb
soaspec-0.1.17 lib/soaspec/interpreter.rb
soaspec-0.1.16 lib/soaspec/interpreter.rb
soaspec-0.1.15 lib/soaspec/interpreter.rb
soaspec-0.1.14 lib/soaspec/interpreter.rb
soaspec-0.1.13 lib/soaspec/interpreter.rb
soaspec-0.1.12 lib/soaspec/interpreter.rb
soaspec-0.1.11 lib/soaspec/interpreter.rb
soaspec-0.1.10 lib/soaspec/interpreter.rb
soaspec-0.1.9 lib/soaspec/interpreter.rb
soaspec-0.1.8 lib/soaspec/interpreter.rb
soaspec-0.1.7 lib/soaspec/interpreter.rb