Class: AutoServiceCLI::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/auto_service_cli/scraper.rb

Constant Summary collapse

SORT_TYPES =
{
  DEFAULT: "default",
  DISTANCE: "distance",
  AVERAGE_RATING: "average_rating",
  NAME: "name"
}
DEFAULT_SORT_TYPE =
SORT_TYPES[:default]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip = nil, sort_type = nil) ⇒ Scraper

Constructors and class methods.



16
17
18
19
# File 'lib/auto_service_cli/scraper.rb', line 16

def initialize(zip = nil, sort_type = nil)
  self.zip = zip if zip
  self.sort_type = sort_type ? sort_type : self.class::SORT_TYPES[:DEFAULT]
end

Instance Attribute Details

#sort_typeObject

Returns the value of attribute sort_type.



2
3
4
# File 'lib/auto_service_cli/scraper.rb', line 2

def sort_type
  @sort_type
end

#zipObject

Returns the value of attribute zip.



2
3
4
# File 'lib/auto_service_cli/scraper.rb', line 2

def zip
  @zip
end

Class Method Details

.valid_sort_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/auto_service_cli/scraper.rb', line 33

def self.valid_sort_type?(type)
  SORT_TYPES.any? { |key, value| value == type }
end

.valid_zip?(zip) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/auto_service_cli/scraper.rb', line 25

def self.valid_zip?(zip)
  zip.to_i > 0
end

Instance Method Details

#scrape_center_details(center) ⇒ Object


Scrape center details from the internal url (if available) Show action.

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/auto_service_cli/scraper.rb', line 71

def scrape_center_details(center)
  raise InvalidPage, "Invalid page!!!" if !center.is_a? AutoServiceCLI::ServiceCenter || center.int_url.nil?
  doc = Nokogiri::HTML(open(center.int_url))
  raise InvalidPage "Invalid page!!!" if doc.nil?

  details = {}

  status = scrape_status(doc); details[:open_status] = status unless status.nil?
  slogan = scrape_slogan(doc); details[:slogan] = slogan unless slogan.nil?
  hours = scrape_hours(doc); details[:working_hours] = hours unless hours.nil?
  description = scrape_description(doc); details[:description] = description unless description.nil?
  services = scrape_services(doc); details[:services] = services unless services.nil?
  brands = scrape_brands(doc); details[:brands] = brands unless brands.nil?
  payment = scrape_payment(doc); details[:payment] = payment unless payment.nil?

  center.details_from_hash(details)
end

#scrape_centersObject


Scrape from the main page. Index action.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/auto_service_cli/scraper.rb', line 45

def scrape_centers
  doc = Nokogiri::HTML(open(get_url))
  raise InvalidPage "Invalid page!!!" if doc.nil?
  centers = doc.css(".organic .result .info")

  # form centers hash.
  centers.each do |center|

    main_details = {}
    obj_center = AutoServiceCLI::ServiceCenter.create(scrape_name(center))

    url = scrape_internal_url(center); main_details[:int_url] = url unless url.nil?
    url = scrape_external_url(center); main_details[:ext_url] = url unless url.nil?
    rating = scrape_rating(center); main_details[:rating] = rating unless rating.nil?
    address = scrape_address(center); main_details[:address] = address unless address.nil?
    phone = scrape_phone_number(center); main_details[:phone_number] = phone unless phone.nil?
    category = scrape_category(center); main_details[:main_category] = category unless category.nil?

    obj_center.details_from_hash(main_details) # setting all details to center.
  end
end