Class: FriendlyShipping::ShipmentOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/shipment_options.rb

Overview

Base class for shipment options. Used when serializing API requests.

Instance Method Summary collapse

Constructor Details

#initialize(package_options: Set.new, package_options_class: PackageOptions) ⇒ ShipmentOptions

Returns a new instance of ShipmentOptions.

Parameters:

  • package_options (Array<PackageOptions>) (defaults to: Set.new)

    the options for packages in this shipment

  • package_options_class (Class) (defaults to: PackageOptions)

    the class to use for package options when none are provided



8
9
10
11
12
13
14
# File 'lib/friendly_shipping/shipment_options.rb', line 8

def initialize(
  package_options: Set.new,
  package_options_class: PackageOptions
)
  @package_options = package_options
  @package_options_class = package_options_class
end

Instance Method Details

#options_for_package(package) ⇒ PackageOptions

Finds and returns package options for the given item. If options cannot be found, the package_options_class is used to construct new options.

Parameters:

  • package (#id)

    the package for which to get options

Returns:



21
22
23
24
25
# File 'lib/friendly_shipping/shipment_options.rb', line 21

def options_for_package(package)
  package_options.detect do |package_option|
    package_option.package_id == package.id
  end || package_options_class.new(package_id: nil)
end