Module: Chemlab::ApiFabricator

Includes:
Support::API
Included in:
Resource
Defined in:
lib/chemlab/api_fabricator.rb

Overview

API Fabricator

Constant Summary collapse

ResourceFabricationFailedError =
Class.new(RuntimeError)
ResourceNotDeletedError =
Class.new(RuntimeError)
ResourceNotFoundError =
Class.new(RuntimeError)
ResourceQueryError =
Class.new(RuntimeError)
ResourceUpdateFailedError =
Class.new(RuntimeError)
ResourceURLMissingError =
Class.new(RuntimeError)
InternalServerError =
Class.new(RuntimeError)

Constants included from Support::API

Support::API::HTTP_STATUS_ACCEPTED, Support::API::HTTP_STATUS_CREATED, Support::API::HTTP_STATUS_NO_CONTENT, Support::API::HTTP_STATUS_OK, Support::API::HTTP_STATUS_SERVER_ERROR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::API

#delete, #get, #head, #parse_body, #post, #put, #return_response_or_raise

Instance Attribute Details

#api_client=(value) ⇒ Object

Sets the attribute api_client

Parameters:

  • value

    the value to set the attribute api_client to.



17
18
19
# File 'lib/chemlab/api_fabricator.rb', line 17

def api_client=(value)
  @api_client = value
end

#api_resourceObject

Returns the value of attribute api_resource.



16
17
18
# File 'lib/chemlab/api_fabricator.rb', line 16

def api_resource
  @api_resource
end

#api_responseObject

Returns the value of attribute api_response.



16
17
18
# File 'lib/chemlab/api_fabricator.rb', line 16

def api_response
  @api_response
end

#userObject

Returns the value of attribute user.



18
19
20
# File 'lib/chemlab/api_fabricator.rb', line 18

def user
  @user
end

Instance Method Details

#api_put(body = api_put_body) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chemlab/api_fabricator.rb', line 59

def api_put(body = api_put_body)
  response = put(
    Runtime::API::Request.new(api_client, api_put_path).url,
    body)

  unless response.code == HTTP_STATUS_OK
    raise ResourceFabricationFailedError, "Updating #{self.class.name} using the API failed (#{response.code}) with `#{response}`."
  end

  process_api_response(parse_body(response))
end

#api_support?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/chemlab/api_fabricator.rb', line 20

def api_support?
  respond_to?(:api_get_path) &&
    (respond_to?(:api_post_path) && respond_to?(:api_post_body)) ||
    (respond_to?(:api_put_path) && respond_to?(:api_put_body))
end

#eager_load_api_client!Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/chemlab/api_fabricator.rb', line 44

def eager_load_api_client!
  return unless api_client.nil?

  api_client.tap do |client|
    # Eager-load the API client so that the personal token creation isn't
    # taken in account in the actual resource creation timing.
    client.user = user
    client.personal_access_token
  end
end

#fabricate_via_api!Object



26
27
28
29
30
31
32
# File 'lib/chemlab/api_fabricator.rb', line 26

def fabricate_via_api!
  unless api_support?
    raise NotImplementedError, "Resource #{self.class.name} does not support fabrication via the API!"
  end

  resource_web_url(api_post)
end

#reload!Object



34
35
36
37
38
# File 'lib/chemlab/api_fabricator.rb', line 34

def reload!
  api_get

  self
end

#remove_via_api!Object



40
41
42
# File 'lib/chemlab/api_fabricator.rb', line 40

def remove_via_api!
  api_delete
end