Module: FulfilApi::Relation::Loadable

Included in:
FulfilApi::Relation
Defined in:
lib/fulfil_api/relation/loadable.rb

Overview

The Loadable extends the relation by

adding methods to load, reload and identify loaded resources from Fulfil's
API endpoints.

By default, all HTTP requests to Fulfil are delayed until they’re directly

or indirectly requested by the user of the gem. This way, we ensure that
we only request data when we need to.

Instance Method Summary collapse

Instance Method Details

#loadtrue, false

Loads resources from Fulfil’s API based on the current filters, fields,

offset, and limits if they haven't been loaded yet.

Requires that FulfilApi::Relation#model_name is set; raises an exception if it’s not.

Returns:

  • (true, false)

    True if the resources were loaded successfully.

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fulfil_api/relation/loadable.rb', line 19

def load # rubocop:disable Metrics/MethodLength
  return true if loaded?
  raise FulfilApi::Resource::ModelNameMissing if model_name.nil?

  response = FulfilApi.client.put(
    "/model/#{model_name}/search_read",
    body: {
      filters: conditions,
      fields: fields,
      limit: request_limit,
      offset: request_offset
    }.compact_blank
  )

  # NOTE: The /search_read endpoint will always be an array. Therefore, we're
  #   always looping over the response values.
  @resources = response.map do |attributes|
    @resource_klass.new(attributes.merge(model_name: model_name))
  end

  @loaded = true
end

#loaded?true, false

Checks whether the resources have been loaded to avoid repeated API calls when

using enumerable methods.

Returns:

  • (true, false)

    True if the resources are already loaded.



46
47
48
# File 'lib/fulfil_api/relation/loadable.rb', line 46

def loaded?
  @loaded
end

#reloadtrue, false

Reloads the resources from Fulfil’s API by resetting the @loaded flag.

Returns:

  • (true, false)

    True if the resources were successfully reloaded.



53
54
55
56
# File 'lib/fulfil_api/relation/loadable.rb', line 53

def reload
  @loaded = false
  load
end