Class: EZDyn::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ezdyn/response.rb

Overview

Abstraction of a Dyn REST API response.

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ezdyn/response.rb', line 5

def initialize(response)
  @response = response
  EZDyn.debug { "API RESPONSE: #{@response.body}" }
  if @response.body =~ %r{^/REST/Job/[0-9]+$}
    EZDyn.debug { "Response delayed! Try again later!" }
    @raw = {
      "status" => "delayed",
      "job_id" => @response.body.split('/').last,
      "msgs" => [],
      "data" => {},
    }
  else
    @raw = JSON.parse(@response.body)
  end
end

Instance Method Details

#dataHash

The payload data from the response.

Returns:

  • (Hash)

    The payload data of the response.



53
54
55
# File 'lib/ezdyn/response.rb', line 53

def data
  @raw["data"]
end

#delayed?Boolean

Do we have to wait for the response?

Returns:

  • (Boolean)

    Returns true if the response is delayed and you must wait and try again.



39
40
41
# File 'lib/ezdyn/response.rb', line 39

def delayed?
  self.status == "delayed"
end

#error_codesArray<String>

All error codes returned by the response.

Returns:

  • (Array<String>)

    An array of error codes returned by the response.



78
79
80
# File 'lib/ezdyn/response.rb', line 78

def error_codes
  @raw["msgs"].collect { |m| m["ERR_CD"] }.compact.uniq
end

#full_messageString

Full version of the descriptive response message with log levels and error codes.

Returns:

  • (String)

    Full descriptive message of the response with error codes and log levels.



62
63
64
65
66
# File 'lib/ezdyn/response.rb', line 62

def full_message
  @raw["msgs"].collect do |msg|
    "[#{msg["SOURCE"]}] #{msg["LVL"]} (#{msg["ERR_CD"]}): #{msg[msg["LVL"]]}"
  end.join("\n")
end

#job_idString

The job ID returned by the response.

Returns:

  • (String)

    The job ID of the response.



46
47
48
# File 'lib/ezdyn/response.rb', line 46

def job_id
  @raw["job_id"]
end

#simple_messageString

The simple descriptive message of the response from the API.

Returns:

  • (String)

    Descriptive message of the response.



71
72
73
# File 'lib/ezdyn/response.rb', line 71

def simple_message
  @raw["msgs"].collect { |m| m[m["LVL"]] }.join("\n")
end

#statusString

Returns the status message of the response.

Returns:

  • (String)

    The status field of the response.



24
25
26
# File 'lib/ezdyn/response.rb', line 24

def status
  @raw["status"]
end

#success?Boolean

Was the response successful?

Returns:

  • (Boolean)

    Returns true if the response was successful.



31
32
33
# File 'lib/ezdyn/response.rb', line 31

def success?
  self.status == "success"
end