module PearWarranty

Constants

COOKIES
PROXIES
TRIES
VERSION

Public Class Methods

check(serial, proxy_index = nil) click to toggle source
# File lib/pear_warranty.rb, line 10
def self.check(serial, proxy_index = nil)
  agent = Mechanize.new
  proxies = [0,1,2,3,4,5,6,7,8,9]
  unless proxy_index
    proxy_index = rand(PROXIES.size)
  else
    proxies -= [proxy_index]
  end
  page = nil
  error = true
  apple_url = "https://selfsolve.apple.com/wcResults.do?sn=#{serial}&Continue=Continue&num=0"
  correct_reg = /warrantyPage\.warrantycheck\.displayHWSupportInfo/
  invalid_reg = [/but this serial number is not valid/, /but the serial number you have provided cannot be found in our records/]
  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 = proxies[rand(proxies.size)]
      proxies -= [proxy_index]
      next
    end
    page = page.body
    if page =~ correct_reg or page =~ invalid_reg[1] or page =~ invalid_reg[0]
      error = false
      break
    end
    proxy_index = proxies[rand(proxies.size)]
    proxies -= [proxy_index]
  end
  if error
    {error: 'Problem with proxy'}
  else
    unless page =~ correct_reg
      { error: 'There is no such imei or service is not available at this moment' }
    else
      text = page.split(correct_reg)[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

Private Class Methods

get_form(proxy, value, agent) click to toggle source
# File lib/pear_warranty.rb, line 68
def self.get_form(proxy, value, agent)
  url = get_url(proxy)
  builder = Nokogiri::HTML::Builder.new do |doc|
    doc.form_ :method=>'POST', :action=>url, name: 'main_submission' do
      doc.input :type=>'hidden', :name=>'pfserverDropdown', :value => url
      doc.input :type=>'hidden', :name=>'allowCookies', :value => 'on'
      doc.input :type=>'hidden', :name=>'pfipDropdown', :value => 'default'
      doc.input type: 'text', name: 'get', value: value
    end
  end
  node = Nokogiri::HTML(builder.to_html)
  form = Mechanize::Form.new(node.at('form'), agent)
  form
end
get_url(proxy) click to toggle source
# File lib/pear_warranty.rb, line 60
def self.get_url(proxy)
  "https://#{PROXIES[proxy] || PROXIES[0]}.proxfree.com/request.php?do=go"
end
to_boolean(str) click to toggle source
# File lib/pear_warranty.rb, line 64
def self.to_boolean(str)
  str == 'true'
end