Class: PaczkomatyInpost::InpostAPI
- Inherits:
-
Object
- Object
- PaczkomatyInpost::InpostAPI
- Defined in:
- lib/paczkomaty_inpost/inpost_api.rb
Instance Attribute Summary collapse
-
#data_adapter ⇒ Object
Returns the value of attribute data_adapter.
-
#params ⇒ Object
Returns the value of attribute params.
-
#request ⇒ Object
Returns the value of attribute request.
Instance Method Summary collapse
-
#initialize(username, password, data_adapter) ⇒ InpostAPI
constructor
A new instance of InpostAPI.
- #inpost_cancel_pack(packcode) ⇒ Object
- #inpost_change_packsize(packcode, packsize) ⇒ Object
- #inpost_check_environment ⇒ Object
- #inpost_create_customer_partner(options = {}) ⇒ Object
- #inpost_find_customer(email) ⇒ Object
- #inpost_find_nearest_machines(postcode, paymentavailable = nil) ⇒ Object
- #inpost_get_cod_report(options = {}) ⇒ Object
- #inpost_get_confirm_printout(packcodes, options = {}) ⇒ Object
- #inpost_get_machine_list(options = {}) ⇒ Object
- #inpost_get_pack_status(packcode) ⇒ Object
- #inpost_get_packs_by_sender(options = {}) ⇒ Object
- #inpost_get_params ⇒ Object
- #inpost_get_pricelist ⇒ Object
- #inpost_get_sticker(packcode, options = {}) ⇒ Object
- #inpost_get_stickers(packcodes, options = {}) ⇒ Object
- #inpost_get_towns ⇒ Object
- #inpost_machines_cache_is_valid?(update_params = false) ⇒ Boolean
- #inpost_pay_for_pack(packcode) ⇒ Object
- #inpost_prepare_pack(temp_id, adresee_email, phone_num, box_machine_name, pack_type, insurance_amount, on_delivery_amount, options = {}) ⇒ Object
- #inpost_prices_cache_is_valid?(update_params = false) ⇒ Boolean
- #inpost_send_packs(packs_data, options = {}) ⇒ Object
- #inpost_set_customer_ref(packcode, customer_ref) ⇒ Object
- #inpost_update_machine_list(update_params = false) ⇒ Object
- #inpost_update_price_list(update_params = false) ⇒ Object
- #valid_data_adapter? ⇒ Boolean
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_adapter ⇒ Object
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 |
#params ⇒ Object
Returns the value of attribute params.
10 11 12 |
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 10 def params @params end |
#request ⇒ Object
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_environment ⇒ Object
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(={}) = {: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!() if invalid_customer? raise 'Missing data for creating Inpost customer account!' else request.create_customer_partner() 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(={}) = {:start_date => (DateTime.now - 60), :end_date => (DateTime.now)}.merge!() request.get_cod_report([:start_date].strftime("%Y-%m-%d"),[: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, = {}) = {:printout_path => nil, :test_printout => false}.merge!() printout = request.get_confirm_printout(packcodes,[:test_printout]) file_status(printout, packcodes, [: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(={}) = {:town => nil, :paymentavailable => nil}.merge!() cache = data_adapter.cached_machines result_list = [] unless cache.empty? cache.each do |machine| if [:town] && [:paymentavailable].nil? result_list << machine if (machine['town'].downcase == [:town].downcase) elsif [:town].nil? && ![:paymentavailable].nil? result_list << machine if (machine['paymentavailable'] == [:paymentavailable]) elsif [:town] && ![:paymentavailable].nil? result_list << machine if ((machine['town'].downcase == [:town].downcase) && (machine['paymentavailable'] == [: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(={}) = {:status => '', :start_date => (DateTime.now - 60), :end_date => DateTime.now, :is_conf_printed => ''}.merge!() request.get_packs_by_sender([:status],[:start_date].strftime("%Y-%m-%d"),[:end_date].strftime("%Y-%m-%d"),[:is_conf_printed]) end |
#inpost_get_params ⇒ Object
33 34 35 |
# File 'lib/paczkomaty_inpost/inpost_api.rb', line 33 def inpost_get_params self.params = request.get_params end |
#inpost_get_pricelist ⇒ Object
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, = {}) = {:sticker_path => nil, :label_type => ''}.merge!() sticker = request.get_sticker(packcode,[:label_type]) file_status(sticker, packcode, [: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, = {}) = {:stickers_path => nil, :label_type => ''}.merge!() stickers = request.get_stickers(packcodes,[:label_type]) file_status(stickers, packcodes, [:stickers_path],'stickers') end |
#inpost_get_towns ⇒ Object
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
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, ={}) = {:customer_ref => nil, :alternative_box_machine_name => nil, :sender_address => nil, :customer_delivering => nil, :sender_box_machine_name => nil}.merge!() 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 => [:alternative_box_machine_name], :customer_delivering => [:customer_delivering], :sender_box_machine_name => [:sender_box_machine_name], :pack_type => pack_type, :insurance_amount => insurance_amount, :on_delivery_amount => on_delivery_amount, :customer_ref => [:customer_ref], :sender_address => [: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
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, = {}) = {:auto_labels => true, :self_send => false}.merge!() request.send_packs(packs_data, [:auto_labels], [: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
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 |