Sha256: f3f4c1950d2939c46236d8ff9199adfb3370b88bf072bb9781d8a418033241dd

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

#!/usr/bin/env ruby

require 'date'

require "rawbotz"

require 'optparse'

options = {config: RawgentoModels::CONF_FILE}

optparse = OptionParser.new do |opts|
  opts.banner = "Usage: #{$PROGRAM_NAME} [OPTIONS]"
  opts.separator ""

  opts.separator "RawgentoDB configuration file (MySQL options)"
  opts.on("-c", "--config FILE", 'file path to rawgento-db YAML config file.') do |c|
    if !File.exist? c
      STDERR.puts "Cannot load conf file #{c}"
      exit 2
    end

    options[:config] = c
  end

  opts.separator ""
  opts.separator "Output options"
  opts.on("-v", "--verbose", 'print debug output (WARNING: including PASSWORD)') do |v|
    $stdout.sync = true
    options[:verbose] = true
  end

  opts.separator ""
  opts.separator "General"
  opts.on_tail('--version', 'Show version.') do
    puts "#{$PROGRAM_NAME} #{Rawbotz::VERSION}"
    exit 0
  end
  opts.on('-h', '--help', 'Show this help.') do
    puts opts
    exit 0
  end
end
optparse.parse!

include RawgentoModels

def main options
  logger = Logger.new(STDOUT)
  logger.level = options[:verbose] ? Logger::DEBUG : Logger::INFO

  logger.debug "#{$PROGRAM_NAME} #{Rawbotz::VERSION}"

  if !File.exist? options[:config]
    logger.warn "Cannot load conf file #{options[:config]}"
    exit 2
  end

  RawgentoModels.establish_connection options[:config]

  begin
    result = RawgentoDB::Query.stock(RawgentoDB.settings(options[:config]))
  rescue Mysql2::Error => e
    logger.error "Problems accessing MySQL database #{e.inspect}"
    exit 2
  end

  now = DateTime.now

  logger.info("Starting stock update")

  result.each do |s|
    product = RawgentoModels::LocalProduct.find_by(product_id: s.product_id)
    if product.nil?
      logger.info("No such product #{s.product_id}")
    else
      product.stock_items.create(qty: s.qty, date: now)
      logger.info("Updated stock for #{s.product_id}")
    end
  end

  logger.info("Finished with stock update")
end

main options

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rawbotz-0.1.5 exe/rawbotz_stock_update
rawbotz-0.1.4 exe/rawbotz_stock_update
rawbotz-0.1.3 exe/rawbotz_stock_update
rawbotz-0.1.2 exe/rawbotz_stock_update