Sha256: 6d0017fbd95558ca8da9a6bacea0bd66d9805b4fc8fe58012f5c4ae1764c958b

Contents?: true

Size: 715 Bytes

Versions: 3

Compression:

Stored size: 715 Bytes

Contents

require 'json'
require 'hashie'

module Rapidash
  class Response
    class << self
      def new(response)
        return nil unless response.body
        return nil if response.body.empty?
        return nil if response.body == "null"
        type = response.headers["content-type"]
        body = JSON.parse(response.body)
        if body.kind_of?(Hash)
          return Hashie::Mash.new(body)
        elsif body.kind_of?(Array)
          output = []
          body.each do |el|
            output << Hashie::Mash.new(el)
          end
          return output
        end
      rescue JSON::ParserError => e
        raise ParseError.new("Failed to parse content for type: #{type}")
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rapidash-0.2.2 lib/rapidash/response.rb
rapidash-0.2.1 lib/rapidash/response.rb
rapidash-0.2.0 lib/rapidash/response.rb