Skip to content

Commit

Permalink
Merge pull request #140 from EasyPost/test_suite
Browse files Browse the repository at this point in the history
Adds Full Test Suite
  • Loading branch information
Justintime50 authored Feb 25, 2022
2 parents c9c9aef + 5bbf1ee commit 582cc9d
Show file tree
Hide file tree
Showing 189 changed files with 12,262 additions and 11,842 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
inherit_from: easycop.yml

# We are ignoring RSpec/FilePath because Simplecov doesn't play nice with nested spec files
RSpec/FilePath:
Enabled: false
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

* Bumps minimum Ruby version from `2.2` to `2.5`
* Documents each interface of the project
* Overhauls the test suite with full coverage
* Introduces `Rubocop` and lints the entire project
* Removes the unusable `Print` and `PrintJob` objects
* Removes deprecated and unusable `stamp_and_barcode_by_reference` method on the Batch object
* Explicitly returns an error of "not implemented" for `Rate.all` and `Parcel.all`
* Removes the `Shipment.get_rates` function as shipments already contain rates. If you need to get new rates for a shipment, use the `Shipment.regenerate_rates` function instead
* Removes the parameters from `Address.verify` as they are unusable
* Removes the deprecated `http_status` property of the `EasyPost::Error` object as it was replaced with `status`
* Various other small improvements and bug fixes


## 3.5.0 2021-12-06
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ Up-to-date documentation at: https://easypost.com/docs
## Development

```bash
# Run tests
API_KEY=123... bundle exec rspec
# Run tests (coverage is generated on a successful test suite run)
EASYPOST_TEST_API_KEY=123... EASYPOST_PROD_API_KEY=123... bundle exec rspec
```

## Releasing
Expand Down
5 changes: 3 additions & 2 deletions easypost.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ Gem::Specification.new do |spec|
spec.summary = 'EasyPost Ruby Client Library'
spec.description = 'Client library for accessing the EasyPost shipping API via Ruby.'
spec.authors = 'EasyPost Developers'
spec.email = 'support@easypost.com'
spec.email = 'oss@easypost.com'
spec.homepage = 'https://www.easypost.com/docs'

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 2.5'

Expand All @@ -27,6 +26,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rspec', '~> 3.10'
spec.add_development_dependency 'rubocop', '~> 1.24'
spec.add_development_dependency 'rubocop-rspec', '~> 2.7'
spec.add_development_dependency 'simplecov', '~> 0.21'
spec.add_development_dependency 'simplecov-lcov', '~> 0.8'
spec.add_development_dependency 'vcr', '~> 6.0'
spec.add_development_dependency 'webmock', '~> 3.14'
end
4 changes: 2 additions & 2 deletions lib/easypost/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def self.create_and_verify(params = {}, carrier = nil, api_key = nil)
end

# Verify an Address.
def verify(params = {}, carrier = nil)
def verify
begin
response = EasyPost.make_request(:get, "#{url}/verify?carrier=#{String(carrier)}", @api_key, params)
response = EasyPost.make_request(:get, "#{url}/verify", @api_key)
rescue StandardError
raise EasyPost::Error.new('Unable to verify address.')
end
Expand Down
8 changes: 4 additions & 4 deletions lib/easypost/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@ def self.create_and_buy(params = {}, api_key = nil)
# Buy a Batch.
def buy(params = {})
response = EasyPost.make_request(:post, "#{url}/buy", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end

# Convert the label format of a Batch.
def label(params = {})
response = EasyPost.make_request(:post, "#{url}/label", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end

# Remove Shipments from a Batch.
def remove_shipments(params = {})
response = EasyPost.make_request(:post, "#{url}/remove_shipments", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end

# Add Shipments to a Batch.
def add_shipments(params = {})
response = EasyPost.make_request(:post, "#{url}/add_shipments", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end
Expand Down
2 changes: 1 addition & 1 deletion lib/easypost/customs_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# CustomsInfo objects contain CustomsItem objects and all necessary information for the generation of customs forms required for international shipping.
class EasyPost::CustomsInfo < EasyPost::Resource
# Retrieve a list of CustomsInfo objects
def self.all(_filters = {}, _api_key = nil)
def self.all
raise NotImplementedError.new('CustomsInfo.all not implemented.')
end
end
2 changes: 1 addition & 1 deletion lib/easypost/customs_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# A CustomsItem object describes goods for international shipment and should be created then included in a CustomsInfo object.
class EasyPost::CustomsItem < EasyPost::Resource
# Retrieve a list of CustomsItem objects
def self.all(_filters = {}, _api_key = nil)
def self.all
raise NotImplementedError.new('CustomsItem.all not implemented.')
end
end
3 changes: 1 addition & 2 deletions lib/easypost/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

# EasyPost Error object.
class EasyPost::Error < StandardError
attr_reader :message, :status, :http_status, :http_body, :code, :errors
attr_reader :message, :status, :http_body, :code, :errors

# Initialize a new EasyPost Error
def initialize(message = nil, status = nil, code = nil, errors = nil, http_body = nil)
@message = message
@status = status
@http_status = status # deprecated
@code = code
@errors = errors
@http_body = http_body
Expand Down
18 changes: 4 additions & 14 deletions lib/easypost/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def inspect
end

# Refresh an object from the API.
def refresh_from(values, api_key, _partial = false) # rubocop:disable Style/OptionalBooleanParameter
def refresh_from(values, api_key)
@api_key = api_key

added = Set.new(values.keys - @values.keys)
Expand All @@ -49,6 +49,9 @@ def refresh_from(values, api_key, _partial = false) # rubocop:disable Style/Opti
add_accessors(added)
end

# IDs don't change, do not update it
@values.delete(:id)

values.each do |k, v|
@values[k.to_s] = EasyPost::Util.convert_to_easypost_object(v, api_key, self, k)
@transient_values.delete(k)
Expand Down Expand Up @@ -134,19 +137,6 @@ def metaclass
class << self; self; end
end

# Remove the accessors of an object.
def remove_accessors(keys)
metaclass.instance_eval do
keys.each do |k|
next if @@immutable_values.include?(k)

k_eq = :"#{k}="
remove_method(k) if method_defined?(k)
remove_method(k_eq) if method_defined?(k_eq)
end
end
end

# Add accessors of an object.
def add_accessors(keys)
metaclass.instance_eval do
Expand Down
6 changes: 3 additions & 3 deletions lib/easypost/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class EasyPost::Order < EasyPost::Resource
# Get the rates of an Order.
def get_rates(params = {})
response = EasyPost.make_request(:get, "#{url}/rates", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end
Expand All @@ -20,13 +20,13 @@ def buy(params = {})
end

response = EasyPost.make_request(:post, "#{url}/buy", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end

# Retrieve a list of Order objects.
def self.all(_filters = {}, _api_key = nil)
def self.all
raise NotImplementedError.new('Order.all not implemented.')
end
end
2 changes: 1 addition & 1 deletion lib/easypost/parcel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Parcel objects represent the physical container being shipped.
class EasyPost::Parcel < EasyPost::Resource
# Retrieving all Parcel objects is not supported.
def self.all(_filters = {}, _api_key = nil)
def self.all
raise NotImplementedError.new('Parcel.all not implemented.')
end
end
6 changes: 3 additions & 3 deletions lib/easypost/pickup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ def buy(params = {})
end

response = EasyPost.make_request(:post, "#{url}/buy", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end

# Cancel a Pickup.
def cancel(params = {})
response = EasyPost.make_request(:post, "#{url}/cancel", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end

# Retrieve a list of all Pickup objects.
def self.all(_filters = {}, _api_key = nil)
def self.all
raise NotImplementedError.new('Pickup.all not implemented.')
end
end
2 changes: 1 addition & 1 deletion lib/easypost/rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# A Rate object contains all the details about the rate of a Shipment.
class EasyPost::Rate < EasyPost::Resource
# Retrieving all Rate objects is not supported.
def self.all(_filters = {}, _api_key = nil)
def self.all
raise NotImplementedError.new('Rate.all not implemented.')
end
end
15 changes: 1 addition & 14 deletions lib/easypost/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,11 @@ def self.create(params = {}, api_key = nil)
EasyPost::Util.convert_to_easypost_object(response, api_key)
end

# Retrieve a Report.
def self.retrieve(params, api_key = nil)
id = if params.is_a?(String)
params
else
params[:id]
end

instance = new(id, api_key)
instance.refresh
instance
end

# Retrieve a list of Report objects.
def self.all(filters = {}, api_key = nil)
url = "#{self.url}/#{filters[:type]}"

response = EasyPost.make_request(:get, url, api_key, filters)
return EasyPost::Util.convert_to_easypost_object(response, api_key) if response
EasyPost::Util.convert_to_easypost_object(response, api_key)
end
end
12 changes: 6 additions & 6 deletions lib/easypost/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class EasyPost::Shipment < EasyPost::Resource
# Regenerate the rates of a Shipment.
def regenerate_rates(params = {})
response = EasyPost.make_request(:post, "#{url}/rerate", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end
Expand All @@ -27,7 +27,7 @@ def buy(params = {})
end

response = EasyPost.make_request(:post, "#{url}/buy", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end
Expand All @@ -41,20 +41,20 @@ def insure(params = {})
end

response = EasyPost.make_request(:post, "#{url}/insure", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end

# Refund a Shipment.
def refund(params = {})
response = EasyPost.make_request(:get, "#{url}/refund", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end

# Conver the label format of a Shipment.
# Convert the label format of a Shipment.
def label(params = {})
if params.is_a?(String)
temp = params.clone
Expand All @@ -63,7 +63,7 @@ def label(params = {})
end

response = EasyPost.make_request(:get, "#{url}/label", @api_key, params)
refresh_from(response, @api_key, true)
refresh_from(response, @api_key)

self
end
Expand Down
2 changes: 2 additions & 0 deletions lib/easypost/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def self.convert_to_easypost_object(response, api_key, parent = nil, name = nil)
types = {
'Address' => EasyPost::Address,
'Batch' => EasyPost::Batch,
'Brand' => EasyPost::Brand,
'CarrierAccount' => EasyPost::CarrierAccount,
'CustomsInfo' => EasyPost::CustomsInfo,
'CustomsItem' => EasyPost::CustomsItem,
Expand Down Expand Up @@ -58,6 +59,7 @@ def self.convert_to_easypost_object(response, api_key, parent = nil, name = nil)
prefixes = {
'adr' => EasyPost::Address,
'batch' => EasyPost::Batch,
'brd' => EasyPost::Brand,
'ca' => EasyPost::CarrierAccount,
'cstinfo' => EasyPost::CustomsInfo,
'cstitem' => EasyPost::CustomsItem,
Expand Down
3 changes: 1 addition & 2 deletions lib/easypost/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def update(params = {})

# Delete a Webhook.
def delete
# NOTE: This method is redefined here since the "url" method conflicts
# with the objects field
# NOTE: This method is redefined here since the "url" method conflicts with the objects field
unless id
raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}")
end
Expand Down
Loading

0 comments on commit 582cc9d

Please sign in to comment.