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::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
-
.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.
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_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
.client ⇒ ErpIntegration::Fulfil::Client
The ‘client` exposes the `ErpIntegration::Fulfil::Client` to the class.
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 |
.config ⇒ ErpIntegration::Configuration
The ‘config` exposes the gem’s configuration to the ‘ApiResource`.
38 39 40 |
# File 'lib/erp_integration/fulfil/api_resource.rb', line 38 def self.config ErpIntegration.config end |
.model_name ⇒ Object
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.
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
#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.
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 |
#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
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
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 |