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 rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

Parameters:

  • json (String)

    a raw response body from the Wavefront API

  • status (Integer)

    HTTP return code from the API

  • debug (Boolean) (defaults to: false)

    whether or not to print the exception message if one is thrown

Raises:



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

def initialize(json, status, debug = false)
  raw = raw_response(json, status)
  @status = build_status(raw, status)
  @response = build_response(raw)

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

Instance Attribute Details

#responseMap (readonly)

Returns the response from the API turned into a Map, which allows.

Returns:

  • (Map)

    the response from the API turned into a Map, which allows



20
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
# File 'lib/wavefront-sdk/response.rb', line 20

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.
  #
  # rubocop:disable Metrics/CyclomaticComplexity
  # rubocop:disable Metrics/PerceivedComplexity
  def initialize(json, status, debug = false)
    raw = raw_response(json, status)
    @status = build_status(raw, status)
    @response = build_response(raw)

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

  def raw_response(json, status)
    json.empty? ? {} : JSON.parse(json, symbolize_names: true)
  rescue StandardError
    { message: json, code: status }
  end

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

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

#statusWavefront::Types::Status (readonly)

Returns:

  • (Wavefront::Types::Status)


20
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
# File 'lib/wavefront-sdk/response.rb', line 20

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.
  #
  # rubocop:disable Metrics/CyclomaticComplexity
  # rubocop:disable Metrics/PerceivedComplexity
  def initialize(json, status, debug = false)
    raw = raw_response(json, status)
    @status = build_status(raw, status)
    @response = build_response(raw)

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

  def raw_response(json, status)
    json.empty? ? {} : JSON.parse(json, symbolize_names: true)
  rescue StandardError
    { message: json, code: status }
  end

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

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

Instance Method Details

#build_response(raw) ⇒ Object



56
57
58
59
60
61
# File 'lib/wavefront-sdk/response.rb', line 56

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

#build_status(raw, status) ⇒ Object



52
53
54
# File 'lib/wavefront-sdk/response.rb', line 52

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

#raw_response(json, status) ⇒ Object



46
47
48
49
50
# File 'lib/wavefront-sdk/response.rb', line 46

def raw_response(json, status)
  json.empty? ? {} : JSON.parse(json, symbolize_names: true)
rescue StandardError
  { message: json, code: status }
end