Class: TerraformEnterprise::API::Response

Inherits:
Object
  • Object
show all
Includes:
TerraformEnterprise::API
Defined in:
lib/terraform_enterprise/api/response.rb

Overview

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

Constant Summary

Constants included from TerraformEnterprise::API

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rest_client_response) ⇒ Response

Returns a new instance of Response.



10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/terraform_enterprise/api/response.rb', line 9

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



9
10
11
# File 'lib/terraform_enterprise/api/response.rb', line 9

def code
  @code
end

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/terraform_enterprise/api/response.rb', line 9

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/terraform_enterprise/api/response.rb', line 9

def errors
  @errors
end

#resourceObject (readonly)

Returns the value of attribute resource.



9
10
11
# File 'lib/terraform_enterprise/api/response.rb', line 9

def resource
  @resource
end

#resourcesObject (readonly)

Returns the value of attribute resources.



9
10
11
# File 'lib/terraform_enterprise/api/response.rb', line 9

def resources
  @resources
end

Instance Method Details

#errors?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/terraform_enterprise/api/response.rb', line 22

def errors?
  @body.is_a?(Hash) && @body['errors'] && @body['errors'].is_a?(Array)
end

#success?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/terraform_enterprise/api/response.rb', line 26

def success?
  @code.between?(200, 299)
end