Class: ExtraSpace::Facility
- Inherits:
-
Object
- Object
- ExtraSpace::Facility
- 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
- #address ⇒ Address
- #email ⇒ String
- #geocode ⇒ Geocode
- #id ⇒ String
- #name ⇒ String
- #phone ⇒ String
- #prices ⇒ Array<Price>
- #url ⇒ String
Class Method Summary collapse
- .crawl ⇒ Object
- .fetch(url:) ⇒ Facility
- .parse(url:, document:) ⇒ Facility
- .parse_next_data(document:) ⇒ Hash
- .sitemap ⇒ Sitemap
Instance Method Summary collapse
-
#initialize(id:, url:, name:, address:, geocode:, phone: DEFAULT_PHONE, email: DEFAULT_EMAIL, prices: []) ⇒ Facility
constructor
A new instance of Facility.
- #inspect ⇒ String
- #text ⇒ String
Constructor Details
#initialize(id:, url:, name:, address:, geocode:, phone: DEFAULT_PHONE, email: DEFAULT_EMAIL, prices: []) ⇒ Facility
Returns a new instance of Facility.
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
#email ⇒ String
31 32 33 |
# File 'lib/extraspace/facility.rb', line 31 def email @email end |
#id ⇒ String
15 16 17 |
# File 'lib/extraspace/facility.rb', line 15 def id @id end |
#name ⇒ String
23 24 25 |
# File 'lib/extraspace/facility.rb', line 23 def name @name end |
#phone ⇒ String
27 28 29 |
# File 'lib/extraspace/facility.rb', line 27 def phone @phone end |
#url ⇒ String
19 20 21 |
# File 'lib/extraspace/facility.rb', line 19 def url @url end |
Class Method Details
.crawl ⇒ Object
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
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
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
82 83 84 |
# File 'lib/extraspace/facility.rb', line 82 def self.parse_next_data(document:) JSON.parse(document.at('#__NEXT_DATA__').text) end |
.sitemap ⇒ Sitemap
46 47 48 |
# File 'lib/extraspace/facility.rb', line 46 def self.sitemap Sitemap.fetch(url: SITEMAP_URL) end |
Instance Method Details
#inspect ⇒ 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 |
#text ⇒ String
135 136 137 |
# File 'lib/extraspace/facility.rb', line 135 def text "#{@id} | #{@name} | #{@phone} | #{@email} | #{@address.text} | #{@geocode.text}" end |