-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwarranty.rb
executable file
·111 lines (91 loc) · 3.18 KB
/
warranty.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env ruby
#####################################################################
# Name: Applecare Warranty Check
# Author: Riley Shott
# Date: June 9, 2013
# Version: 0.1.1
# Description: Checks whether or not the given serial(s) are covered under the AppleCare Protection Plan.
# This is just a quick script to demonstrate that you can get a JSON formatted response with some Applecare information.
#####################################################################
### LIBRARIES ###
begin
require 'uri'
require 'net/https'
require 'rubygems'
require 'json'
rescue LoadError => e
puts "Could not load a required library"
puts e
puts "Try installing the required gem:"
puts "gem install library"
exit 1
end
### CONSTANTS ###
URL = URI.parse('https://expresslane.apple.com')
HTTP = Net::HTTP.new(URL.host, URL.port)
HTTP.use_ssl = true
HTTP.verify_mode = OpenSSL::SSL::VERIFY_NONE
### METHODS ###
# Returns the session cookies needed to reach the warranty page
def get_session_cookies
begin
resp = HTTP.get('/GetproductgroupList.action')
cookie = resp.response['set-cookie']
headers = {
'Cookie' => cookie,
's_cc' => 'true',
's_orientation' => '[[B]]',
's_sq'=> '[[B]]',
's_orientationHeight' => '731',
}
rescue => e
puts "An error occured while getting the session cookies"
puts e.backtrace
exit 1
end
end
# Returns a hash object with a Mac's warranty status
def get_warranty_info(serial, cookies)
begin
resp = HTTP.get("/Coverage.action?serialId=#{serial}", cookies)
hash = JSON.parse(resp.body)
rescue => e
puts "An error occured checking this serial - #{serial}"
puts e.backtrace
end
end
def valid_warranty_hash?(hash)
return false unless hash['strRespCd'].eql?('00')
true
end
def app?(hash)
return true if ((hash['strHwCoverage'].eql?('PP')) and (hash['strPhCoverage'].eql?('PP')))
false
end
### MAIN ###
if (ARGV[0].eql?('-h') or ARGV[0].eql?('--help'))
puts "Usage: #{__FILE__} serial1 serial2 serial3 etc..."
exit 0
end
if ARGV.empty?
puts "No serial number given. Proceeding with the local serial number."
ARGV[0] = %x(system_profiler SPHardwareDataType | grep -v tray |awk '/Serial/ {print $4}').upcase.chomp
end
session_cookies = get_session_cookies
ARGV.each do |serial|
warranty = get_warranty_info(serial, session_cookies)
if valid_warranty_hash?(warranty)
has_app = app?(warranty)
puts "\nSerial Number: #{warranty['strEntSlNo']}"
puts "Eligible for APP: #{warranty['strAppEligible']}" unless has_app
puts "Has APP: #{has_app}"
puts "Hardware Coverage: #{warranty['strHwCovStatus']}"
puts "Phone Coverage: #{warranty['strPhCovStatus']}"
# Not sure what exactly this indicates. Adding this value from the current date did not add up to the expiry of my AppleCare
puts "Days Left: #{warranty['strPhDaysLeft']}\n"
else
puts "\nAn error occured while checking this serial - #{serial}"
end
end
puts "\nPlease be aware that the days left value is not indicative of the remaining days of the warranty. It is shown for demonstrational purposes only. See comment on line 101 for more information"
exit 0