class TerraformEnterprise::API::Response

Wrapps the JSON-API HTTP response for easy access to data and resources

Attributes

body[R]
code[R]
data[R]
errors[R]
resource[R]
resources[R]

Public Class Methods

new(rest_client_response) click to toggle source
# File lib/terraform_enterprise/api/response.rb, line 10
def initialize(rest_client_response)
  @code = rest_client_response.code
  @body = parse(rest_client_response.body)

  return unless @body.is_a?(Hash)

  @data      = @body['data'] || @body
  @errors    = @body['errors'] || []
  @resource  = Resource.new(@body) if @data.is_a?(Hash)
  @resources = @data.map { |row| Resource.new('data' => row) } if @data.is_a?(Array)
end

Public Instance Methods

errors?() click to toggle source
# File lib/terraform_enterprise/api/response.rb, line 22
def errors?
  @body.is_a?(Hash) && @body['errors'] && @body['errors'].is_a?(Array)
end
success?() click to toggle source
# File lib/terraform_enterprise/api/response.rb, line 26
def success?
  @code.between?(200, 299)
end

Private Instance Methods

parse(str) click to toggle source
# File lib/terraform_enterprise/api/response.rb, line 32
def parse(str)
  JSON.parse(str)
rescue JSON::ParserError
  str
end