lib/akamai_ccu/response.rb in akamai_ccu-1.5.4 vs lib/akamai_ccu/response.rb in akamai_ccu-1.5.5

- old
+ new

@@ -2,85 +2,40 @@ module AkamaiCCU class Response BAD_STATUS = 400 - def self.factory(body) - response = new(body) - case response - when ->(res) { res.successful? } - Ack.new(body) - else - Error.new(body) - end - end + attr_reader :body, :title, :status, :detail, :support_id, :purge_id, :decribed_by, :completion_at - attr_reader :body, :status, :detail - - def initialize(body = {}) + 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 - private def parse(body) - return body if body.is_a? Hash - JSON.parse(body) - end - end - - class Error < Response - attr_reader :type, :title, :request_id, :instance, :method, :server_ip, :client_ip - - def initialize(body) - super(body) - @type = @body["type"] - @title = @body["title"] - @request_id = @body["requestId"] - @instance = @body["instance"] - @method = @body["method"] - @serverIp = @body["serverIp"] - @clientIp = @body["clientIp"] - @requested_at = @body["requestTime"] - end - - def requested_at - return nil unless @requested_at - Time.parse(@requested_at) - end - def to_s %W[status=#{@status}].tap do |a| a << "title=#{@title}" if @title a << "detail=#{@detail}" if @detail - a << "request_id=#{@request_id}" if @request_id - a << "method=#{@method}" if @method - a << "requested_at=#{@requested_at}" if @requested_at + 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 - end - class Ack < Response - attr_reader :purge_id, :estimated_secs, :support_id, :completion_at - - def initialize(body, time = Time.now) - super(body) - @purge_id = @body["purgeId"] - @estimated_secs = @body["estimatedSeconds"] - @support_id = @body["supportId"] - @completion_at = time + @estimated_secs.to_i - end - - def to_s - %W[status=#{@status}].tap do |a| - a << "detail=#{@detail}" if @detail - a << "purge_id=#{@purge_id}" if @purge_id - a << "support_id=#{@support_id}" if @support_id - a << "copletion_at=#{AkamaiCCU.format_utc(@completion_at)}" if @completion_at - end.join("; ") + private def parse(body) + return body if body.is_a? Hash + JSON.parse(body) end end end