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.

Installation

Add this line to your application's Gemfile:

  gem 'erp_integration'

Or run the following command to add ErpIntegration to your Gemfile:

  $ bundle add erp_integration

Usage

Configuration

To configure the gem, create an initializer and add the following lines:

# config/initializers/erp_integration.rb
ErpIntegration.configure do |config|
  config.fulfil_api_keys = '<your-api-key>'
  config.fulfil_base_url = '<your-base-url>'
end

You can configure multiple API keys, to enable rotation mechanism (see "API key rotation").

# config/initializers/erp_integration.rb
ErpIntegration.configure do |config|
  config.fulfil_api_keys = ['<your-api-key1>', '<your-api-key2>']
  config.fulfil_base_url = '<your-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.

You can set the api_key_rotation_threshold option to automatically change the API key when requests are running low. If you don't want to use the x-ratelimit-remaining response header for rotation, set the api_key_rotation_threshold to 0 or ignore it. The minimum threshold is 1, meaning the key will rotate when there are 0 remaining requests.

# config/initializers/erp_integration.rb
ErpIntegration.configure do |config|
  config.fulfil_api_keys = ['<your-api-key1>', '<your-api-key2>']
  config.api_key_rotation_threshold = 1
  ...
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.

ErpIntegration::SalesOrder.api_keys_pool = '<your-api-key>'
# or
ErpIntegration::SalesOrder.api_keys_pool = ['<your-api-key1>', '<your-api-key2>']

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.

# 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:

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.

$ ErpIntegration::SalesOrder.where(reference: 'MT1000SKX')
=> [<ErpIntegration::SalesOrder @id=100 />]

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.

$ ErpIntegration::SalesOrder.find(100)
# => #<ErpIntegration::SalesOrder @id=100 />

$ ErpIntegration::SalesOrder.find_by(code: "MT100")
# => #<ErpIntegration::SalesOrder @id=100 />

$ ErpIntegration::SalesOrder.find_by!(code: "MT100")
# => #<ErpIntegration::SalesOrder @id=100 />

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.

$ ErpIntegration::SalesOrder.select(:id, :reference).find_by(reference: 'MT1000SKX')
# => <ErpIntegration::SalesOrder @id=100 @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

It's important to note that you'll need the right privileges to publish the gem. Ask @germansvriz or @stefanvermaas to add you as a gem owner.

Publish a new version

  1. Run the prerelease script

    $ bin/prerelease 0.0.1
    
  2. Create Pull Request

  3. Merge it to develop

  4. Run Release script

    $ bin/release 0.0.1
    

We're following semver 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.

License

The gem is available as open source under the terms of the MIT License.

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.