Sha256: 66380d73f8b374147ae8c40e50f23d257cd8f05d25128a1470196dc0f90464ad

Contents?: true

Size: 868 Bytes

Versions: 3

Compression:

Stored size: 868 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).new
    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
      OperationsFactory.build(client.call(barcode: barcode))
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
russianpost-0.4.5 lib/russianpost/parcel.rb
russianpost-0.4.4 lib/russianpost/parcel.rb
russianpost-0.4.3 lib/russianpost/parcel.rb