Skip to content

This gem provides a simple client for Easyship

License

Notifications You must be signed in to change notification settings

mmarusyk/easyship

Repository files navigation

Easyship

This gem provides a simple client for Easyship, offering accessing to Easyship's shipping, tracking, and logistics services directly from Ruby applications.

Requirements

Before you begin, ensure you have met the following requirements:

  • Ruby version 3.0.0 or newer. You can check your Ruby version by running ruby -v.
  • Bundler installed. You can install Bundler with gem install bundler.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add easyship

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install easyship

Usage

To use the easyship gem in your Ruby application, you need to configure it with your API key and then you can start making requests to Easyship's services.

Configuration

First, configure the gem with your Easyship API key:

If you have to use in a ruby file:

require 'easyship'

Easyship.configure do |c|
  c.url = 'api_url'
  c.api_key = 'your_easyship_api_key'
end

If you have to use in Rails:

  1. Add to Gemfile
  gem 'easyship'
  1. Run bundle i

  2. Create a new file in config/initializers directory

Easyship.configure do |config|
  config.url = 'api_url'
  config.api_key = 'your_easyship_api_key'
end

Configuration supports the next keys: url, api_key, per_page.

Making Requests

Easyship::Client supports the next methods: get, post, put, delete.

Easyship::Client.instance.get('/2023-01/account')

To make post request:

payload = {
  origin_country_alpha2: "SG",
  destination_country_alpha2: "US",
  tax_paid_by: "Recipient",
  is_insured: false,
  items: [
    {
      description: "Silk dress",
      sku: "1002541",
      actual_weight: 1.2,
      height: 10,
      width: 15,
      length: 20,
      category: "fashion",
      declared_currency: "SGD",
      declared_customs_value: 100
    }
  ]
}

Easyship::Client.instance.post('/2023-01/shipment', payload)

Handle errors

When using the easyship gem in a Rails application, it's important to handle potential errors that may arise during API calls. Here's how you can handle errors gracefully:

  1. Wrap your API calls in a begin-rescue block.
  2. Catch specific errors from the easyship gem to handle them accordingly.

For example:

begin
  Easyship::Client.instance.post('/2023-01/shipment', payload)
rescue Easyship::Errors::RateLimitError => e
  Rails.logger.error("Easyship Error: #{e.message}")
end

Pagination

The get method in the Easyship::Client class is designed to support pagination seamlessly when interacting with the Easyship API by passing block of code. This method abstracts the complexity of managing pagination logic, allowing you to retrieve all items across multiple pages with a single method call.

Suppose you want to retrieve a list of shipments that may span multiple pages. Here's how you can use the get method:

shipments = []

Easyship::Client.instance.get('/2023-01/shipments') do |page|
  shipments.concat(page[:shipments])
end

shipments # Returns all shipments from all pages

To setup items perpage, use the key per_page in your configuration.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. Then, eun rake rubocop to run the rubocop. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mmarusyk/easyship. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct. You can find a list of contributors in the CONTRIBUTORS.md file.

License

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

Code of Conduct

Everyone interacting in the Easyship project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.