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.1.5 lib/soaspec/interpreter.rb
soaspec-0.1.4 lib/soaspec/interpreter.rb
soaspec-0.1.3 lib/soaspec/interpreter.rb
soaspec-0.1.2 lib/soaspec/interpreter.rb
soaspec-0.1.0 lib/soaspec/interpreter.rb
soaspec-0.0.89 lib/soaspec/interpreter.rb
soaspec-0.0.87 lib/soaspec/interpreter.rb
soaspec-0.0.86 lib/soaspec/interpreter.rb