Sha256: 952290ea91819c0a0a0fd6e6907c0e72ad35307ea94bf9b564b89ac4c047c87a

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 KB

Contents

module RestClient
  module Mixin
    module Response
      attr_reader :net_http_res

      # HTTP status code, always 200 since RestClient throws exceptions for
      # other codes.
      def code
        @code ||= @net_http_res.code.to_i
      end

      # A hash of the headers, beautified with symbols and underscores.
      # e.g. "Content-type" will become :content_type.
      def headers
        @headers ||= self.class.beautify_headers(@net_http_res.to_hash)
      end

      # The raw headers.
      def raw_headers
        @raw_headers ||= @net_http_res.to_hash
      end

      # Hash of cookies extracted from response headers
      def cookies
        @cookies ||= (self.headers[:set_cookie] || "").split('; ').inject({}) do |out, raw_c|
          key, val = raw_c.split('=')
          unless %w(expires domain path secure).member?(key)
            out[key] = val
          end
          out
        end
      end

      def self.included(receiver)
        receiver.extend(RestClient::Mixin::Response::ClassMethods)
      end

      module ClassMethods
        def beautify_headers(headers)
          headers.inject({}) do |out, (key, value)|
            out[key.gsub(/-/, '_').downcase.to_sym] = value.first
            out
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
esod-client-0.2.1 lib/rest-client-1.2.0/lib/restclient/mixin/response.rb
esod-client-0.2.0 lib/rest-client-1.2.0/lib/restclient/mixin/response.rb
esod-client-0.1.1 lib/rest-client-1.2.0/lib/restclient/mixin/response.rb
esod-client-0.1.0 lib/rest-client-1.2.0/lib/restclient/mixin/response.rb
rest-client-1.2.0 lib/restclient/mixin/response.rb