Module: WeighflowCli::OrderHandler::Handlers
- Included in:
- Finder, OrderRepository
- Defined in:
- lib/weighflow_cli/order_handler.rb
Overview
Helpers
Defined Under Namespace
Classes: OrderIndexResult
Instance Method Summary collapse
-
#build_detail_file_name(order) ⇒ Object
Build the file name from the checksum.
-
#create_checksum(order) ⇒ Object
MD5 hash of order detail.
-
#external_ids(orders) ⇒ Object
Pluck the external unique id from the orders.
-
#find_deletes(orders) ⇒ Object
return orders that have been cached, but are no longer needed.
-
#find_index(index) ⇒ Object
Find a matching index.
-
#for_each_index(&block) ⇒ Object
Loop through all indexes.
-
#index_file_name ⇒ Object
Data index file name.
-
#index_line(order, file_name) ⇒ Object
Create the index simple line entry, for speed and change checking by recording an MD5 hash of the contents.
-
#store_in_cache(orders) ⇒ Object
Store all orders in data path - Saves the entire contents in separate file - Records the index of the filename with a checksum for detail reference.
Instance Method Details
#build_detail_file_name(order) ⇒ Object
Build the file name from the checksum
98 99 100 |
# File 'lib/weighflow_cli/order_handler.rb', line 98 def build_detail_file_name(order) File.join(WeighflowCli.data_path, 'order_' + create_checksum(order) + '.json') end |
#create_checksum(order) ⇒ Object
MD5 hash of order detail
84 85 86 |
# File 'lib/weighflow_cli/order_handler.rb', line 84 def create_checksum(order) Digest::MD5.hexdigest(order.to_json) end |
#external_ids(orders) ⇒ Object
Pluck the external unique id from the orders
138 139 140 |
# File 'lib/weighflow_cli/order_handler.rb', line 138 def external_ids(orders) orders.map { |order| order[:external_unique_id].to_s } end |
#find_deletes(orders) ⇒ Object
return orders that have been cached, but are no longer needed
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/weighflow_cli/order_handler.rb', line 145 def find_deletes(orders) unique_ids = external_ids(orders) deletes = [] for_each_index do |index_result| unless unique_ids.include?(index_result.index) deletes << index_result.data index_result.delete! end end deletes end |
#find_index(index) ⇒ Object
Find a matching index
130 131 132 133 134 |
# File 'lib/weighflow_cli/order_handler.rb', line 130 def find_index(index) for_each_index do |compare_index| return compare_index if compare_index.index == index end end |
#for_each_index(&block) ⇒ Object
Loop through all indexes. This is memory effecient. Does not load all into memory
120 121 122 123 124 125 126 |
# File 'lib/weighflow_cli/order_handler.rb', line 120 def for_each_index(&block) return unless File.exists?(index_file_name) File.foreach(index_file_name) do |li| compare_index, checksum, stored_json_file_name = li.chomp!.split('|') block.call OrderIndexResult.new(index: compare_index, checksum: checksum, file_name: stored_json_file_name) end end |
#index_file_name ⇒ Object
Data index file name
77 78 79 |
# File 'lib/weighflow_cli/order_handler.rb', line 77 def index_file_name @index_file_name ||= File.join(WeighflowCli.data_path, 'orders_index_cache') end |
#index_line(order, file_name) ⇒ Object
Create the index simple line entry, for speed and change checking by recording an MD5 hash of the contents
91 92 93 |
# File 'lib/weighflow_cli/order_handler.rb', line 91 def index_line(order, file_name) order[:external_unique_id] + "|#{create_checksum(order)}|#{file_name}" end |
#store_in_cache(orders) ⇒ Object
Store all orders in data path
- Saves the entire contents in separate file
- Records the index of the filename with a checksum for detail reference
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/weighflow_cli/order_handler.rb', line 107 def store_in_cache(orders) File.open(index_file_name, 'w') do |index_file| # process each order orders.each do |order| detail_file_name = build_detail_file_name(order) File.write(detail_file_name, JSON.pretty_generate(order)) index_file.puts(index_line(order, detail_file_name)) end end end |