Skip to content

Instantly share code, notes, and snippets.

@groob
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groob/765620f5c06eb2c9957d to your computer and use it in GitHub Desktop.
Save groob/765620f5c06eb2c9957d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# File: Warranty.rb
#
# Decription: Contact's Apple's selfserve servers to capture warranty
# information about your product. Accepts arguments of
# machine serial numbers.
#
# Author: Gary Larizza
# Last Modified: 8/13/2012
# Why: Apple hates APIs
require 'uri'
require 'net/http'
require 'net/https'
require 'date'
def get_warranty(serial)
# Setup HTTP connection
uri = URI.parse('https://selfsolve.apple.com/wcResults.do')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
# Prepare POST data
request.set_form_data(
{
'sn' => serial,
'Continue' => 'Continue',
'cn' => '',
'locale' => '',
'caller' => '',
'num' => '0'
}
)
# POST data and get the response
response = http.request(request)
response_data = response.body
# I apologize for this line
warranty_status = response_data.split('warrantyPage.warrantycheck.displayHWSupportInfo').last.split('Repairs and Service Coverage: ')[1] =~ /^Active/ ? true : false
model = response_data.split('warrantyPage.warrantycheck.displayProductInfo').last.split('false')[0].split("\', \'")[1]
unless response_data.match("Portable Computers")
configuration = response_data.split('warrantyPage.warrantycheck.displayProductInfo').last.split('false')[0].split("\', \'")[1].match("iMac|Xserve|mini")
else
configuration = response_data.split('warrantyPage.warrantycheck.displayProductInfo').last.split('false')[0].split("\', \'")[1].match("..-inch")
end
# And this one too
expiration_date = response_data.split('Estimated Expiration Date: ')[1].split('<')[0] if warranty_status
puts "\nSerial Number:\t\t#{serial}"
puts "Warranty Status:\t" + (warranty_status ? "Active and it expires on #{expiration_date}" : 'Expired')
puts "Configuration:\t\t#{configuration}"
puts "Model:\t\t#{model}"
#TODO:
# Grab product description and calculate Purchase Data
# Catch invalid Serial Numbers
# Make this more than just a proof of concept...
end
if ARGV.size > 0 then
serial = ARGV.each do |serial|
get_warranty(serial.upcase)
end
else
puts "Without your input, we'll use this machine's serial number."
serial = %x(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}').upcase.chomp
get_warranty(serial)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment