Class: Scraper::Parser::ImeiPage

Inherits:
Base
  • Object
show all
Defined in:
lib/scraper/parser/imei_page.rb

Overview

ImeiPage class which parse the site page body

Constant Summary

PHONE_EXPIRATION_PATTERN =
/displayPHSupportInfo.*Estimated\s+Expiration\s+Date:\s+(?<date>[^<]+)/
HARDWARE_EXPIRATION_PATTERN =
/displayHWSupportInfo.*Estimated\s+Expiration\s+Date:\s+(?<date>[^<]+)/
JSON_PATTERN =
/jsonObj\s*=\s*{(?<obj>[^}]+)}/m
JSON_GROUP_PATTERN =
/'([^']+)'\s*:\s*'?([^,]+?)'?[\n,]/
IMAGE_URL_PATTERN =
/displayProductInfo.*(?<image>http[^'"]+)/
PRODUCT_NAME_PATTERN =
/displayProductInfo.*?'.*?'.*?'(?<name>[^']+)/
IMEI_PATTERN =
/displayProductInfo.*?'.*?'.*?'.*?'.*?'(?<imei>\d{15})/
NOT_ACTIVATED_PATTERN =
/iPhone\s+has\s+not\s+been\s+activated/
KEY_ALIASES =

JSON keys aliases

{
  "phoneSupportHasCoverage" => :phone_has_coverage,
  "hwSupportHasCoverage" => :hardware_has_coverage,
  "hwSupportCoverageValue" => :hardware_support_coverage,
  "hasActiveAPP" => :has_active_app,
  "isAPPEligible" => :app_eligible,
  "isPPIEligible" => :ppi_eligible,
  "productType" => :product_type,
  "numDaysSinceDOP" => :days_since_dop,
  "hasTechToolDownload" => :has_tech_tool
}

Instance Attribute Summary

Attributes inherited from Base

#data

Instance Method Summary (collapse)

Methods inherited from Base

#doc, #parse

Constructor Details

- (ImeiPage) initialize

Returns a new instance of ImeiPage



31
32
33
# File 'lib/scraper/parser/imei_page.rb', line 31

def initialize
  reset_data
end

Instance Method Details

- (Object) after_parse

Cleanup after data is parsed



112
113
114
115
116
117
118
119
120
121
# File 'lib/scraper/parser/imei_page.rb', line 112

def after_parse
  # We don't need additional info, if device is not activated
  if @data[:not_activated]
    @data.delete_if { |k,v| !%i(not_activated imei).include?(k) }
  end

  @data = {error: "Has no information"} unless @data[:imei]

  convert_days_since_dop
end

- (Object) parse_hardware_expiration

Parse hardware expiration



65
66
67
# File 'lib/scraper/parser/imei_page.rb', line 65

def parse_hardware_expiration
  expiration_date_parse(:hardware)
end

- (Object) parse_image_url

Parse product image url



72
73
74
75
76
77
# File 'lib/scraper/parser/imei_page.rb', line 72

def parse_image_url
  matchers = IMAGE_URL_PATTERN.match(script_content)
  return unless matchers

  @data[:image] = matchers[:image]
end

- (Object) parse_imei

Parse IMEI number



92
93
94
95
96
97
# File 'lib/scraper/parser/imei_page.rb', line 92

def parse_imei
  matchers = IMEI_PATTERN.match(script_content)
  return unless matchers

  @data[:imei] = matchers[:imei]
end

- (Object) parse_json

Parse JSON object in the script content



46
47
48
49
50
51
52
53
# File 'lib/scraper/parser/imei_page.rb', line 46

def parse_json
  matchers = JSON_PATTERN.match(script_content)
  return unless matchers

  matchers[:obj].scan(JSON_GROUP_PATTERN).each do |group|
    @data[json_key_alias(group.first)] = eval_json_value(group.last)
  end
end

- (Object) parse_not_activated

Parse info when phone is not activated



102
103
104
105
106
107
# File 'lib/scraper/parser/imei_page.rb', line 102

def parse_not_activated
  matchers = NOT_ACTIVATED_PATTERN.match(script_content)
  return unless matchers

  @data[:not_activated] = true
end

- (Object) parse_phone_expiration

Parse phone expiration



58
59
60
# File 'lib/scraper/parser/imei_page.rb', line 58

def parse_phone_expiration
  expiration_date_parse(:phone)
end

- (Object) parse_product_name

Parse product name



82
83
84
85
86
87
# File 'lib/scraper/parser/imei_page.rb', line 82

def parse_product_name
  matchers = PRODUCT_NAME_PATTERN.match(script_content)
  return unless matchers

  @data[:product_name] = matchers[:name]
end

- (Object) reset_data

Reset the data object



38
39
40
41
# File 'lib/scraper/parser/imei_page.rb', line 38

def reset_data
  @data = {}
  @doc = nil
end