Class: PaczkomatyInpost::FileAdapter
- Inherits:
-
Object
- Object
- PaczkomatyInpost::FileAdapter
- Defined in:
- lib/paczkomaty_inpost/io_adapters/file_adapter.rb
Instance Attribute Summary collapse
-
#data_path ⇒ Object
Returns the value of attribute data_path.
Instance Method Summary collapse
- #cached_machines ⇒ Object
- #cached_prices ⇒ Object
-
#initialize(data_path) ⇒ FileAdapter
constructor
A new instance of FileAdapter.
- #last_update_machines ⇒ Object
- #last_update_prices ⇒ Object
- #save_machine_list(data, last_update) ⇒ Object
- #save_pdf(file_content, packcodes, path, type) ⇒ Object
- #save_price_list(data, last_update) ⇒ Object
Constructor Details
#initialize(data_path) ⇒ FileAdapter
Returns a new instance of FileAdapter.
10 11 12 13 14 |
# File 'lib/paczkomaty_inpost/io_adapters/file_adapter.rb', line 10 def initialize(data_path) self.data_path = Pathname.new(data_path) validate_path(self.data_path) end |
Instance Attribute Details
#data_path ⇒ Object
Returns the value of attribute data_path.
7 8 9 |
# File 'lib/paczkomaty_inpost/io_adapters/file_adapter.rb', line 7 def data_path @data_path end |
Instance Method Details
#cached_machines ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/paczkomaty_inpost/io_adapters/file_adapter.rb', line 36 def cached_machines data = [] if File.exist?(File.join(data_path, 'machines.dat')) data = JSON.parse(File.read(data_path + 'machines.dat')) end return data end |
#cached_prices ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/paczkomaty_inpost/io_adapters/file_adapter.rb', line 45 def cached_prices data = [] if File.exist?(File.join(data_path, 'prices.dat')) data = JSON.parse(File.read(data_path + 'prices.dat')) end return data end |
#last_update_machines ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/paczkomaty_inpost/io_adapters/file_adapter.rb', line 54 def last_update_machines data = 0 if File.exist?(File.join(data_path, 'machines_date.dat')) data = File.read(data_path + 'machines_date.dat') end return data end |
#last_update_prices ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/paczkomaty_inpost/io_adapters/file_adapter.rb', line 63 def last_update_prices data = 0 if File.exist?(File.join(data_path, 'prices_date.dat')) data = File.read(data_path + 'prices_date.dat') end return data end |
#save_machine_list(data, last_update) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/paczkomaty_inpost/io_adapters/file_adapter.rb', line 16 def save_machine_list(data, last_update) File.open(File.join(data_path, 'machines.dat'), 'w') do |f| f.write data.to_json end File.open(File.join(data_path, 'machines_date.dat'), 'w') do |f| f.write last_update end end |
#save_pdf(file_content, packcodes, path, type) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/paczkomaty_inpost/io_adapters/file_adapter.rb', line 72 def save_pdf(file_content,packcodes,path,type) file_path = get_file_path(path) file_basename = packcodes.kind_of?(Array) ? packcodes.join('.') : packcodes File.open(File.join(file_path, "#{type}.#{file_basename}.pdf"), 'w') do |f| f.write file_content end return true end |
#save_price_list(data, last_update) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/paczkomaty_inpost/io_adapters/file_adapter.rb', line 26 def save_price_list(data, last_update) File.open(File.join(data_path, 'prices.dat'), 'w') do |f| f.write data.to_json end File.open(File.join(data_path, 'prices_date.dat'), 'w') do |f| f.write last_update end end |