module WeighflowCli::OrderHandler::Handlers

Helpers

Public Instance Methods

build_detail_file_name(order) click to toggle source

Build the file name from the checksum

# 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) click to toggle source

MD5 hash of order detail

# File lib/weighflow_cli/order_handler.rb, line 84
def create_checksum(order)
  Digest::MD5.hexdigest(order.to_json)
end
external_ids(orders) click to toggle source

Pluck the external unique id from the orders

# 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) click to toggle source

return orders that have been cached, but are no longer needed

# 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) click to toggle source

Find a matching index

# 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) click to toggle source

Loop through all indexes. This is memory effecient. Does not load all into memory

# 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() click to toggle source

Data index file name

# 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) click to toggle source

Create the index simple line entry, for speed and change checking by recording an MD5 hash of the contents

# 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) click to toggle source

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
# 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