easyship 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a4f0cee3b11970266c3c8ad8752e912f18805bc0584c134aaba5de84b9c1920
4
- data.tar.gz: 647fcc4d41650c6806ab27af95b69e6eaa482a4152243239bd011963531279e5
3
+ metadata.gz: b78843ee202c13d7e7ffd9fe40f3f6129896959c3ae689070787c9caffbd5d6a
4
+ data.tar.gz: 72b3086492a9938697547f90e96d126afe88431c93298dbc198b054d858c490d
5
5
  SHA512:
6
- metadata.gz: 23b54e132b58cd333722b18b5cb295f78faf317d8b91e4a5cb822258d7d5d63c4b94815c0f4f92e418c4987afe51697f966a12e476881546fe2b2c369caa0749
7
- data.tar.gz: 64a6448f591490294720cfc0291a18e3b5ac89529c72037c09ad5763aa0c4e877d40594d6207491d82315aefabb2a40ccdf0dc787f65811d59c726d4091004e4
6
+ metadata.gz: fc149787c49b67629702d71bc0bffba4043b2aff2216e46606a2623a0bbaedb4991aa3d7adadc662a2302ca1b05ad1f570eb8d772fc0fdc1af7acf1f5dab0d96
7
+ data.tar.gz: 4d602bda8293d6434b3b4e17fba312dfb58792c6cf27b591ef7383613b6eae53d8dcf748fee1ff0a181e32dc618b5cd8e71c12a765dd3f343cc178b910347278
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
+ ## [v0.1.1](https://github.com/mmarusyk/easyship/tree/v0.1.1) - 2024-06-08
2
3
 
3
- ## [0.1.0] - 2024-06-08
4
+ ### Features
5
+ - Add pagination cursor by @mmarusyk in https://github.com/mmarusyk/easyship/pull/1
6
+
7
+ ## [v0.1.0](https://github.com/mmarusyk/easyship/tree/v0.1.0) - 2024-06-08
4
8
 
5
9
  - Initial release
data/README.md CHANGED
@@ -7,7 +7,7 @@ This gem provides a simple client for Easyship, offering accessing to Easyship's
7
7
 
8
8
  Install the gem and add to the application's Gemfile by executing:
9
9
 
10
- $ bundle add easyhip
10
+ $ bundle add easyship
11
11
 
12
12
  If bundler is not being used to manage dependencies, install the gem by executing:
13
13
 
@@ -46,7 +46,7 @@ Easyship::Configuration.configure do |config|
46
46
  config.api_key = 'your_easyship_api_key'
47
47
  end
48
48
  ```
49
-
49
+ Configuration supports the next keys: `url`, `api_key`, `per_page`.
50
50
 
51
51
  ### Making Requests
52
52
  ```ruby
@@ -79,7 +79,7 @@ Easyship::Client.post('/2023-01/shipment', payload)
79
79
  ```
80
80
 
81
81
  ### Handle errors
82
- hen 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:
82
+ Then 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:
83
83
 
84
84
  1. Wrap your API calls in a `begin-rescue` block.
85
85
  2. Catch specific errors from the `easyship` gem to handle them accordingly.
@@ -94,12 +94,30 @@ rescue Easyship::Errors::RateLimitError => e
94
94
  end
95
95
  ```
96
96
 
97
+ ### Pagination
98
+ 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.
99
+
100
+ Suppose you want to retrieve a list of shipments that may span multiple pages. Here's how you can use the `get` method:
101
+
102
+ ```ruby
103
+ shipments = []
104
+
105
+ Easyship::Client.instance.get('/2023-01/shipments') do |page|
106
+ shipments.concat(page[:shipments])
107
+ end
108
+
109
+ shipments # Returns all shipments from all pages
110
+ ```
111
+
112
+ To setup items perpage, use the key `per_page` in your configuration.
113
+
97
114
  ## Development
98
115
 
99
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
116
+ 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.
100
117
 
101
118
  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](https://rubygems.org).
102
119
 
120
+
103
121
  ## Contributing
104
122
 
105
123
  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](https://github.com/mmarusyk/easyship/blob/main/CODE_OF_CONDUCT.md).
@@ -4,6 +4,7 @@ require 'singleton'
4
4
  require 'faraday'
5
5
  require_relative 'middleware/error_handler_middleware'
6
6
  require_relative 'handlers/response_body_handler'
7
+ require_relative 'pagination/cursor'
7
8
 
8
9
  module Easyship
9
10
  # Represents a client to interact with the Easyship API
@@ -15,10 +16,14 @@ module Easyship
15
16
  @api_key = Easyship.configuration.api_key
16
17
  end
17
18
 
18
- def get(path, params = {})
19
- response = connection.get(path, params)
19
+ def get(path, params = {}, &block)
20
+ if block
21
+ Easyship::Pagination::Cursor.new(self, path, params).all(&block)
22
+ else
23
+ response = connection.get(path, params)
20
24
 
21
- handle_response(response)
25
+ handle_response(response)
26
+ end
22
27
  end
23
28
 
24
29
  def post(path, params = {})
@@ -3,11 +3,12 @@
3
3
  module Easyship
4
4
  # Represents the configuration settings for the Easyship client.
5
5
  class Configuration
6
- attr_accessor :url, :api_key
6
+ attr_accessor :url, :api_key, :per_page
7
7
 
8
8
  def initialize
9
9
  @url = nil
10
10
  @api_key = nil
11
+ @per_page = 100 # Maximum possible number of items per page
11
12
  end
12
13
  end
13
14
  end
@@ -5,9 +5,9 @@ module Easyship
5
5
  # Handles the response body
6
6
  class ResponseBodyHandler
7
7
  def self.handle_response(response)
8
- JSON.parse(response.body, symbolize_names: true)
9
- rescue JSON::ParserError
10
- response.body
8
+ body = response.body
9
+
10
+ JSON.parse(body, symbolize_names: true) unless body.nil? || body.empty?
11
11
  end
12
12
  end
13
13
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Easyship
4
+ module Pagination
5
+ # Represents a pagination object
6
+ class Cursor
7
+ attr_reader :client, :path, :params, :key, :per_page
8
+
9
+ def initialize(client, path, params)
10
+ @client = client
11
+ @path = path
12
+ @params = params
13
+ @per_page = params[:per_page] || Easyship.configuration.per_page
14
+ end
15
+
16
+ def all
17
+ page = 1
18
+
19
+ loop do
20
+ body = client.get(path, params.merge(page: page, per_page: per_page))
21
+
22
+ break if body.nil? || body.empty?
23
+
24
+ yield body
25
+
26
+ break if body.dig(:meta, :pagination, :next).nil?
27
+
28
+ page += 1
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Easyship
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easyship
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Marusyk
@@ -54,6 +54,7 @@ files:
54
54
  - lib/easyship/errors/unprocessable_content_error.rb
55
55
  - lib/easyship/handlers/response_body_handler.rb
56
56
  - lib/easyship/middleware/error_handler_middleware.rb
57
+ - lib/easyship/pagination/cursor.rb
57
58
  - lib/easyship/version.rb
58
59
  - sig/easyship.rbs
59
60
  homepage: https://rubygems.org/gems/easyship