Class: Wavefront::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront-sdk/response.rb

Overview

Every API path has its own response class, which allows us to provide a stable interface. If the API changes underneath us, the SDK will break in a predictable way, throwing a Wavefront::Exception::UnparseableResponse exception.

Most Wavefront::Response classes present the returned data in two parts, each accessible by dot notation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, status, debug = false) ⇒ Response

Create and return a Wavefront::Response object

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wavefront-sdk/response.rb', line 33

def initialize(json, status, debug = false)
  begin
    raw = json.empty? ? {} : JSON.parse(json, symbolize_names: true)
  rescue
    raw = { message: json, code: status }
  end

  @status = build_status(raw, status)
  @response = build_response(raw)

  p self if debug
rescue => e
  puts "could not parse:\n#{json}" if debug
  puts e.message if debug
  raise Wavefront::Exception::UnparseableResponse
end

Instance Attribute Details

#responseMap (readonly)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/wavefront-sdk/response.rb', line 21

class Response
  attr_reader :status, :response

  # Create and return a Wavefront::Response object
  # @param json [String] a raw response body from the Wavefront API
  # @param status [Integer] HTTP return code from the API
  # @param debug [Boolean] whether or not to print the exception
  #   message if one is thrown
  # @raise [Wavefront::Exception::UnparseableResponse] if the
  #   response cannot be parsed. This may be because the API
  #   has changed underneath us.
  #
  def initialize(json, status, debug = false)
    begin
      raw = json.empty? ? {} : JSON.parse(json, symbolize_names: true)
    rescue
      raw = { message: json, code: status }
    end

    @status = build_status(raw, status)
    @response = build_response(raw)

    p self if debug
  rescue => e
    puts "could not parse:\n#{json}" if debug
    puts e.message if debug
    raise Wavefront::Exception::UnparseableResponse
  end

  def build_status(raw, status)
    Wavefront::Type::Status.new(raw, status)
  end

  def build_response(raw)
    if raw.is_a?(Hash)
      if raw.key?(:response)
        if raw[:response].is_a?(Hash)
          Map(raw[:response])
        else
          raw[:response]
        end
      else
        Map.new(raw)
      end
    else
      Map.new
    end
  end
end

#statusWavefront::Types::Status (readonly)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/wavefront-sdk/response.rb', line 21

class Response
  attr_reader :status, :response

  # Create and return a Wavefront::Response object
  # @param json [String] a raw response body from the Wavefront API
  # @param status [Integer] HTTP return code from the API
  # @param debug [Boolean] whether or not to print the exception
  #   message if one is thrown
  # @raise [Wavefront::Exception::UnparseableResponse] if the
  #   response cannot be parsed. This may be because the API
  #   has changed underneath us.
  #
  def initialize(json, status, debug = false)
    begin
      raw = json.empty? ? {} : JSON.parse(json, symbolize_names: true)
    rescue
      raw = { message: json, code: status }
    end

    @status = build_status(raw, status)
    @response = build_response(raw)

    p self if debug
  rescue => e
    puts "could not parse:\n#{json}" if debug
    puts e.message if debug
    raise Wavefront::Exception::UnparseableResponse
  end

  def build_status(raw, status)
    Wavefront::Type::Status.new(raw, status)
  end

  def build_response(raw)
    if raw.is_a?(Hash)
      if raw.key?(:response)
        if raw[:response].is_a?(Hash)
          Map(raw[:response])
        else
          raw[:response]
        end
      else
        Map.new(raw)
      end
    else
      Map.new
    end
  end
end

Instance Method Details

#build_response(raw) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wavefront-sdk/response.rb', line 54

def build_response(raw)
  if raw.is_a?(Hash)
    if raw.key?(:response)
      if raw[:response].is_a?(Hash)
        Map(raw[:response])
      else
        raw[:response]
      end
    else
      Map.new(raw)
    end
  else
    Map.new
  end
end

#build_status(raw, status) ⇒ Object



50
51
52
# File 'lib/wavefront-sdk/response.rb', line 50

def build_status(raw, status)
  Wavefront::Type::Status.new(raw, status)
end