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
|
# File 'lib/pear_warranty.rb', line 10
def self.check(serial, proxy_index = nil)
agent = Mechanize.new
proxy_index ||= rand(PROXIES.size)
page = nil
error = true
apple_url = "https://selfsolve.apple.com/wcResults.do?sn=#{serial}&Continue=Continue&num=0"
TRIES.times do
form = get_form(proxy_index, apple_url, agent)
set_cookie(agent)
begin
page = agent.submit(form)
rescue Net::HTTP::Persistent::Error
proxy_index = rand(PROXIES.size)
next
end
page = page.body
unless page =~ /pferror/
error = false
break
end
proxy_index = rand(PROXIES.size)
end
if error
{error: 'Problem with proxy'}
else
if page =~ /(but this serial number is not valid)|(but the serial number you have provided cannot be found in our records)/
{ error: 'There is no such imei or service is not available at this moment' }
else
text = page.split('warrantyPage.warrantycheck.displayPHSupportInfo')[1].scan(/\(.*\)/)[0].delete('()')
params = text.split(', ')
warranty = to_boolean(params.first.delete ' ')
{
warranty: warranty,
date: (Date.parse(params[2][/Estimated Expiration Date: (.*)/, 1] + ' ' + params[3][0..3]) if warranty)
}
end
end
end
|