#!/usr/bin/env ruby require 'optparse' require 'gls_agent' require 'rainbow' def puts_error message STDERR.puts Rainbow("Error: #{message}").red end def puts_info message puts Rainbow(message).yellow end def puts_success message puts Rainbow("Success: #{message}").green end options = {} optparse = OptionParser.new do |opts| opts.banner = "Usage: gls_create_label [OPTIONS] [FILE]\n Register and download a GLS parcel sticker." opts.separator "" opts.separator "GLS login options" opts.on('-u', '--gls-user GLSUSER', 'username of GLS account.') do |u| options[:user] = u end opts.on('-p', '--gls-password GLSPASSWORD', 'password of GLS account.') do |p| options[:pass] = p end opts.separator "" opts.separator "Label data definition" opts.on('-l', '--label-data DATA', 'Label data, comma-separated.') do |l| fields = l.split(',') if fields.length != 8 puts_error 'Error: label data has to be in format "01.01.2016,John Doe,Company,Home Street,1,1234,City,1".' exit 1 end options[:date] = fields.shift options[:name] = fields.shift options[:company_name] = fields.shift options[:street] = fields.shift options[:streetno] = fields.shift options[:zip] = fields.shift options[:city] = fields.shift options[:weight] = fields.shift end opts.separator "" opts.separator "Output options" opts.on('-o', '--output-file FILE', 'Write output (pdf) to FILE, defaults to gls-label.pdf .') do |o| options[:output_file] = o end opts.separator "" opts.separator "General options" opts.on_tail('--version', 'Show version.') do puts "gls_create_label #{GLSAgent::VERSION}" exit 0 end opts.on('-h', '--help', 'Show help.') do puts opts exit 0 end end optparse.parse! defaults = GLSAgent::Dotfile::get_opts options = defaults.merge options if !options[:user] || !options[:pass] puts_error 'Need to specify user and pass in ~/.gls_agent or -u -p .' exit 1 end if ARGV.empty? && (!options[:date] || !options[:name] || !options[:street] || !options[:streetno] || !options[:zip] || !options[:city] || !options[:weight]) puts_error 'Need to specify delivery data in ~/.gls_agent or -l.' exit 1 end options[:output_file] ||= 'gls-label.pdf' mech = GLSMech.new mech.user = options[:user] mech.pass = options[:pass] if !ARGV.empty? parcels = File.readlines(ARGV[0]).map do |line| GLSAgent::job_from_csv line end filenames = parcels.map do |parcel| "#{parcel.name.gsub(/\W+/,'_')}.pdf" end mech.save_parcel_labels(parcels, filenames) do |error,saved_as| if error puts_error error else puts_info "Saved #{saved_as}." end end else parcel = GLSAgent::job_from_hash(options) begin pdf_file_path = mech.save_parcel_label parcel, options[:output_file] rescue GLSAgent::GLSEndpointError => error puts_error "The GLS Endpoint answered:\n#{error.message}" exit 3 end if !pdf_file_path puts_error "Could not login or navigate GLS page." exit 2 end if pdf_file_path != options[:output_file] puts_info "Warning: #{options[:output_file]} already exists, saved to #{pdf_file_path} instead!." else puts_success "Saved label to #{pdf_file_path}." end end # Be cool, explicitely. exit 0