Class: ErpIntegration::Fulfil::ApiResource
- Inherits:
-
Object
- Object
- ErpIntegration::Fulfil::ApiResource
- Includes:
- Enumerable, Context, FinderMethods, PaginationMethods, Persistence, QueryMethods
- Defined in:
- lib/erp_integration/fulfil/api_resource.rb
Direct Known Subclasses
Resources::BillOfMaterial, Resources::BillOfMaterialInput, Resources::BillOfMaterialOutput, Resources::BoxType, Resources::Carrier, Resources::CarrierService, Resources::ChannelListing, Resources::Country, Resources::CustomerShipment, Resources::CustomerShipmentReturn, Resources::GiftCard, Resources::InternalShipment, Resources::Location, Resources::PartyAddress, Resources::Product, Resources::ProductCategory, Resources::ProductOption, Resources::ProductTemplate, Resources::ProductionOrder, Resources::PurchaseOrder, Resources::PurchaseOrderLine, Resources::PurchaseProductSupplier, Resources::PurchaseRequest, Resources::SalesLineOption, Resources::SalesOrder, Resources::SalesOrderLine, Resources::SalesReturnReason, Resources::StockBinTransfer, Resources::StockMove, Resources::SupplierShipment, Resources::Task, Resources::TrackingNumber, Resources::Webhook
Constant Summary
Constants included from PaginationMethods
PaginationMethods::DEFAULT_LIMIT, PaginationMethods::DEFAULT_OFFSET, PaginationMethods::MAX_LIMIT
Instance Attribute Summary collapse
-
#resource_klass ⇒ Object
Returns the value of attribute resource_klass.
Attributes included from QueryMethods
#or_clauses, #selected_fields, #where_clauses
Attributes included from PaginationMethods
#limit_value, #offset_value, #page_number
Class Method Summary collapse
-
.api_keys_pool ⇒ ErpIntegration::ApiKeysPool
The ‘api_keys_pool` exposes the API keys pool to the class.
-
.api_keys_pool=(fulfil_api_keys) ⇒ ErpIntegration::ApiKeysPool
Allows setting a new API keys pool for the ‘ApiResource`.
-
.client ⇒ ErpIntegration::Fulfil::Client
The ‘client` exposes the `ErpIntegration::Fulfil::Client` to the class.
-
.config ⇒ ErpIntegration::Configuration
The ‘config` exposes the gem’s configuration to the ‘ApiResource`.
- .model_name ⇒ Object
-
.model_name=(name) ⇒ String
Fulfil doesn’t use logical naming conventions.
Instance Method Summary collapse
-
#all ⇒ Array
The ‘where` and `includes` methods lazyly build a search/read query for Fulfil.
-
#count ⇒ Integer
As with the ‘all` method, the `query methods` lazyly build a search/read or search/count query for Fulfil.
-
#each(&block) ⇒ Object
The ‘each` method turns the `ApiResource` instance into an enumerable object.
-
#find_each {|results| ... } ⇒ nil
Iterates over each page of results and yields it for processing.
-
#initialize(resource_klass) ⇒ ApiResource
constructor
A new instance of ApiResource.
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
Methods included from PaginationMethods
#limit, #limit!, #offset, #offset!, #page, #page!
Methods included from FinderMethods
Methods included from Context
Constructor Details
#initialize(resource_klass) ⇒ ApiResource
Returns a new instance of ApiResource.
23 24 25 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 23 def initialize(resource_klass) @resource_klass = resource_klass end |
Instance Attribute Details
#resource_klass ⇒ Object
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_pool ⇒ ErpIntegration::ApiKeysPool
The ‘api_keys_pool` exposes the API keys pool to the class.
39 40 41 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 39 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`
45 46 47 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 45 def self.api_keys_pool=(fulfil_api_keys) @api_keys_pool = ApiKeysPool.new.tap { |pool| pool.api_keys = fulfil_api_keys } end |
.client ⇒ ErpIntegration::Fulfil::Client
The ‘client` exposes the `ErpIntegration::Fulfil::Client` to the class.
29 30 31 32 33 34 35 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 29 def self.client Client.new( api_keys_pool: api_keys_pool, base_url: config.fulfil_base_url, logger: config.logger ) end |
.config ⇒ ErpIntegration::Configuration
The ‘config` exposes the gem’s configuration to the ‘ApiResource`.
51 52 53 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 51 def self.config ErpIntegration.config end |
.model_name ⇒ Object
67 68 69 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 67 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.
63 64 65 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 63 def self.model_name=(name) instance_variable_set(:@model_name, name) end |
Instance Method Details
#all ⇒ Array
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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 75 def all return @all if defined?(@all) @all = 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 |
#count ⇒ Integer
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
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 96 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
112 113 114 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 112 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
137 138 139 140 141 142 143 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 137 def find_each page = 1 while (results = clone.page(page)).any? yield results page += 1 end end |