Class: PublicStorage::Facility
- Inherits:
-
Object
- Object
- PublicStorage::Facility
- 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
- .crawl ⇒ Object
- .fetch(url:) ⇒ Facility
- .parse(document:) ⇒ Facility
- .parse_ld(document:) ⇒ Hash
- .sitemap ⇒ Sitemap
Instance Method Summary collapse
-
#initialize(id:, name:, address:, geocode:, prices:) ⇒ Facility
constructor
A new instance of Facility.
- #inspect ⇒ String
- #text ⇒ String
Constructor Details
#initialize(id:, name:, address:, geocode:, prices:) ⇒ Facility
Returns a new instance of Facility.
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
#address ⇒ Address
23 24 25 |
# File 'lib/publicstorage/facility.rb', line 23 def address @address end |
#geocode ⇒ Geocode
27 28 29 |
# File 'lib/publicstorage/facility.rb', line 27 def geocode @geocode end |
#id ⇒ Integer
15 16 17 |
# File 'lib/publicstorage/facility.rb', line 15 def id @id end |
#name ⇒ String
19 20 21 |
# File 'lib/publicstorage/facility.rb', line 19 def name @name end |
#prices ⇒ Array<Price>
31 32 33 |
# File 'lib/publicstorage/facility.rb', line 31 def prices @prices end |
Class Method Details
.crawl ⇒ Object
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
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
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
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 |
.sitemap ⇒ Sitemap
34 35 36 |
# File 'lib/publicstorage/facility.rb', line 34 def self.sitemap Sitemap.fetch(url: SITEMAP_URL) end |
Instance Method Details
#inspect ⇒ 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 |
#text ⇒ String
107 108 109 |
# File 'lib/publicstorage/facility.rb', line 107 def text "#{@id} | #{@name} | #{@address.text} | #{@geocode.text}" end |