Class: Chemlab::Resource

Inherits:
Object
  • Object
show all
Extended by:
SingleForwardable
Includes:
ApiFabricator
Defined in:
lib/chemlab/resource.rb

Defined Under Namespace

Classes: DSL

Constant Summary collapse

NoValueError =
Class.new(RuntimeError)

Constants included from ApiFabricator

ApiFabricator::InternalServerError, ApiFabricator::ResourceFabricationFailedError, ApiFabricator::ResourceNotDeletedError, ApiFabricator::ResourceNotFoundError, ApiFabricator::ResourceQueryError, ApiFabricator::ResourceURLMissingError, ApiFabricator::ResourceUpdateFailedError

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

Attributes included from ApiFabricator

#api_resource, #api_response, #user

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiFabricator

#api_put, #api_support?, #eager_load_api_client!, #fabricate_via_api!, #reload!, #remove_via_api!

Methods included from Support::API

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

Constructor Details

#initialize(api_client) ⇒ Resource

Returns a new instance of Resource.



14
15
16
# File 'lib/chemlab/resource.rb', line 14

def initialize(api_client)
  @api_client = api_client
end

Class Method Details

.fabricate!(*args, &prepare_block) ⇒ Object



18
19
20
21
22
# File 'lib/chemlab/resource.rb', line 18

def self.fabricate!(*args, &prepare_block)
  fabricate_via_api!(*args, &prepare_block)
rescue NotImplementedError
  fabricate_via_browser_ui!(*args, &prepare_block)
end

.fabricate_via_api!(*args, &prepare_block) ⇒ Object

Raises:

  • (NotImplementedError)


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chemlab/resource.rb', line 36

def self.fabricate_via_api!(*args, &prepare_block)
  options = args.extract_options!
  resource = options.fetch(:resource) { new }
  parents = options.fetch(:parents) { [] }

  raise NotImplementedError unless resource.api_support?

  resource.eager_load_api_client!

  do_fabricate!(resource: resource, prepare_block: prepare_block, parents: parents) do
    log_fabrication(:api, resource, parents, args) { resource.fabricate_via_api! }
  end
end

.fabricate_via_browser_ui!(*args, &prepare_block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chemlab/resource.rb', line 24

def self.fabricate_via_browser_ui!(*args, &prepare_block)
  options = args.extract_options!
  resource = options.fetch(:resource) { new }
  parents = options.fetch(:parents) { [] }

  do_fabricate!(resource: resource, prepare_block: prepare_block, parents: parents) do
    log_fabrication(:browser_ui, resource, parents, args) { resource.fabricate!(*args) }

    current_url
  end
end

.remove_via_api!(*args, &prepare_block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chemlab/resource.rb', line 50

def self.remove_via_api!(*args, &prepare_block)
  options = args.extract_options!
  resource = options.fetch(:resource) { new }
  parents = options.fetch(:parents) { [] }

  resource.eager_load_api_client!

  do_fabricate!(resource: resource, prepare_block: prepare_block, parents: parents) do
    log_fabrication(:api, resource, parents, args) { resource.remove_via_api! }
  end
end

Instance Method Details

#fabricate!(*_args) ⇒ Object

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/chemlab/resource.rb', line 62

def fabricate!(*_args)
  raise NotImplementedError
end

#populate(*attributes) ⇒ Object



78
79
80
# File 'lib/chemlab/resource.rb', line 78

def populate(*attributes)
  attributes.each(&method(:public_send))
end

#visit!Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chemlab/resource.rb', line 66

def visit!
  Runtime::Logger.debug(%Q[Visiting #{self.class.name} at "#{web_url}"])

  # Just in case an async action is not yet complete
  Support::WaitForRequests.wait_for_requests

  Support::Retrier.retry_until do
    visit(web_url)
    wait_until { current_url.include?(URI.parse(web_url).path.split('/').last || web_url) }
  end
end

#wait_until(max_duration: 60, sleep_interval: 0.1) ⇒ Object



82
83
84
85
86
# File 'lib/chemlab/resource.rb', line 82

def wait_until(max_duration: 60, sleep_interval: 0.1)
  Support::Waiter.wait_until(max_duration: max_duration, sleep_interval: sleep_interval) do
    yield
  end
end