class AppleWarrantyCheck::Process

Constants

BOOL_ARG
CHECK_URL
ERR_RESP_REGEXP
EXP_DATE
HW_SUPPORT_INFO_KEYS
HW_SUPPORT_INFO_REGEXP
HW_SUPPORT_STATUS
NOT_REGISTERED_REGEXP
PH_SUPPORT_INFO_KEYS
PH_SUPPORT_INFO_REGEXP
PH_SUPPORT_STATUS
PRODUCT_INFO_KEYS
PRODUCT_INFO_REGEXP
STR_ARG

Attributes

imei[RW]
response[RW]

Public Class Methods

new(imei=nil) click to toggle source
# File lib/apple_warranty_check/process.rb, line 28
def initialize(imei=nil)
  @imei = imei
end

Public Instance Methods

get_hw_support_info(html) click to toggle source
# File lib/apple_warranty_check/process.rb, line 59
def get_hw_support_info(html)
  get_support_info HW_SUPPORT_INFO_REGEXP, HW_SUPPORT_INFO_KEYS, HW_SUPPORT_STATUS, html
end
get_phone_support_info(html) click to toggle source
# File lib/apple_warranty_check/process.rb, line 55
def get_phone_support_info(html)
  get_support_info PH_SUPPORT_INFO_REGEXP, PH_SUPPORT_INFO_KEYS, PH_SUPPORT_STATUS, html
end
get_product_info(html) click to toggle source
# File lib/apple_warranty_check/process.rb, line 63
def get_product_info(html)
  process_with_exception 'could not find product info' do
    [
      PRODUCT_INFO_KEYS,
      PRODUCT_INFO_REGEXP.match(html)[1].split(',').map{ |el| el.strip.gsub('\'', '') }
    ].transpose.to_h
  end
end
parse_body(html) click to toggle source
# File lib/apple_warranty_check/process.rb, line 43
def parse_body(html)
  return error_response(html) unless ERR_RESP_REGEXP.match(html).nil?

  return not_registered unless NOT_REGISTERED_REGEXP.match(html).nil?

  {
    product_info: get_product_info(html),
    phone_support: get_phone_support_info(html),
    hw_support: get_hw_support_info(html)
  }
end
run() click to toggle source
# File lib/apple_warranty_check/process.rb, line 32
def run
  @response = get_response

  case response
  when Net::HTTPSuccess
    parse_body response.body
  else
    error_msg [response.code, response.message].join(': ')
  end
end

Private Instance Methods

error_msg(message) click to toggle source
# File lib/apple_warranty_check/process.rb, line 112
def error_msg(message)
  { error: message }
end
error_response(html) click to toggle source
# File lib/apple_warranty_check/process.rb, line 103
def error_response(html)
  error_msg ERR_RESP_REGEXP.match(html)[1]
end
get_response() click to toggle source
# File lib/apple_warranty_check/process.rb, line 74
def get_response
  uri = URI(CHECK_URL)
  params = {sn: imei, Continue: 'Continue', cn: '', local: '', caller: '', num: 0 }

  uri.query = URI.encode_www_form(params)

  res = Net::HTTP.get_response(uri)
end
get_support_info(regexp, keys, status_regexp, html) click to toggle source
# File lib/apple_warranty_check/process.rb, line 83
def get_support_info(regexp, keys, status_regexp, html)
  process_with_exception 'could not find support info' do
    match_data = regexp.match(html)

    keys.map.
      with_index{ |el, i| [el, match_data[i+1]] }.
      to_h.
      merge!(
        support_status: status_regexp.match(match_data[2])[1],
        expiration_date: (EXP_DATE.match(match_data[3])[1] rescue nil)
      )
  end
end
not_registered() click to toggle source

TODO follow redirect to get live apple message

# File lib/apple_warranty_check/process.rb, line 108
def not_registered
  error_msg "Please validate your product's purchase date. Apple is unable to provide information about your service coverage."
end
process_with_exception(message) { || ... } click to toggle source
# File lib/apple_warranty_check/process.rb, line 97
def process_with_exception(message)
  yield
rescue NoMethodError
  message
end