easyship 0.1.0 → 0.1.2
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 +4 -4
- data/CHANGELOG.md +16 -1
- data/CONTRIBUTORS.md +3 -0
- data/README.md +33 -6
- data/lib/easyship/client.rb +8 -3
- data/lib/easyship/configuration.rb +2 -1
- data/lib/easyship/handlers/response_body_handler.rb +3 -3
- data/lib/easyship/middleware/error_handler_middleware.rb +28 -2
- data/lib/easyship/pagination/cursor.rb +33 -0
- data/lib/easyship/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a715ea30361416f3bc58cee44919e36a7ac7dbe85f1f615c2a74350f889bdef
|
4
|
+
data.tar.gz: 297a361bdad68de29061c1cf6aaa09db8aad1573aa9356c069784b564526ca7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf0c4bded300aa2b72ab47b5ddbd16ba5733c3f515bc403e74322bd4dd5b10b0c9a214aeb629bb257fe0078952b4b5ad0ec4d75e05794666207fcef57d420084
|
7
|
+
data.tar.gz: 394cc54f2400a0f1b86a66340c19e03a42298fe31dc1764ee5c443b8e8fa4e1ddc2af3be5123e2ba4efc781f843c6ab2739481d850d28e83155d178886a8b1a0
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,20 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
-
## [
|
3
|
+
## [v0.1.2](https://github.com/mmarusyk/easyship/tree/v0.1.2) - 2024-06-24
|
4
|
+
|
5
|
+
### Features
|
6
|
+
- Handle Handle different responses of errors from Easyship (v1, v2,...) by @mmarusyk in https://github.com/mmarusyk/easyship/pull/3
|
7
|
+
|
8
|
+
### Fixed
|
9
|
+
- Updated README.md by @mmarusyk in https://github.com/mmarusyk/easyship/pull/2
|
10
|
+
|
11
|
+
|
12
|
+
## [v0.1.1](https://github.com/mmarusyk/easyship/tree/v0.1.1) - 2024-06-08
|
13
|
+
|
14
|
+
### Features
|
15
|
+
- Add pagination cursor by @mmarusyk in https://github.com/mmarusyk/easyship/pull/1
|
16
|
+
|
17
|
+
|
18
|
+
## [v0.1.0](https://github.com/mmarusyk/easyship/tree/v0.1.0) - 2024-06-08
|
4
19
|
|
5
20
|
- Initial release
|
data/CONTRIBUTORS.md
ADDED
data/README.md
CHANGED
@@ -3,11 +3,18 @@
|
|
3
3
|
This gem provides a simple client for Easyship, offering accessing to Easyship's
|
4
4
|
shipping, tracking, and logistics services directly from Ruby applications.
|
5
5
|
|
6
|
+
## Requirements
|
7
|
+
|
8
|
+
Before you begin, ensure you have met the following requirements:
|
9
|
+
|
10
|
+
- Ruby version 3.0.0 or newer. You can check your Ruby version by running `ruby -v`.
|
11
|
+
- Bundler installed. You can install Bundler with `gem install bundler`.
|
12
|
+
|
6
13
|
## Installation
|
7
14
|
|
8
15
|
Install the gem and add to the application's Gemfile by executing:
|
9
16
|
|
10
|
-
$ bundle add
|
17
|
+
$ bundle add easyship
|
11
18
|
|
12
19
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
13
20
|
|
@@ -25,7 +32,7 @@ If you have to use in a ruby file:
|
|
25
32
|
```ruby
|
26
33
|
require 'easyship'
|
27
34
|
|
28
|
-
Easyship
|
35
|
+
Easyship.configure do |c|
|
29
36
|
c.url = 'api_url'
|
30
37
|
c.api_key = 'your_easyship_api_key'
|
31
38
|
end
|
@@ -41,14 +48,16 @@ If you have to use in Rails:
|
|
41
48
|
|
42
49
|
3. Create a new file in `config/initializers` directory
|
43
50
|
```ruby
|
44
|
-
Easyship
|
51
|
+
Easyship.configure do |config|
|
45
52
|
config.url = 'api_url'
|
46
53
|
config.api_key = 'your_easyship_api_key'
|
47
54
|
end
|
48
55
|
```
|
49
56
|
|
57
|
+
Configuration supports the next keys: `url`, `api_key`, `per_page`.
|
50
58
|
|
51
59
|
### Making Requests
|
60
|
+
`Easyship::Client` supports the next methods: `get`, `post`, `put`, `delete`.
|
52
61
|
```ruby
|
53
62
|
Easyship::Client.get('/2023-01/account')
|
54
63
|
```
|
@@ -79,7 +88,7 @@ Easyship::Client.post('/2023-01/shipment', payload)
|
|
79
88
|
```
|
80
89
|
|
81
90
|
### Handle errors
|
82
|
-
|
91
|
+
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:
|
83
92
|
|
84
93
|
1. Wrap your API calls in a `begin-rescue` block.
|
85
94
|
2. Catch specific errors from the `easyship` gem to handle them accordingly.
|
@@ -94,15 +103,33 @@ rescue Easyship::Errors::RateLimitError => e
|
|
94
103
|
end
|
95
104
|
```
|
96
105
|
|
106
|
+
### Pagination
|
107
|
+
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.
|
108
|
+
|
109
|
+
Suppose you want to retrieve a list of shipments that may span multiple pages. Here's how you can use the `get` method:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
shipments = []
|
113
|
+
|
114
|
+
Easyship::Client.instance.get('/2023-01/shipments') do |page|
|
115
|
+
shipments.concat(page[:shipments])
|
116
|
+
end
|
117
|
+
|
118
|
+
shipments # Returns all shipments from all pages
|
119
|
+
```
|
120
|
+
|
121
|
+
To setup items perpage, use the key `per_page` in your configuration.
|
122
|
+
|
97
123
|
## Development
|
98
124
|
|
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.
|
125
|
+
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
126
|
|
101
127
|
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
128
|
|
129
|
+
|
103
130
|
## Contributing
|
104
131
|
|
105
|
-
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).
|
132
|
+
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). You can find a list of contributors in the [CONTRIBUTORS.md](https://github.com/mmarusyk/easyship/blob/main/CONTRIBUTORS.md) file.
|
106
133
|
|
107
134
|
## License
|
108
135
|
|
data/lib/easyship/client.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
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
|
@@ -25,11 +25,37 @@ module Easyship
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def body_error(body)
|
28
|
-
body.is_a?(Hash)
|
28
|
+
return {} unless body.is_a?(Hash)
|
29
|
+
|
30
|
+
if body.key?(:error) && body[:error].is_a?(Hash)
|
31
|
+
format_body_error(body)
|
32
|
+
elsif body.key?(:errors) && body[:errors].is_a?(Array)
|
33
|
+
format_body_errors_array(body)
|
34
|
+
elsif body.key?(:errors)
|
35
|
+
format_body_errors(body)
|
36
|
+
else
|
37
|
+
format_by_default(body)
|
38
|
+
end
|
29
39
|
end
|
30
40
|
|
31
41
|
def message(body)
|
32
|
-
body
|
42
|
+
body_error(body)[:message]
|
43
|
+
end
|
44
|
+
|
45
|
+
def format_body_error(body)
|
46
|
+
body[:error]
|
47
|
+
end
|
48
|
+
|
49
|
+
def format_body_errors_array(body)
|
50
|
+
{ details: body[:errors], message: body[:errors].map { |error| error[:message] }.join(', ') }
|
51
|
+
end
|
52
|
+
|
53
|
+
def format_body_errors(body)
|
54
|
+
{ details: body[:errors], message: body[:errors] }
|
55
|
+
end
|
56
|
+
|
57
|
+
def format_by_default(body)
|
58
|
+
{ details: body, message: 'Something went wrong.' }
|
33
59
|
end
|
34
60
|
|
35
61
|
def json?(body)
|
@@ -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
|
data/lib/easyship/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easyship
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Marusyk
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- ".rubocop.yml"
|
38
38
|
- CHANGELOG.md
|
39
39
|
- CODE_OF_CONDUCT.md
|
40
|
+
- CONTRIBUTORS.md
|
40
41
|
- LICENSE.txt
|
41
42
|
- README.md
|
42
43
|
- Rakefile
|
@@ -54,6 +55,7 @@ files:
|
|
54
55
|
- lib/easyship/errors/unprocessable_content_error.rb
|
55
56
|
- lib/easyship/handlers/response_body_handler.rb
|
56
57
|
- lib/easyship/middleware/error_handler_middleware.rb
|
58
|
+
- lib/easyship/pagination/cursor.rb
|
57
59
|
- lib/easyship/version.rb
|
58
60
|
- sig/easyship.rbs
|
59
61
|
homepage: https://rubygems.org/gems/easyship
|