Class: SpeedeeService
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- SpeedeeService
- Defined in:
- app/models/speedee_service.rb
Overview
== Schema Information
Table name: speedee_services
Database name: primary
id :integer not null, primary key
days_in_transit_from_60047 :integer not null
is_active :boolean default(TRUE), not null
sort_code :integer not null
zip :integer not null
Indexes
zip_is_active (zip,is_active)
Constant Summary collapse
- FUEL_SURCHARGE_API_URL =
Speedee has a basic web call (not quite API/web service) that provides a weekly fuel surcharge rate, with some history.
"http://speedeedelivery.com/files/fs.ini"- FALLBACK_FUEL_SURCHARGE_RATE =
Fallback fuel surcharge rate.
0.07- SERVICE_STATES =
Spee-Dee's regional footprint (upper Midwest). A near-instant, in-memory
pre-filter so we never pay a Dispatch Science API round-trip for obviously
out-of-region destinations (CA, AK, …). Coarser than the zip tables — a
state's coverage is partial — so this only decides "worth asking the API",
not final eligibility. Phase 2 keeps this as the primary gate.
https://speedeedelivery.com/services/service-area-maps/ %w[IL IN IA KS MI MN MO ND NE SD WI].to_set.freeze
Constants included from Models::Schedulable
Models::Schedulable::SIMPLE_FORM_OPTIONS
Class Method Summary collapse
-
.by_zip ⇒ ActiveRecord::Relation<SpeedeeService>
A relation of SpeedeeServices that are by zip.
-
.delivery_type_code(options) ⇒ String, Integer
Maps shipment options to the two-digit Speedee delivery type code.
-
.generate_speedee_barcode(tracking_number) ⇒ String
PNG image data.
- .get_fuel_surcharge_rate ⇒ Hash{String=>Object}
-
.get_latest_fuel_surcharge_rate ⇒ Hash{Symbol=>Object}
Status, rate, and message.
-
.get_tracking_number(container_code, options) ⇒ String
Builds the Speedee tracking number: 'SP' + account number (6) + delivery type code (2) + container code (10).
-
.is_active ⇒ ActiveRecord::Relation<SpeedeeService>
A relation of SpeedeeServices that are is active.
- .serviceable_state?(state_code) ⇒ Boolean
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Models::Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Class Method Details
.by_zip ⇒ ActiveRecord::Relation<SpeedeeService>
A relation of SpeedeeServices that are by zip. Active Record Scope
32 |
# File 'app/models/speedee_service.rb', line 32 scope :by_zip, ->(zip) { where(zip: zip).is_active } |
.delivery_type_code(options) ⇒ String, Integer
Maps shipment options to the two-digit Speedee delivery type code.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'app/models/speedee_service.rb', line 110 def self.delivery_type_code() # Specifications for Delivery Type Codes # The following is a list of defined Delivery Type Codes. The placement of the two digit code should be directly after the shipper number in the 9th and 10th position. For shipper number 6-981, delivery type 03, package ID 12345678. The barcode should read. # SP 006981 03 12345678 # 72 - COD (No special code for type of cash) (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 # 73 – COD, CASHIERS CK/MO ONLY (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 # Line 2: CASHIERS CK/MO ONLY # 74 – COD/AOD, Adult Signature Required (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 # Line 2: AOD ADULT SIGNATURE REQUIRED # 75 - COD/AOD, CASHIERS CK/MO, Adult Signature Required (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 CASHIERS CK/MO ONLY # Line 2: AOD ADULT SIGNATURE REQUIRED # 76 – COD/AOD, Indirect Signature Required (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 # Line 2: INDIRECT AOD-SIGNATURE REQ'D # 77 – COD/AOD, CASHIERS CK/MO ONLY, Indirect Signature Required (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 CASHIERS CK/MO ONLY # Line 2: INDIRECT AOD-SIGNATURE REQ'D # 78 – COD/AOD, Direct Signature Required (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 # Line 2: DIRECT AOD-SIG REQ'D AT ADDR # 79 – COD/AOD, CASHIERS CK/MO ONLY, Direct Signature Required (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 CASHIERS CK/MO ONLY # Line 2: DIRECT AOD-SIG REQ'D AT ADDR # C4 – COD/AOD, Adult Signature Required - With Signature Card (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 # Line 2: AOD ADULT SIGNATURE REQ’D W/TAG # C5 - COD/AOD, CASHIERS CK/MO, Adult Signature Required - With Signature Card # (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 CASHIERS CK/MO ONLY # Line 2: AOD ADULT SIGNATURE REQ’D W/TAG # C6 – COD/AOD, Indirect Signature Required - With Signature Card # (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 # Line 2: INDIRECT AOD SIGNATURE REQ’D W/TAG # C7 – COD/AOD, CASHIERS CK/MO ONLY, Indirect Signature Required - With Signature Card # (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 CASHIERS CK/MO ONLY # Line 2: INDIRECT AOD SIGNATURE REQ’D W/TAG # C8 – COD/AOD, Direct Signature Required - With Signature Card # (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 # Line 2: DIRECT AOD-SIG REQ'D AT ADDR W/TAG # C9 – COD/AOD, CASHIERS CK/MO ONLY, Direct Signature Required - With Signature Card # (Oversized/Hazardous Available) # Print on Service Detail of Label – Line 1: COD $500.00 CASHIERS CK/MO ONLY # Line 2: DIRECT AOD-SIG REQ'D AT ADDR WTAG # 41 – Acknowledgement of Delivery - Adult Signature Required (Oversized/Hazardous Available) # ** THIS DELIVERY TYPE IS DEPRICATED – USE TYPE 47 ** # Print on Service Detail of Label – Line 1: AOD # Line 2: ADULT SIGNATURE REQUIRED # 42 - Acknowledgement of Delivery - Signature Required (Oversized/Hazardous Available) # ** THIS DELIVERY TYPE IS DEPRICATED – USE TYPE 43 or 45 ** # Print on Service Detail of Label – Line 1: AOD # Line 2: SIGNATURE REQUIRED # 43 - Acknowledgement of Delivery – Indirect Delivery # Print on Service Detail of Label – Line 1: AOD # Line 2: SIGNATURE REQ’D # 44 - Acknowledgement of Delivery – Indirect Delivery – With Signature Card # Print on Service Detail of Label – Line 1: AOD # Line 2: SIGNATURE REQ’D W/TAG # 45 - Acknowledgement of Delivery – Direct Delivery # Print on Service Detail of Label – Line 1: AOD # Line 2: SIGNATURE REQ’D AT ADDR # 46 - Acknowledgement of Delivery – Direct Delivery – With Signature Card # Print on Service Detail of Label – Line 1: AOD # Line 2: SIGNATURE REQ’D AT ADDR W/TAG # 47 - Acknowledgement of Delivery – Adult Signature Required # Print on Service Detail of Label – Line 1: AOD # Line 2: ADULT SIGNATURE REQ’D # 48 - Acknowledgement of Delivery – Adult Signature Required – With Signature Card # Print on Service Detail of Label – Line 1: AOD # Line 2: ADULT SIGNATURE REQ’D W/TAG # 26 - 3 Attempts Pickup Tag (Oversized/Hazardous Available, Declared Value/COD/AOD not Available) # 52 – Only Hazardous Materials: (Oversized Available) # 51 – Only Oversized Packages: (No Other Options) # 12 – LTL order (No Other Options) # 06 - EZR: Easy Return Label available by special arrangement through Sales Dept. (No Other Options) # 03 - Standard Package (No Other Options) # The following identifiers need to be printed in the Service Summary area on the label if used: COD, AOD, HAZ, OVR. (Explained in more detail on Delivery Label Documentation) code = "03" # default # we do NOT support COD for Speedee or any other carrier # if options[:cod_amount].to_f > 0.0 # # COD # code = 72 # COD (No special code for type of cash) # if options[:cod_collection_type] == 'no_check' # # CASHIERS CK/MO ONLY # code = 73 # COD, CASHIERS CK/MO ONLY # if options[:signature_confirmation] # # AOD # code = 79 # COD/AOD, CASHIERS CK/MO ONLY, Direct Signature Required # end # else # if options[:signature_confirmation] # # AOD # code = 78 # COD/AOD, Direct Signature Required # end # end # else if [:signature_confirmation] code = 45 # Acknowledgement of Delivery – Direct Delivery elsif [:oversize] code = 51 end # end code end |
.generate_speedee_barcode(tracking_number) ⇒ String
Returns PNG image data.
224 225 226 227 228 229 230 231 232 233 |
# File 'app/models/speedee_service.rb', line 224 def self.(tracking_number) raise "Tracking number is invalid, must be 32 digits or fewer long" unless tracking_number&.size&.<= 32 # file_path ||= Rails.root.join(Rails.application.config.x.temp_storage_path.to_s, "gs1-128-#{id}-#{Time.current.to_i}#{rand(1000)}.png") require 'zint' = Zint::Barcode.new(value: tracking_number, symbology: Zint::Constants::Symbologies::BARCODE_CODE128) .scale = 2 .show_hrt = 0 # barby rendered bars only; keep it that way .to_memory_file(extension: '.png') end |
.get_fuel_surcharge_rate ⇒ Hash{String=>Object}
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/speedee_service.rb', line 41 def self.get_fuel_surcharge_rate res = {} cache_key = "speedee_delivery_fuel_surcharge_rate_data:#{Date.current.year}_week_#{Date.current.cweek}" fuel_surcharge_rate_data = Rails.cache.fetch(cache_key, expires_in: 1.week) do rate = nil rate_res = SpeedeeService.get_latest_fuel_surcharge_rate rate = rate_res[:rate] if rate_res[:status] == :ok rate ||= FALLBACK_FUEL_SURCHARGE_RATE res['rate'] = rate res['status'] = rate_res[:status].to_s res['error_message'] = rate_res[:message].to_s if rate_res[:status] != :ok res end if fuel_surcharge_rate_data['status'] != "ok" # error, do not cache fuel_surcharge_rate_data['error_message'] ||= "Could not get DOE weekly national fuel rate" Rails.cache.delete(cache_key) end fuel_surcharge_rate_data end |
.get_latest_fuel_surcharge_rate ⇒ Hash{Symbol=>Object}
Returns status, rate, and message.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/speedee_service.rb', line 63 def self.get_latest_fuel_surcharge_rate response = "" begin Timeout.timeout(20) do Rails.logger.info "get_latest_fuel_surcharge_rate: attempting to get FUEL_SURCHARGE_API_URL: #{SpeedeeService::FUEL_SURCHARGE_API_URL}" response = HTTP.get(SpeedeeService::FUEL_SURCHARGE_API_URL.to_s).body Rails.logger.info "get_latest_fuel_surcharge_rate: response: #{response}" nil status = :error = "No rate returned" if (rate = response.to_s.split("=").last.to_f) > 0.0 status = :ok = "" end result = { rate: rate, status: status, message: } return result end rescue StandardError => e = "Timeout trying to reach Speedee Service #{SpeedeeService::FUEL_SURCHARGE_API_URL}, response (if any): #{response}, Error: #{e}. " Rails.logger.error " * #{}" ErrorReporting.warning() { rate: nil, status: :timeout, message: "Error: #{e}. Speedee Service servers are not responding." } end end |
.get_tracking_number(container_code, options) ⇒ String
Builds the Speedee tracking number: 'SP' + account number (6) +
delivery type code (2) + container code (10).
98 99 100 101 |
# File 'app/models/speedee_service.rb', line 98 def self.get_tracking_number(container_code, ) code = container_code.presence || "UNKNOWN" "SP#{"000000#{Heatwave::Configuration.fetch(:speedee_delivery_account_number)}".last(6)}#{SpeedeeService.delivery_type_code().to_s.last(2)}#{code.last(10)}" end |
.is_active ⇒ ActiveRecord::Relation<SpeedeeService>
A relation of SpeedeeServices that are is active. Active Record Scope
31 |
# File 'app/models/speedee_service.rb', line 31 scope :is_active, -> { where(is_active: true) } |
.serviceable_state?(state_code) ⇒ Boolean
36 37 38 |
# File 'app/models/speedee_service.rb', line 36 def self.serviceable_state?(state_code) SERVICE_STATES.include?(state_code.to_s.upcase) end |