<commit_before>#!/usr/bin/env ruby
require 'easypost'
require 'yaml'
require 'slop'

require 'dotenv'
Dotenv.load

opts = Slop.parse do |o|
  o.string '-f', '--file', 'a postage config file'
  o.bool '--buy', 'Actually buy the label'
  o.bool '--test', 'Use the testing environment'
end

unless opts[:file]
  puts opts
  exit
end

if opts[:test]
  # Test Key
  EasyPost.api_key = ENV['EASYPOST_TEST_KEY']
else
  # LIVE KEY!!
  EasyPost.api_key = ENV['EASYPOST_PROD_KEY']
end

class ShippingConfig
  attr_reader :from, :to, :package, :customs_info, :customs_items

  def initialize(file)
    @config = YAML.load_file(file)
    @from = @config['from']
    @to = @config['to']
    @package = @config['package']
    @customs_info = @config['customs_info']
    @customs_items = @config['customs_items']
  end
end

shipping_config = ShippingConfig.new(opts[:file])

from_address = EasyPost::Address.create(shipping_config.from)
to_address = EasyPost::Address.create(shipping_config.to)
parcel = EasyPost::Parcel.create(shipping_config.package)

customs_form = unless shipping_config.customs_info.nil?
                 customs_items = if shipping_config.customs_items
                                   shipping_config.customs_items.map do |item|
                                     EasyPost::CustomsItem.create(item)
                                   end
                                 end
                 combined = shipping_config.customs_info
                 combined[:customs_items] = customs_items
                 EasyPost::CustomsInfo.create(combined)
               end
shipment = EasyPost::Shipment.create(
  to_address: to_address,
  from_address: from_address,
  parcel: parcel,
  customs_info: customs_form
)

unless opts[:buy]
  to_addr = shipping_config.to
  lowest = shipment.lowest_rate
  puts "to: #{to_addr['name']} in #{to_addr['city']}, #{to_addr['state']}",
       "rate: #{lowest[:rate]}",
       "carrier: #{lowest[:carrier]}"
  exit
end

shipment.buy(
  rate: shipment.lowest_rate
)
shipment.label('file_format' => 'pdf')

puts "Tracking Code:    #{shipment.tracking_code}"
puts "Shipping Label:   #{shipment.postage_label.label_pdf_url}"
puts "Shipment ID Code: #{shipment.id}"
<commit_msg>Simplify processing using the command pattern<commit_after>#!/usr/bin/env ruby
require 'easypost'
require 'yaml'
require 'slop'

require 'dotenv'
Dotenv.load

opts = Slop.parse do |o|
  o.string '-f', '--file', 'a postage config file'
  o.bool '--buy', 'Actually buy the label'
  o.bool '--test', 'Use the testing environment'
end

unless opts[:file]
  puts opts
  exit
end

if opts[:test]
  # Test Key
  EasyPost.api_key = ENV['EASYPOST_TEST_KEY']
else
  # LIVE KEY!!
  EasyPost.api_key = ENV['EASYPOST_PROD_KEY']
end

# Loads and provides access to the shipping config
class ShippingConfig
  attr_reader :from, :to, :package, :customs_info, :customs_items

  def initialize(file)
    @config = YAML.load_file(file)
    @from = @config['from']
    @to = @config['to']
    @package = @config['package']
    @customs_info = @config['customs_info']
    @customs_items = @config['customs_items']
  end
end

# Checks the price for a shipment
class CheckPriceCommand
  def initialize(config, shipment)
    @config = config
    @shipment = shipment
  end

  def run
    to_addr = @config.to
    lowest = @shipment.lowest_rate
    puts "to: #{to_addr['name']} in #{to_addr['city']}, #{to_addr['state']}",
         "rate: #{lowest[:rate]}",
         "carrier: #{lowest[:carrier]}"
  end
end

# Purchases Postage for a shipment
class PurchasePostageCommand
  def initialize(shipment)
    @shipment = shipment
  end

  def run
    @shipment.buy(
      rate: @shipment.lowest_rate
    )
    @shipment.label('file_format' => 'pdf')

    puts "Tracking Code:    #{@shipment.tracking_code}"
    puts "Shipping Label:   #{@shipment.postage_label.label_pdf_url}"
    puts "Shipment ID Code: #{@shipment.id}"
  end
end

shipping_config = ShippingConfig.new(opts[:file])

from_address = EasyPost::Address.create(shipping_config.from)
to_address = EasyPost::Address.create(shipping_config.to)
parcel = EasyPost::Parcel.create(shipping_config.package)

customs_form = unless shipping_config.customs_info.nil?
                 customs_items = if shipping_config.customs_items
                                   shipping_config.customs_items.map do |item|
                                     EasyPost::CustomsItem.create(item)
                                   end
                                 end
                 combined = shipping_config.customs_info
                 combined[:customs_items] = customs_items
                 EasyPost::CustomsInfo.create(combined)
               end
shipment = EasyPost::Shipment.create(
  to_address: to_address,
  from_address: from_address,
  parcel: parcel,
  customs_info: customs_form
)

if opts[:buy]
  PurchasePostageCommand.new(shipment)
else
  CheckPriceCommand.new(shipping_config, shipment)
end.run

{'fraction_non_alphanumeric': 0.11802748585286985, 'fraction_numerical': 0.0, 'mean_word_length': 3.100248550124275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1401140', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1628, 'n_words': 350}