easyship 0.1.4 → 0.2.0

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: 1c6c501de616b3a63e0d2115bcd85edd664d19457e15cee9ddc39b90460faf06
4
- data.tar.gz: c28725fd205ad6c7943bc3af5ec230c6dfa0a3a9a605a36caaab01d9ff6b4120
3
+ metadata.gz: 7c050cd7909443866f9f0365555f577024aa49ce0065be2a961018385409972d
4
+ data.tar.gz: a827b2cdaa8207b8723f5a7c2b65efba7c6b7f9e10cf16b46ea36f34aebae763
5
5
  SHA512:
6
- metadata.gz: d3e59ba1d63fccd367093dd0d0cfa2c4e853be57820384e7ab033929f808adffca7313633009dc6b7804d3c54d8130ab3f8da45586e31c59865a903c4b906e38
7
- data.tar.gz: db7d3fb69ea674cfc5a9ee6527193ce637bf123b74d51f104a821de4f0c9273d3cb4d25621e8ea6b885f852f16c09e2c87485e6845b04bb4e1f70ad4aff319d2
6
+ metadata.gz: 959f2149e8e0d3f33ce0592de28df9718a0fb1c24cdb78909acffbdcecbeab71449b29a1000c03589d24616603214f0f9aa0dc05bad67e96ee413b2acad2a25c
7
+ data.tar.gz: 0ba62b8bf714db84417d4c2abb13e66738b0510dd0f57997c9403cdbfb81e2fa78029fb9b058d07a5ed943bb5eb8a9054be8929f6a7975b816fc59dda360b781
data/.rubocop.yml CHANGED
@@ -1,4 +1,5 @@
1
- require:
1
+ plugins:
2
+ - rubocop-rake
2
3
  - rubocop-rspec
3
4
  - rubocop-performance
4
5
 
@@ -7,4 +8,7 @@ AllCops:
7
8
  NewCops: enable
8
9
 
9
10
  Style/Copyright:
10
- Enabled: false
11
+ Enabled: false
12
+
13
+ RSpec/ExampleLength:
14
+ Max: 10
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [v.0.2.0](https://github.com/mmarusyk/easyship/tree/v0.2.0) - 2025-05-04
4
+
5
+ ### Added
6
+ - Add response_body and response_header to Easyship::Error
7
+ - Deprecate body_error by @mmarusyk in https://github.com/mmarusyk/easyship/pull/9
8
+ - Add badges by @mmarusyk in https://github.com/mmarusyk/easyship/pull/10
9
+
10
+
11
+ ## [v0.1.5](https://github.com/mmarusyk/easyship/tree/v0.1.5) - 2025-05-03
12
+
13
+ ### Fixed
14
+ - update docs by @troyizzle in https://github.com/mmarusyk/easyship/pull/5
15
+ - Add test coverage by @mmarusyk in https://github.com/mmarusyk/easyship/pull/6
16
+ - Automate release by @mmarusyk in https://github.com/mmarusyk/easyship/pull/7
3
17
 
4
18
  ## [v0.1.4](https://github.com/mmarusyk/easyship/tree/v0.1.4) - 2024-10-16
5
19
 
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Easyship
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/easyship.svg)](https://badge.fury.io/rb/easyship)
4
+ [![Build Status](https://github.com/mmarusyk/easyship/workflows/Ruby/badge.svg)](https://github.com/mmarusyk/easyship/actions?query=workflow%3ARuby)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+
3
7
  This gem provides a simple client for Easyship, offering accessing to Easyship's
4
8
  shipping, tracking, and logistics services directly from Ruby applications.
5
9
 
@@ -59,7 +63,7 @@ Configuration supports the next keys: `url`, `api_key`, `per_page`.
59
63
  ### Making Requests
60
64
  `Easyship::Client` supports the next methods: `get`, `post`, `put`, `delete`.
61
65
  ```ruby
62
- Easyship::Client.get('/2023-01/account')
66
+ Easyship::Client.instance.get('/2023-01/account')
63
67
  ```
64
68
 
65
69
  To make post request:
@@ -84,7 +88,7 @@ payload = {
84
88
  ]
85
89
  }
86
90
 
87
- Easyship::Client.post('/2023-01/shipment', payload)
91
+ Easyship::Client.instance.post('/2023-01/shipment', payload)
88
92
  ```
89
93
 
90
94
  ### Handle errors
@@ -97,12 +101,21 @@ For example:
97
101
 
98
102
  ```ruby
99
103
  begin
100
- Easyship::Client.post('/2023-01/shipment', payload)
104
+ Easyship::Client.instance.post('/2023-01/shipment', payload)
101
105
  rescue Easyship::Errors::RateLimitError => e
102
106
  Rails.logger.error("Easyship Error: #{e.message}")
103
107
  end
104
108
  ```
105
109
 
110
+ Each error instance provides these methods:
111
+
112
+ - `message`: Returns the error message text.
113
+ - `body_error`: Returns an error details **(Deprecated)**.
114
+ - `error`: Returns a hash containing original detailed error object from Easyship.
115
+ - `response_body`: Returns the full response body from the API.
116
+ - `response_headers`: Returns the HTTP headers from the API response.
117
+
118
+
106
119
  ### Pagination
107
120
  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
121
 
@@ -5,7 +5,23 @@ Dir[File.join(__dir__, 'errors', '**', '*.rb')].each { |f| require_relative f }
5
5
  module Easyship
6
6
  # Represents a mapping of HTTP status codes to Easyship-specific classes
7
7
  class Error
8
- # rubocop:disable Style::MutableConstant Style::MissingElse
8
+ class << self
9
+ def for_status(status_code)
10
+ ERRORS[status_code] || default_error_for(status_code)
11
+ end
12
+
13
+ private
14
+
15
+ def default_error_for(status_code)
16
+ case status_code.to_s
17
+ when /4\d{2}/
18
+ Easyship::Errors::ClientError
19
+ when /5\d{2}/
20
+ Easyship::Errors::ServerError
21
+ end
22
+ end
23
+ end
24
+
9
25
  ERRORS = {
10
26
  400 => Easyship::Errors::BadRequestError,
11
27
  401 => Easyship::Errors::InvalidTokenError,
@@ -13,18 +29,6 @@ module Easyship
13
29
  404 => Easyship::Errors::ResourceNotFoundError,
14
30
  422 => Easyship::Errors::UnprocessableContentError,
15
31
  429 => Easyship::Errors::RateLimitError
16
- }
17
-
18
- ERRORS.default_proc = proc do |_hash, key|
19
- case key.to_s
20
- when /4\d{2}/
21
- Easyship::Errors::ClientError
22
- when /5\d{2}/
23
- Easyship::Errors::ServerError
24
- end
25
- end
26
- # rubocop:enable Style::MutableConstant Style::MissingElse
27
-
28
- ERRORS.freeze
32
+ }.freeze
29
33
  end
30
34
  end
@@ -4,12 +4,20 @@ module Easyship
4
4
  module Errors
5
5
  # Represents an error that is raised when an error occurs in the Easyship API.
6
6
  class EasyshipError < StandardError
7
- attr_reader :message, :body_error
7
+ attr_reader :message, :error, :response_body, :response_headers
8
8
 
9
- def initialize(message: '', body_error: {})
9
+ def initialize(message: '', body_error: {}, error: {}, response_body: nil, response_headers: {})
10
10
  super(message)
11
+ @error = error
11
12
  @message = message
12
13
  @body_error = body_error
14
+ @response_body = response_body
15
+ @response_headers = response_headers
16
+ end
17
+
18
+ def body_error
19
+ warn '[DEPRECATION] `body_error` is deprecated. Please use `error` instead.'
20
+ @body_error
13
21
  end
14
22
  end
15
23
  end
@@ -9,20 +9,33 @@ module Easyship
9
9
  def on_complete(env)
10
10
  status_code = env[:status].to_i
11
11
  body = response_body(env[:body])
12
+ headers = env[:response_headers]
12
13
 
13
- handle_status_code(status_code, body)
14
+ handle_status_code(status_code, headers, body)
14
15
  end
15
16
 
16
17
  private
17
18
 
18
- def handle_status_code(status_code, body)
19
- error_class = Easyship::Error::ERRORS[status_code]
19
+ def handle_status_code(status_code, headers, body)
20
+ error_class = Easyship::Error.for_status(status_code)
20
21
 
21
- raise_error(error_class, body) if error_class
22
+ raise_error(error_class, headers, body) if error_class
22
23
  end
23
24
 
24
- def raise_error(class_error, body)
25
- raise class_error.new(message: message(body), body_error: body_error(body))
25
+ def raise_error(class_error, headers, body)
26
+ raise class_error.new(
27
+ message: message(body),
28
+ body_error: body_error(body),
29
+ error: build_error(body),
30
+ response_body: body,
31
+ response_headers: headers
32
+ )
33
+ end
34
+
35
+ def build_error(body)
36
+ return default_error unless body.is_a?(Hash)
37
+
38
+ body[:error]
26
39
  end
27
40
 
28
41
  def body_error(body)
@@ -55,6 +68,10 @@ module Easyship
55
68
  { details: body, message: 'Something went wrong.' }
56
69
  end
57
70
 
71
+ def default_error
72
+ { details: [], message: 'Something went wrong.' }
73
+ end
74
+
58
75
  def response_body(body)
59
76
  JSON.parse(body, symbolize_names: true)
60
77
  rescue JSON::ParserError
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Easyship
4
- VERSION = '0.1.4'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easyship
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Marusyk
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-10-16 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: faraday
@@ -68,7 +67,6 @@ metadata:
68
67
  allowed_push_host: https://rubygems.org
69
68
  source_code_uri: https://github.com/mmarusyk/easyship
70
69
  changelog_uri: https://github.com/mmarusyk/easyship/blob/main/CHANGELOG.md
71
- post_install_message:
72
70
  rdoc_options: []
73
71
  require_paths:
74
72
  - lib
@@ -83,8 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
81
  - !ruby/object:Gem::Version
84
82
  version: '0'
85
83
  requirements: []
86
- rubygems_version: 3.5.16
87
- signing_key:
84
+ rubygems_version: 3.6.7
88
85
  specification_version: 4
89
86
  summary: A Ruby client for integrating with Easyship's API for shipping and logistics
90
87
  management.