Created
February 1, 2022 08:51
-
-
Save rklubenspies/634d8698cb6d6da566ba9e6dab29c4dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "uri" | |
require "net/http" | |
require "json" | |
API_HOST = "ssapi.shipstation.com" | |
API_KEY = "XXX" | |
API_SECRET = "YYY" | |
WEATHER_API_KEY = "ZZZ" | |
def getAccountTags | |
url = URI("https://ssapi.shipstation.com/accounts/listtags") | |
https = Net::HTTP.new(url.host, url.port) | |
https.use_ssl = true | |
request = Net::HTTP::Get.new(url) | |
request["Host"] = API_HOST | |
request.basic_auth(API_KEY,API_SECRET) | |
response = https.request(request) | |
return response.read_body | |
end | |
def getAwaitingShipping | |
url = URI("https://ssapi.shipstation.com/orders?orderStatus=awaiting_shipment&sortBy=OrderDate&sortDir=ASC&page=1&pageSize=5") | |
https = Net::HTTP.new(url.host, url.port) | |
https.use_ssl = true | |
request = Net::HTTP::Get.new(url) | |
request["Host"] = API_HOST | |
request.basic_auth(API_KEY,API_SECRET) | |
response = https.request(request) | |
parsed_json = JSON.parse(response.body) | |
return parsed_json['orders'] | |
end | |
def getOnHold | |
url = URI("https://ssapi.shipstation.com/orders?orderStatus=on_hold&sortBy=OrderDate&sortDir=ASC&page=1&pageSize=500") | |
https = Net::HTTP.new(url.host, url.port) | |
https.use_ssl = true | |
request = Net::HTTP::Get.new(url) | |
request["Host"] = API_HOST | |
request.basic_auth(API_KEY,API_SECRET) | |
response = https.request(request) | |
parsed_json = JSON.parse(response.body) | |
return parsed_json['orders'] | |
end | |
def getWeather(postalCode) | |
url = URI("https://api.weatherapi.com/v1/forecast.json?key=#{WEATHER_API_KEY}&q=#{postalCode}&days=500") | |
https = Net::HTTP.new(url.host, url.port) | |
https.use_ssl = true | |
request = Net::HTTP::Get.new(url) | |
request["Host"] = "api.weatherapi.com" | |
response = https.request(request) | |
parsed_json = JSON.parse(response.body) | |
return parsed_json['forecast']['forecastday'] | |
end | |
def weatherCheck(postalCode, sensitivePlants) | |
forecast = getWeather(postalCode) | |
# if sensitivePlants | |
# # do separate logic for sensitive plants | |
# else | |
# Any low under zero => hold 3 days | |
forecast.each do |day| | |
if day['day']['mintemp_f'] <= 0.0 | |
return "HOLD_3_DAYS" | |
end | |
end | |
# days 2-5 high under 20 => hold 3 days | |
forecast[1..4].each do |day| | |
if day['day']['maxtemp_f'] <= 20.0 | |
return "HOLD_3_DAYS" | |
end | |
end | |
# days 3-6 high under 40 degrees => tag as heat pack likely | |
forecast[2..5].each do |day| | |
if day['day']['maxtemp_f'] <= 40.0 | |
return "HEAT_PACK_LIKELY" | |
end | |
end | |
# days 3-6 high 40 or above, tag no heat pack | |
forecast[2..5].each do |day| | |
if day['day']['maxtemp_f'] > 40.0 | |
return "NO_HEAT_PACK" | |
end | |
end | |
return "NOT_CHECKED" | |
# end | |
end | |
def analyzeOrder(order) | |
# Check if the items have a sensitive SKU and flag order appropriately | |
order['items'].each do |item| | |
if item['sku'].match? /P1004|P1124|P1005|P1106/ | |
# puts "Contains Sensitive SKU" | |
order['sensitivePlants'] = true | |
else | |
order['sensitivePlants'] = false | |
end | |
end | |
# Check if order has postal code and if so check the weather | |
if order['shipTo']['postalCode'] | |
order['status'] = weatherCheck(order['shipTo']['postalCode'], order['sensitivePlants']) | |
else | |
order['status'] = "NOT_CHECKED" | |
end | |
# TODO: Add tag based on status | |
# TODO: Place on hold based on status | |
# TODO: Report what was done to each order (so CC can see what actions were taken) | |
# Output all the orders | |
puts "#{order['orderNumber']}, #{order['customerEmail']}, #{order['status']}, #{order['sensitivePlants']}" | |
end | |
# CONSOLE OUTPUTS BELOW | |
puts "Order Number, Customer Email, Weather Check Status, Contains Sensitive Plants?" | |
getAwaitingShipping.each do |order| | |
analyzeOrder(order) | |
end | |
getOnHold.each do |order| | |
analyzeOrder(order) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment