Sha256: cf514cbe0e9cd591f380762675425e9e9d5a29c366c89c2d28d96c5711dea0d0

Contents?: true

Size: 864 Bytes

Versions: 4

Compression:

Stored size: 864 Bytes

Contents

require "multi_json"

module Acter
  class Response
    def initialize(status, headers, body)
      @status = status
      @success = (200..299).include?(status[/\d+/].to_i)
      @headers = headers.sort.map {|a| a.join(": ") }
      @body = case body
        when String
          @body_is_json = false
          body
        else
          @body_is_json = true
          MultiJson.dump(body, pretty: true)
        end
    end

    def self.new_from_faraday(faraday_response)
      status_string = "#{faraday_response.status} #{faraday_response.reason_phrase}"
      new(status_string, faraday_response.headers, faraday_response.body)
    end

    attr_reader :status, :success, :headers, :body, :body_is_json
    alias_method :success?, :success
    alias_method :body_is_json?, :body_is_json
    remove_method :success
    remove_method :body_is_json
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
acter-0.2.0 lib/acter/response.rb
acter-0.1.3 lib/acter/response.rb
acter-0.1.2 lib/acter/response.rb
acter-0.1.1 lib/acter/response.rb