Class: PublicStorage::Facility

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

Overview

Constant Summary collapse

SITEMAP_URL =
'https://www.publicstorage.com/sitemap_0-product.xml'
PRICE_SELECTOR =
'.units-results-section .unit-list-group .unit-list-item'
LD_SELECTOR =
'script[type="application/ld+json"]'
ID_REGEX =
/(?<id>\d+)\.html/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, address:, geocode:, prices:) ⇒ Facility

Returns a new instance of Facility.

Parameters:



88
89
90
91
92
93
94
# File 'lib/publicstorage/facility.rb', line 88

def initialize(id:, name:, address:, geocode:, prices:)
  @id = id
  @name = name
  @address = address
  @geocode = geocode
  @prices = prices
end

Instance Attribute Details

#addressAddress

Returns:



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

def address
  @address
end

#geocodeGeocode

Returns:



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

def geocode
  @geocode
end

#idInteger

Returns:

  • (Integer)


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

def id
  @id
end

#nameString

Returns:

  • (String)


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

def name
  @name
end

#pricesArray<Price>

Returns:



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

def prices
  @prices
end

Class Method Details

.crawlObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/publicstorage/facility.rb', line 68

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:



41
42
43
44
# File 'lib/publicstorage/facility.rb', line 41

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

.parse(document:) ⇒ Facility

Parameters:

  • document (NokoGiri::XML::Document)

Returns:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/publicstorage/facility.rb', line 49

def self.parse(document:)
  data = parse_ld(document:)
  id = Integer(data['url'].match(ID_REGEX)[:id])
  name = data['name']
  address = Address.parse(data: data['address'])
  geocode = Geocode.parse(data: data['geo'])

  prices = document.css(PRICE_SELECTOR).map { |element| Price.parse(element:) }

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

.parse_ld(document:) ⇒ Hash

Parameters:

  • document (NokoGiri::XML::Document)

Returns:

  • (Hash)


64
65
66
# File 'lib/publicstorage/facility.rb', line 64

def self.parse_ld(document:)
  JSON.parse(document.at_css(LD_SELECTOR).text).find { |entry| entry['@type'] == 'SelfStorage' }
end

.sitemapSitemap

Returns:



34
35
36
# File 'lib/publicstorage/facility.rb', line 34

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

Instance Method Details

#inspectString

Returns:

  • (String)


97
98
99
100
101
102
103
104
# File 'lib/publicstorage/facility.rb', line 97

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

#textString

Returns:

  • (String)


107
108
109
# File 'lib/publicstorage/facility.rb', line 107

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