Class: PocztaPolska::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/poczta_polska/tracker.rb

Overview

The Tracker class gives access to all necessary API methods and wraps the results in correct Ruby classes.

Instance Method Summary collapse

Constructor Details

#initialize(wsdl, username, password) ⇒ Tracker


7
8
9
# File 'lib/poczta_polska/tracker.rb', line 7

def initialize(wsdl, username, password)
  @client = Savon.client(wsdl: wsdl, wsse_auth: [username, password])
end

Instance Method Details

#api_versionObject

Returns the API version you are using.


12
13
14
15
# File 'lib/poczta_polska/tracker.rb', line 12

def api_version
  resp = @client.call(:wersja)
  resp.body[:wersja_response][:return].to_s
end

#check(package_id, details = false) ⇒ Package

Finds a package by its ID.

Raises:


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/poczta_polska/tracker.rb', line 32

def check(package_id, details = false)
  method_name = 'sprawdz_przesylke'
  method_name << '_pl' if details

  method = method_name.to_sym
  response_key = "#{method_name}_response".to_sym

  response = @client.call(method, message: {numer: package_id})
  data = response.body[response_key][:return]

  case data[:status].to_i
  when -1 then raise UnknownPackageError
  when -2 then raise WrongPackageError
  when -99 then raise Error
  end

  Package.new(data)
end

#check_many(package_ids, details = false, date_range = nil) ⇒ Array<Package>

Finds multiple packages by their IDs.

Raises:


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/poczta_polska/tracker.rb', line 60

def check_many(package_ids, details = false, date_range = nil)
  method_name = 'sprawdz_przesylki'
  method_name << '_od_do' unless date_range.nil?
  method_name << '_pl' if details

  method = method_name.to_sym
  response_key = "#{method_name}_response".to_sym

  params = {numery: package_ids}
  unless date_range.nil?
    params[:od_dnia] = date_range.first.iso8601
    params[:do_dnia] = date_range.end.iso8601
  end

  response = @client.call(method, message: params)
  data = response.body[response_key][:return]

  case data[:status].to_i
  when -1 then raise TooManyPackagesError
  when -2 then raise ManyPackagesForbiddenError
  when -3 then raise DateRangeError
  when -99 then raise Error
  end

  data[:przesylki].map { |p| Package.new(p) }
end

#max_packagesObject

Returns the maximum number of packages that you can check in a single request with #check_many method. If you are not authenticated, it is always 1.


20
21
22
23
# File 'lib/poczta_polska/tracker.rb', line 20

def max_packages
  resp = @client.call(:maksymalna_liczba_przesylek)
  resp.body[:maksymalna_liczba_przesylek_response][:return].to_i
end