Class: PaczkomatyInpost::InpostAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/paczkomaty_inpost/inpost_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, data_adapter) ⇒ InpostAPI

Returns a new instance of InpostAPI.



13
14
15
16
17
18
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 13

def initialize(username, password, data_adapter)
  self.data_adapter = data_adapter
  self.request = PaczkomatyInpost::Request.new(username, password)
  inpost_check_environment
  inpost_get_params
end

Instance Attribute Details

#data_adapterObject

Returns the value of attribute data_adapter.



10
11
12
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 10

def data_adapter
  @data_adapter
end

#paramsObject

Returns the value of attribute params.



10
11
12
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 10

def params
  @params
end

#requestObject

Returns the value of attribute request.



10
11
12
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 10

def request
  @request
end

Instance Method Details

#inpost_cancel_pack(packcode) ⇒ Object



144
145
146
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 144

def inpost_cancel_pack(packcode)
  request.cancel_pack(packcode)
end

#inpost_change_packsize(packcode, packsize) ⇒ Object



148
149
150
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 148

def inpost_change_packsize(packcode, packsize)
  request.change_packsize(packcode, packsize)
end

#inpost_check_environmentObject



20
21
22
23
24
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 20

def inpost_check_environment
  raise 'Used data adapter is not compatible with API' unless valid_data_adapter?
  raise 'You must use valid username' if request.username.nil? || request.username == ''
  raise 'Paczkomaty API: password cannot be empty' if request.password.nil? || request.password == ''
end

#inpost_create_customer_partner(options = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 178

def inpost_create_customer_partner(options={})
  customer_options = {:email => nil,
                      :mobile_number => nil,
                      :prefered_box_machine_name => nil,
                      :alternative_box_machine_name => nil,
                      :phone_num => nil,
                      :street => nil,
                      :town => nil,
                      :post_code => nil,
                      :building => nil,
                      :flat => nil,
                      :first_name => nil,
                      :last_name => nil,
                      :company_name => nil,
                      :regon => nil,
                      :nip => nil}.merge!(options)
  if invalid_customer? customer_options
    raise 'Missing data for creating Inpost customer account!'
  else
    request.create_customer_partner(customer_options)
  end
end

#inpost_find_customer(email) ⇒ Object



110
111
112
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 110

def inpost_find_customer(email)
  request.download_customer_preferences(email)
end

#inpost_find_nearest_machines(postcode, paymentavailable = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 92

def inpost_find_nearest_machines(postcode,paymentavailable=nil)
  post_code = postcode.gsub(' ','')
  nearest_machines = request.download_nearest_machines(post_code,paymentavailable)
  cache = data_adapter.cached_machines
  result_list = []

  unless nearest_machines.empty? || cache.empty?
    nearest_machines.each_with_index do |machine, idx|
      result_list << cache.detect {|c| c['name'] == machine[:name]}
      return [] if result_list[idx].nil? # cache is out of date
      result_list[idx]['distance'] = machine[:distance].to_f
    end
    result_list = result_list.sort_by {|k| k['distance']} unless result_list.empty?
  end

  return result_list
end

#inpost_get_cod_report(options = {}) ⇒ Object



201
202
203
204
205
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 201

def inpost_get_cod_report(options={})
  report_options = {:start_date => (DateTime.now - 60),
                    :end_date => (DateTime.now)}.merge!(options)
  request.get_cod_report(report_options[:start_date].strftime("%Y-%m-%d"),report_options[:end_date].strftime("%Y-%m-%d"))
end

#inpost_get_confirm_printout(packcodes, options = {}) ⇒ Object



172
173
174
175
176
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 172

def inpost_get_confirm_printout(packcodes, options = {})
  printout_options = {:printout_path => nil, :test_printout => false}.merge!(options)
  printout = request.get_confirm_printout(packcodes,printout_options[:test_printout])
  file_status(printout, packcodes, printout_options[:printout_path],'confirm_printout')
end

#inpost_get_machine_list(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 59

def inpost_get_machine_list(options={})
  list_options = {:town => nil, :paymentavailable => nil}.merge!(options)
  cache = data_adapter.cached_machines
  result_list = []
  unless cache.empty?
    cache.each do |machine|
      if list_options[:town] && list_options[:paymentavailable].nil?
        result_list << machine if (machine['town'].downcase == list_options[:town].downcase)
      elsif list_options[:town].nil? && !list_options[:paymentavailable].nil?
        result_list << machine if (machine['paymentavailable'] == list_options[:paymentavailable])
      elsif list_options[:town] && !list_options[:paymentavailable].nil?
        result_list << machine if ((machine['town'].downcase == list_options[:town].downcase) && (machine['paymentavailable'] == list_options[:paymentavailable]))
      else
        result_list << machine
      end
    end
  end

  return result_list
end

#inpost_get_pack_status(packcode) ⇒ Object



140
141
142
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 140

def inpost_get_pack_status(packcode)
  request.pack_status(packcode)
end

#inpost_get_packs_by_sender(options = {}) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 207

def inpost_get_packs_by_sender(options={})
  packs_options = {:status => '',
                   :start_date => (DateTime.now - 60),
                   :end_date => DateTime.now,
                   :is_conf_printed => ''}.merge!(options)
  request.get_packs_by_sender(packs_options[:status],packs_options[:start_date].strftime("%Y-%m-%d"),packs_options[:end_date].strftime("%Y-%m-%d"),packs_options[:is_conf_printed])
end

#inpost_get_paramsObject



33
34
35
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 33

def inpost_get_params
  self.params = request.get_params
end

#inpost_get_pricelistObject



80
81
82
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 80

def inpost_get_pricelist
  data_adapter.cached_prices
end

#inpost_get_sticker(packcode, options = {}) ⇒ Object



160
161
162
163
164
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 160

def inpost_get_sticker(packcode, options = {})
  sticker_options = {:sticker_path => nil, :label_type => ''}.merge!(options)
  sticker = request.get_sticker(packcode,sticker_options[:label_type])
  file_status(sticker, packcode, sticker_options[:sticker_path],'sticker')
end

#inpost_get_stickers(packcodes, options = {}) ⇒ Object



166
167
168
169
170
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 166

def inpost_get_stickers(packcodes, options = {})
  sticker_options = {:stickers_path => nil, :label_type => ''}.merge!(options)
  stickers = request.get_stickers(packcodes,sticker_options[:label_type])
  file_status(stickers, packcodes, sticker_options[:stickers_path],'stickers')
end

#inpost_get_townsObject



84
85
86
87
88
89
90
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 84

def inpost_get_towns
  towns = []
  cache = data_adapter.cached_machines
  towns = cache.map{|item| item['town']}.compact.uniq.sort unless cache.empty?

  return towns
end

#inpost_machines_cache_is_valid?(update_params = false) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 37

def inpost_machines_cache_is_valid?(update_params=false)
  inpost_get_params if update_params
  data_adapter.last_update_machines >= params[:last_update]
end

#inpost_pay_for_pack(packcode) ⇒ Object



152
153
154
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 152

def inpost_pay_for_pack(packcode)
  request.pay_for_pack(packcode)
end

#inpost_prepare_pack(temp_id, adresee_email, phone_num, box_machine_name, pack_type, insurance_amount, on_delivery_amount, options = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 114

def inpost_prepare_pack(temp_id, adresee_email, phone_num, box_machine_name, pack_type, insurance_amount, on_delivery_amount, options={})
  pack_options = {:customer_ref => nil, :alternative_box_machine_name => nil, :sender_address => nil,
                  :customer_delivering => nil, :sender_box_machine_name => nil}.merge!(options)
  pack = PaczkomatyInpost::InpostPack.new(:temp_id => temp_id,
                                          :adresee_email => adresee_email,
                                          :sender_email => request.username,
                                          :phone_num => phone_num,
                                          :box_machine_name => box_machine_name,
                                          :alternative_box_machine_name => pack_options[:alternative_box_machine_name],
                                          :customer_delivering => pack_options[:customer_delivering],
                                          :sender_box_machine_name => pack_options[:sender_box_machine_name],
                                          :pack_type => pack_type,
                                          :insurance_amount => insurance_amount,
                                          :on_delivery_amount => on_delivery_amount,
                                          :customer_ref => pack_options[:customer_ref],
                                          :sender_address => pack_options[:sender_address])
  raise "Invalid or missing parameters given when creating inpost pack." unless pack.valid?

  return pack
end

#inpost_prices_cache_is_valid?(update_params = false) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 42

def inpost_prices_cache_is_valid?(update_params=false)
  inpost_get_params if update_params
  data_adapter.last_update_prices >= params[:last_update]
end

#inpost_send_packs(packs_data, options = {}) ⇒ Object



135
136
137
138
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 135

def inpost_send_packs(packs_data, options = {})
  send_options = {:auto_labels => true, :self_send => false}.merge!(options)
  request.send_packs(packs_data, send_options[:auto_labels], send_options[:self_send])
end

#inpost_set_customer_ref(packcode, customer_ref) ⇒ Object



156
157
158
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 156

def inpost_set_customer_ref(packcode, customer_ref)
  request.set_customer_ref(packcode, customer_ref)
end

#inpost_update_machine_list(update_params = false) ⇒ Object



47
48
49
50
51
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 47

def inpost_update_machine_list(update_params=false)
  inpost_get_params if update_params
  data = request.download_machines
  data_adapter.save_machine_list(data, params[:last_update])
end

#inpost_update_price_list(update_params = false) ⇒ Object



53
54
55
56
57
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 53

def inpost_update_price_list(update_params=false)
  inpost_get_params if update_params
  data = request.download_pricelist
  data_adapter.save_price_list(data, params[:last_update])
end

#valid_data_adapter?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 26

def valid_data_adapter?
  data_adapter.respond_to?(:save_machine_list) && data_adapter.respond_to?(:cached_machines) &&
  data_adapter.respond_to?(:save_price_list) && data_adapter.respond_to?(:cached_prices) &&
  data_adapter.respond_to?(:last_update_machines) && data_adapter.respond_to?(:last_update_prices) &&
  data_adapter.respond_to?(:save_pdf)
end