Skip to content

Commit

Permalink
Merge pull request #256 from EasyPost/metadata
Browse files Browse the repository at this point in the history
feat: add carrier metadata beta endpoint
  • Loading branch information
Justintime50 authored Apr 18, 2023
2 parents ef787b4 + 063a83c commit 6aa713a
Show file tree
Hide file tree
Showing 5 changed files with 635 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/easypost/beta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module EasyPost::Beta
end

require_relative 'beta/carrier_metadata'
require_relative 'beta/end_shipper'
require_relative 'beta/referral'
require_relative 'beta/rate'
25 changes: 25 additions & 0 deletions lib/easypost/beta/carrier_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# A CarrierMetadata object contains all the selected metadata for all selected carriers.
class EasyPost::Beta::CarrierMetadata < EasyPost::Resource
# Retrieve metadata for carrier(s).
def self.retrieve_carrier_metadata(carriers = [], types = [], api_key = nil)
path = '/beta/metadata?'

params = {}

if carriers.length.positive?
params[:carriers] = carriers.join(',')
end

if types.length.positive?
params[:types] = types.join(',')
end

# urlencoded params
path += URI.encode_www_form(params)

response = EasyPost.make_request(:get, path, api_key, params)
EasyPost::Util.convert_to_easypost_object(response['carriers'] || [], api_key)
end
end
34 changes: 34 additions & 0 deletions spec/beta/carrier_metadata_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require 'spec_helper'

describe EasyPost::Beta::CarrierMetadata do
describe '.retrieve_carrier_metadata' do
it 'retrieve metadata for all carriers' do
metadata = described_class.retrieve_carrier_metadata

expect(metadata).to be_an_instance_of(Array)

expect(metadata.find { |carrier| carrier.name == 'usps' }).not_to be_nil
expect(metadata.find { |carrier| carrier.name == 'fedex' }).not_to be_nil
end

it 'retrieve metadata for a single carrier' do
carrier = 'usps'
types = %w[service_levels predefined_packages]

metadata = described_class.retrieve_carrier_metadata(['usps'], types)

expect(metadata).to be_an_instance_of(Array)

first_entry = metadata.first

# Assert we only have one carrier, and it's the one we requested
expect(metadata.length).to eq(1)
expect(first_entry[:name]).to eq(carrier)

# Assert we have the requested types
expect(first_entry).to have_attributes(service_levels: Array, predefined_packages: Array)
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6aa713a

Please sign in to comment.