<commit_before>require 'socket'
require 'easypost'
require 'net/http'
require 'uri'

class ShippingLabelJob < ActiveJob::Base
  queue_as :default

  def perform(user_id, shipment_id, format)
    begin
      shipment = Shipment.find(shipment_id)
      courier_data = JSON.parse(shipment.courier_data)
    
      EasyPost.api_key = Cache.setting(shipment.order.domain_id, 'Shipping', 'EasyPost API Key')
      ep_shipment = EasyPost::Shipment.retrieve(courier_data['id'])
      response = ep_shipment.label({'file_format' => format})
      
      # download label
      label_url = response[:postage_label]["label_#{format}_url"]
      label_data = Net::HTTP.get(URI.parse(label_url))
      
      ip_addr = Cache.setting(shipment.order.domain_id, :shipping, 'Thermal Printer IP')
      s = TCPSocket.new(ip_addr, 9100)
      s.send label_data, 0
      s.close
      
      memo = "Shipping label printed to #{ip_addr}"
    rescue => e
      memo = e.message
    end
    
    OrderHistory.create(order_id: shipment.order_id, user_id: user_id, event_type: :shipping_label_print, system_name: 'Rhombus', comment: memo)
  end
end<commit_msg>thermal printer URI<commit_after>require 'socket'
require 'easypost'
require 'net/http'
require 'uri'

class ShippingLabelJob < ActiveJob::Base
  queue_as :default

  def perform(user_id, shipment_id, format)
    begin
      shipment = Shipment.find(shipment_id)
      courier_data = JSON.parse(shipment.courier_data)
    
      EasyPost.api_key = Cache.setting(shipment.order.domain_id, 'Shipping', 'EasyPost API Key')
      ep_shipment = EasyPost::Shipment.retrieve(courier_data['id'])
      response = ep_shipment.label({'file_format' => format})
      
      # download label
      label_url = response[:postage_label]["label_#{format}_url"]
      label_data = Net::HTTP.get(URI.parse(label_url))
      
      uri = URI(Cache.setting(shipment.order.domain_id, :shipping, 'Thermal Printer URI'))
      s = TCPSocket.new(uri.host, uri.port)
      s.send label_data, 0
      s.close
      
      memo = "Shipping label printed to #{ip_addr}"
    rescue => e
      memo = e.message
    end
    
    OrderHistory.create(order_id: shipment.order_id, user_id: user_id, event_type: :shipping_label_print, system_name: 'Rhombus', comment: memo)
  end
end
{'fraction_non_alphanumeric': 0.1137855579868709, 'fraction_numerical': 0.00262582056892779, 'mean_word_length': 3.5447316103379722, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '792338', 'n_tokens_mistral': 780, 'n_tokens_neox': 769, 'n_words': 151}