Class: ExtraSpace::Facility

Inherits:
Object
  • Object
show all
Defined in:
lib/extraspace/facility.rb

Overview

A facility (address + geocode + prices) on extraspace.com.

e.g. www.extraspace.com/storage/facilities/us/alabama/auburn/3264/

Constant Summary collapse

DEFAULT_EMAIL =
'[email protected]'
DEFAULT_PHONE =
'1-855-518-1443'
SITEMAP_URL =
'https://www.extraspace.com/facility-sitemap.xml'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, url:, name:, address:, geocode:, phone: DEFAULT_PHONE, email: DEFAULT_EMAIL, prices: []) ⇒ Facility

Returns a new instance of Facility.

Parameters:

  • id (String)
  • url (String)
  • name (String)
  • address (Address)
  • geocode (Geocode)
  • phone (String) (defaults to: DEFAULT_PHONE)
  • email (String) (defaults to: DEFAULT_EMAIL)
  • prices (Array<Price>) (defaults to: [])


109
110
111
112
113
114
115
116
117
118
# File 'lib/extraspace/facility.rb', line 109

def initialize(id:, url:, name:, address:, geocode:, phone: DEFAULT_PHONE, email: DEFAULT_EMAIL, prices: [])
  @id = id
  @url = url
  @name = name
  @address = address
  @geocode = geocode
  @phone = phone
  @email = email
  @prices = prices
end

Instance Attribute Details

#addressAddress

Returns:



35
36
37
# File 'lib/extraspace/facility.rb', line 35

def address
  @address
end

#emailString

Returns:

  • (String)


31
32
33
# File 'lib/extraspace/facility.rb', line 31

def email
  @email
end

#geocodeGeocode

Returns:



39
40
41
# File 'lib/extraspace/facility.rb', line 39

def geocode
  @geocode
end

#idString

Returns:

  • (String)


15
16
17
# File 'lib/extraspace/facility.rb', line 15

def id
  @id
end

#nameString

Returns:

  • (String)


23
24
25
# File 'lib/extraspace/facility.rb', line 23

def name
  @name
end

#phoneString

Returns:

  • (String)


27
28
29
# File 'lib/extraspace/facility.rb', line 27

def phone
  @phone
end

#pricesArray<Price>

Returns:



43
44
45
# File 'lib/extraspace/facility.rb', line 43

def prices
  @prices
end

#urlString

Returns:

  • (String)


19
20
21
# File 'lib/extraspace/facility.rb', line 19

def url
  @url
end

Class Method Details

.crawlObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/extraspace/facility.rb', line 86

def self.crawl
  sitemap.links.each do |link|
    url = link.loc

    facility = fetch(url:)
    puts facility.text

    facility.prices.each do |price|
      puts price.text
    end

    puts
  end
end

.fetch(url:) ⇒ Facility

Parameters:

  • url (String)

Returns:



53
54
55
56
# File 'lib/extraspace/facility.rb', line 53

def self.fetch(url:)
  document = Crawler.html(url:)
  parse(url:, document:)
end

.parse(url:, document:) ⇒ Facility

Parameters:

  • url (String)
  • document (Nokogiri::HTML::Document)

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/extraspace/facility.rb', line 62

def self.parse(url:, document:)
  data = parse_next_data(document: document)
  page_data = data.dig('props', 'pageProps', 'pageData', 'data')
  store_data = page_data.dig('facilityData', 'data', 'store')
  unit_classes = page_data.dig('unitClasses', 'data', 'unitClasses')
  id = store_data['number']
  name = store_data['name']

  address = Address.parse(data: store_data['address'])
  geocode = Geocode.parse(data: store_data['geocode'])
  prices = unit_classes.map { |price_data| Price.parse(data: price_data) }

  new(id:, url:, name:, address:, geocode:, prices:)
end

.parse_next_data(document:) ⇒ Hash

Parameters:

  • document (Nokogiri::HTML::Document)

Returns:

  • (Hash)

Raises:

  • (ParseError)


82
83
84
# File 'lib/extraspace/facility.rb', line 82

def self.parse_next_data(document:)
  JSON.parse(document.at('#__NEXT_DATA__').text)
end

.sitemapSitemap

Returns:



46
47
48
# File 'lib/extraspace/facility.rb', line 46

def self.sitemap
  Sitemap.fetch(url: SITEMAP_URL)
end

Instance Method Details

#inspectString

Returns:

  • (String)


121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/extraspace/facility.rb', line 121

def inspect
  props = [
    "id=#{@id.inspect}",
    "url=#{@url.inspect}",
    "address=#{@address.inspect}",
    "geocode=#{@geocode.inspect}",
    "phone=#{@phone.inspect}",
    "email=#{@email.inspect}",
    "prices=#{@prices.inspect}"
  ]
  "#<#{self.class.name} #{props.join(' ')}>"
end

#textString

Returns:

  • (String)


135
136
137
# File 'lib/extraspace/facility.rb', line 135

def text
  "#{@id} | #{@name} | #{@phone} | #{@email} | #{@address.text} | #{@geocode.text}"
end