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

.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_key: config.fulfil_api_key,
    merchant_id: config.fulfil_merchant_id,
    logger: config.logger
  )
end

.configErpIntegration::Configuration

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

Returns:



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

def self.config
  ErpIntegration.config
end

.model_nameObject



54
55
56
# File 'lib/erp_integration/fulfil/api_resource.rb', line 54

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



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

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.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/erp_integration/fulfil/api_resource.rb', line 62

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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/erp_integration/fulfil/api_resource.rb', line 83

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



99
100
101
# File 'lib/erp_integration/fulfil/api_resource.rb', line 99

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)


124
125
126
127
128
129
130
# File 'lib/erp_integration/fulfil/api_resource.rb', line 124

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