Sha256: 5fffe4fe08bae02fcb848c89c14661f03d72fff8867cc21b15a1ba90de43d0d7

Contents?: true

Size: 900 Bytes

Versions: 1

Compression:

Stored size: 900 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
          :string
        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

1 entries across 1 versions & 1 rubygems

Version Path
soaspec-0.2.22 lib/soaspec/interpreter.rb