Class: ErpIntegration::Fulfil::ApiResource

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Context, FinderMethods, PaginationMethods, Persistence, QueryMethods
Defined in:
lib/erp_integration/fulfil/api_resource.rb

Constant Summary

Constants included from PaginationMethods

PaginationMethods::DEFAULT_LIMIT, PaginationMethods::DEFAULT_OFFSET, PaginationMethods::MAX_LIMIT

Instance Attribute Summary collapse

Attributes included from QueryMethods

#or_clauses, #selected_fields, #where_clauses

Attributes included from PaginationMethods

#limit_value, #offset_value, #page_number

Class Method Summary collapse

Instance Method Summary collapse

Methods included from QueryMethods

#or, #or!, #select, #select!, #where, #where!, #where_domain, #where_ilike, #where_in, #where_less_or_equal_to, #where_less_than, #where_like, #where_more_or_equal_to, #where_more_than, #where_not, #where_not_in

Methods included from Persistence

#create, #destroy, #update

Methods included from PaginationMethods

#limit, #limit!, #offset, #offset!, #page, #page!

Methods included from FinderMethods

#find, #find_by, #find_by!

Methods included from Context

#context?, #with_context

Constructor Details

#initialize(resource_klass) ⇒ ApiResource

Returns a new instance of ApiResource.



22
23
24
# File 'lib/erp_integration/fulfil/api_resource.rb', line 22

def initialize(resource_klass)
  @resource_klass = resource_klass
end

Instance Attribute Details

#resource_klassObject

Returns the value of attribute resource_klass.



19
20
21
# File 'lib/erp_integration/fulfil/api_resource.rb', line 19

def resource_klass
  @resource_klass
end

Class Method Details

.api_keys_poolErpIntegration::ApiKeysPool

The ‘api_keys_pool` exposes the API keys pool to the class.

Returns:



38
39
40
# File 'lib/erp_integration/fulfil/api_resource.rb', line 38

def self.api_keys_pool
  @api_keys_pool ||= ApiKeysPool.new.tap { |pool| pool.api_keys = config.fulfil_api_keys }
end

.api_keys_pool=(fulfil_api_keys) ⇒ ErpIntegration::ApiKeysPool

Allows setting a new API keys pool for the ‘ApiResource`

Returns:



44
45
46
# File 'lib/erp_integration/fulfil/api_resource.rb', line 44

def self.api_keys_pool=(fulfil_api_keys)
  @api_keys_pool = ApiKeysPool.new.tap { |pool| pool.api_keys = fulfil_api_keys }
end

.clientErpIntegration::Fulfil::Client

The ‘client` exposes the `ErpIntegration::Fulfil::Client` to the class.

Returns:



28
29
30
31
32
33
34
# File 'lib/erp_integration/fulfil/api_resource.rb', line 28

def self.client
  Client.new(
    api_keys_pool: api_keys_pool,
    base_url: config.fulfil_base_url,
    logger: config.logger
  )
end

.configErpIntegration::Configuration

The ‘config` exposes the gem’s configuration to the ‘ApiResource`.

Returns:



50
51
52
# File 'lib/erp_integration/fulfil/api_resource.rb', line 50

def self.config
  ErpIntegration.config
end

.model_nameObject



66
67
68
# File 'lib/erp_integration/fulfil/api_resource.rb', line 66

def self.model_name
  instance_variable_get(:@model_name)
end

.model_name=(name) ⇒ String

Fulfil doesn’t use logical naming conventions. However, we do need the name of a model to build the resource URI.

By manually setting the model name, we allow the ‘Fulfil::Connection` module to connect to the correct API endpoint.

Parameters:

  • name (String)

    The logical path name in Fulfil.

Returns:

  • (String)

    The model name



62
63
64
# File 'lib/erp_integration/fulfil/api_resource.rb', line 62

def self.model_name=(name)
  instance_variable_set(:@model_name, name)
end

Instance Method Details

#allArray

The ‘where` and `includes` methods lazyly build a search/read query for Fulfil. By calling `all`, the prepared search/read query will actually be executed and the results will be fetched.

Returns:

  • (Array)

    An enumerable collection object with all API results.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/erp_integration/fulfil/api_resource.rb', line 74

def all
  return @results if defined?(@results)

  @results =
    client.put(
      api_resource_path,
      Query.new(
        fields: selected_fields,
        filters: where_clauses,
        alternative_filters: or_clauses,
        limit: limit_value,
        offset: calculated_offset
      )
    ).map { |item| resource_klass.new(item) }
end

#countInteger

As with the ‘all` method, the `query methods` lazyly build a search/read or search/count query for Fulfil. By calling `count`, the prepared search/count will actually be executed and the result will be fetched

Returns:

  • (Integer)

    The count of records that match with the query in Fulfil



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/erp_integration/fulfil/api_resource.rb', line 95

def count
  return @count if defined?(@count)

  @count =
    client.put(
      "model/#{model_name}/search_count",
      Query.new(
        fields: nil,
        filters: where_clauses,
        alternative_filters: or_clauses
      ).to_h.except(:fields)
    )
end

#each(&block) ⇒ Object

The ‘each` method turns the `ApiResource` instance into an enumerable object. For more information, see ruby-doc.org/core-3.0.2/Enumerable.html



111
112
113
# File 'lib/erp_integration/fulfil/api_resource.rb', line 111

def each(&block)
  all.each(&block)
end

#find_each {|results| ... } ⇒ nil

Iterates over each page of results and yields it for processing.

It fetches each page of results and yields it to the provided block for processing. The loop continues until there are no more results to process.

ErpIntegration::SalesOrder.select(:id, :total_amount, :state).find_each do |orders|

orders.each do |order|
  puts "#{order.id},$#{order.total_amount['decimal']},#{order.state}"
end

end

> 5887403,$478.12,draft

> 5884252,$1497.03,draft

> 5742565,$78.75,draft

Yields:

  • (results)

    Yields the current page of results to the provided block for processing.

Yield Parameters:

  • results (Array)

    An array of results for the current page.

Returns:

  • (nil)


136
137
138
139
140
141
142
# File 'lib/erp_integration/fulfil/api_resource.rb', line 136

def find_each
  page = 1
  while (results = clone.page(page)).any?
    yield results
    page += 1
  end
end