Sha256: e14ab616bdffdb591b4dc741acbf8a60a38bf0d0b94b89a7bbe83c9ff34152d1

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

module Tickethub
  class Response
    attr_reader :response, :body, :headers, :parser

    def initialize(response, parser = nil)
      @response = response
      @body     = response.body
      @headers  = Headers.new(response.to_hash)
      @format   = Formats.for(headers.content_type)
      @parser ||= @format && @format.new
    end

    def as_json
      decoded
    end

    def to_json(*)
      as_json.to_json
    end

    def code
      response.code.to_i
    end

    alias_method :status, :code

    def decoded
      @decoded ||= parser ? parser.decode(body) : body
      code == 202 ? Tickethub::Job.load(Tickethub.endpoint, @decoded) : @decoded
    end

    def respond_to?(name)
      super || decoded.respond_to?(name) || response.respond_to?(name)
    end

    protected

    def method_missing(name, *args, &block)
      if decoded.respond_to?(name)
        decoded.send(name, *args, &block)
      elsif response.respond_to?(name)
        response.send(name, *args, &block)
      else
        super
      end
    end
  end
end

require_relative 'response/headers'

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
tickethub-0.3.7 lib/tickethub/response.rb
tickethub-0.3.6 lib/tickethub/response.rb
tickethub-0.3.4 lib/tickethub/response.rb
tickethub-0.3.3 lib/tickethub/response.rb
tickethub-0.3.2 lib/tickethub/response.rb
tickethub-0.3.1 lib/tickethub/response.rb