# Mejuri's ERP integration The ERP integration gem allows connecting to multiple ERP at the same time and query the data from these ERP's. It's currently only supporting [Fulfil](https://www.fulfil.io). ## Installation Add this line to your application's Gemfile: ```ruby gem 'erp_integration' ``` Or run the following command to add `ErpIntegration` to your Gemfile: ```shell $ bundle add erp_integration ``` ## Usage ### Configuration To configure the gem, create an initializer and add the following lines: ```erb # config/initializers/erp_integration.rb ErpIntegration.configure do |config| config.fulfil_api_keys = '' config.fulfil_base_url = '' end ``` You can configure multiple API keys, to enable rotation mechanism (see ["API key rotation"](README.md:L42)). ```erb # config/initializers/erp_integration.rb ErpIntegration.configure do |config| config.fulfil_api_keys = ['', ''] config.fulfil_base_url = '' end ``` ### API keys rotation To set up API key rotation, configure multiple keys. Every time a Fulfil client receives a `404` or `429`, it will rotate an API key. ```erb # config/initializers/erp_integration.rb ErpIntegration.configure do |config| config.fulfil_api_keys = ['', ''] ... end ``` You can also configure a separate group of API keys for each resource. From this point, `ErpIntegration::SalesOrder` will use custom API keys, and the other resources will continue to use the configured API keys pool. ```ruby ErpIntegration::SalesOrder.api_keys_pool = '' # or ErpIntegration::SalesOrder.api_keys_pool = ['', ''] ``` ### Rate limiting To manage the number of requests made to avoid rate limiting by the API provider, you can configure rate limiters in the ERP Integration gem. Each rate limiter should implement the `api_key_fragment` and `within_limit` methods. ### Setting Up Rate Limiters You can configure multiple rate limiters to be used for HTTP operations on the client. ```ruby # config/initializers/erp_integration.rb ErpIntegration.configure do |config| config.rate_limiters = [ MyRateLimiter.new(api_key_fragment: 'key1'), MyRateLimiter.new(api_key_fragment: 'key2') ] end ``` Each rate limiter must implement the following methods: - `api_key_fragment`: This method should return a string that is used to identify the rate limiter by the API key. - `within_limit`: This method should `yield` to the block if the rate limit is not exceeded. ### Example Rate Limiter Here is an example implementation of a rate limiter: ```ruby class MyRateLimiter def initialize(api_key_fragment:) @api_key_fragment = api_key_fragment end def api_key_fragment @api_key_fragment end def within_limit # Implement your rate limiting logic here yield end end ``` ### Supported Query Methods After configuring the gem, one can easily query all the available ERP resources from the connected third-parties. In all cases, the API will return a collection of resources. > **NOTE**: If you need to lookup an individual resource take a look at the "Supported finder methods" section. ```ruby $ ErpIntegration::SalesOrder.where(reference: 'MT1000SKX') => [] ``` There are also other type of `where` queries available: - `where_like` for case sensitive queries. - `where_ilike` for case insensitive queries. - `where_not` for non-equality queries. - `where_in` for inclusion queries. - `where_not_in` for exclusion queries. ### Supported Finder Methods The __Query Methods__ allow you to lookup a list of resources. The __Finder Methods__ allow you to lookup **an individual resource** from the API. - `#find` looks up a resource by id and raises `ErpIntegration::ResourceNotFound` when no result is found. - `#find_by` looks up a resource by a given set of query methods and returns `nil` when no result is found. - `#find_by!` looks up a resource by a given set of query methods and raises `ErpIntegration::ResourceNotFound` when no result is found. A difference between the query methods and the finder methods is the way it's executed. The finder methods are executed directly after they're called. The query methods will be lazily executed. ```ruby $ ErpIntegration::SalesOrder.find(100) # => # $ ErpIntegration::SalesOrder.find_by(code: "MT100") # => # $ ErpIntegration::SalesOrder.find_by!(code: "MT100") # => # ``` ### Supported Selection Methods By default, only the `id` will be added to ERP resources. However, one can use the `select` method to include more fields. ```ruby $ ErpIntegration::SalesOrder.select(:id, :reference).find_by(reference: 'MT1000SKX') # => ``` ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. ### Releasing **Prerequisites** To be able to publish a new release, you'll need to set up a Rubygems account. > To begin, you’ll need to create an account on RubyGems.org. Visit the sign up page and supply an email address that you control, a handle (username) and a password. > > After creating the account, use your email and password when pushing the gem. (RubyGems saves the credentials in ~/.gem/credentials for you so you only need to log in once.) > [Publishing to RubyGems.org](https://guides.rubygems.org/publishing/) It's important to note that you'll need the right privileges to publish the gem. Ask [@germansvriz](https://github.com/germansvriz) or [@stefanvermaas](https://github.com/stefanvermaas) to add you as a gem owner. **Publish a new version** 1. Run the prerelease script ```shell $ bin/prerelease 0.0.1 ``` 2. Create Pull Request 3. Merge it to develop 4. Run Release script ```shell $ bin/release 0.0.1 ``` We're following [semver](https://semver.org/) for the release process of this gem. Make sure to apply the correct semver version for a new release. > **NOTE**: You don't have to add a `v` to the version you want to release. The release script will handle that for you. ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/mejuri-inc/erp_integration. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/mejuri-inc/erp_integration/blob/main/CODE_OF_CONDUCT.md). ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). ## Code of Conduct Everyone interacting in the ErpIntegration project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mejuri-inc/erp_integration/blob/main/CODE_OF_CONDUCT.md).