Module: PearWarranty

Defined in:
lib/pear_warranty.rb,
lib/pear_warranty/version.rb

Constant Summary collapse

TRIES =
10
PROXIES =
['qc', 'de', 'al', 'nl', 'fr', 'no', 'tx', 'nj', 'il', 'ga'  ]
COOKIES =
{'_ga' => 'GA1.2.343285711.1435218476', '_gat' => '1', 's' => '3fd5f4953c541474c867c23b28853ad2', 'token' => 'acc1de9efdac455d'}
VERSION =
"0.0.5"

Class Method Summary collapse

Class Method Details

.check(serial, proxy_index = nil) ⇒ Object



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(COOKIES.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(COOKIES.size)
      next
    end
    page = page.body
    unless page =~ /pferror/
      error = false
      break
    end
    proxy_index = rand(COOKIES.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