Sha256: e4bdecf55a2923b50aa3d14075f3623c92a096834eabfc6f36e2c6d46c9a672c

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

require "time"

module AkamaiCCU
  class Response
    BAD_STATUS = 400

    attr_reader :body, :title, :status, :detail, :support_id, :purge_id, :decribed_by, :completion_at

    def initialize(body, time = Time.now)
      @body = parse(body)
      @title = @body["title"]
      @status = @body.fetch("httpStatus") { @body.fetch("status", BAD_STATUS) }
      @detail = @body["detail"]
      @support_id = @body.fetch("supportId") { @body["requestId"] }
      @purge_id = @body["purgeId"]
      @described_by = @body.fetch("describedBy") { @body["type"] }
      @estimated_secs = @body["estimatedSeconds"]
      @completion_at = time + @estimated_secs.to_i if @estimated_secs
    end

    def successful?
      (@status.to_i / 100) == 2
    end

    def to_s
      %W[status=#{@status}].tap do |a|
        a << "title=#{@title}" if @title
        a << "detail=#{@detail}" if @detail
        a << "support_id=#{@support_id}" if @support_id
        a << "purge_id=#{@purge_id}" if @purge_id
        a << "described_by=#{@described_by}" if @described_by
        a << "copletion_at=#{@completion_at}" if @completion_at
      end.join("; ")
    end

    private def parse(body)
      return body if body.is_a? Hash
      JSON.parse(body)
    rescue JSON::ParserError => e
      { "detail"=>e.message }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
akamai_ccu-1.6.0 lib/akamai_ccu/response.rb
akamai_ccu-1.5.9 lib/akamai_ccu/response.rb