Sha256: 657ec7d66d1ce170ffba4a78b2c6f2610dd4e471692da8c5f249f967c7f42309

Contents?: true

Size: 967 Bytes

Versions: 1

Compression:

Stored size: 967 Bytes

Contents

require 'russianpost/client'
require 'russianpost/operations_factory'
require 'russianpost/barcode'

module RussianPost
  class Parcel
    attr_reader :barcode, :client

    def initialize(barcode, opts = {})
      @barcode = Barcode.new(barcode)
      @client  = (opts[:client] || Client).instance
    end

    def operations
      @operations ||= fetch_operations
    end

    def location
      operations.last.operation_address unless operations.empty?
    end

    def mass
      operations.map{ |o| o.mass }.max
    end

    def rank
      operations.map{ |o| o.mail_rank }.compact.last
    end

    def recipient
      operations.map{ |o| o.rcpn }.compact.last
    end

    def type
      operations.last.mail_type unless operations.empty?
    end

    private

    def fetch_operations
      raise InvalidBarcode unless barcode.valid?
      OperationsFactory.build(client.call(barcode: barcode))
    end
  end

  class InvalidBarcode < ArgumentError; end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
russianpost-0.7.0 lib/russianpost/parcel.rb