Class: NSAStorage::Facility
- Inherits:
-
Object
- Object
- NSAStorage::Facility
- Defined in:
- lib/nsastorage/facility.rb
Overview
A facility (address + geocode + prices) on nsastorage.com.
e.g. www.nsastorage.com/storage/california/storage-units-paramount/7752-Jackson-St-876
Defined Under Namespace
Classes: ParseError
Constant Summary collapse
- DEFAULT_EMAIL =
'[email protected]'
- DEFAULT_PHONE =
'+1-844-434-1150'
- SITEMAP_URL =
'https://www.nsastorage.com/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
- .fetch(url:) ⇒ Facility
- .parse(url:, document:) ⇒ Facility
- .parse_ld_json_script(document:) ⇒ Hash
- .parse_ld_json_scripts(document:) ⇒ Array<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.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/nsastorage/facility.rb', line 103 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
33 34 35 |
# File 'lib/nsastorage/facility.rb', line 33 def email @email end |
#id ⇒ String
17 18 19 |
# File 'lib/nsastorage/facility.rb', line 17 def id @id end |
#name ⇒ String
25 26 27 |
# File 'lib/nsastorage/facility.rb', line 25 def name @name end |
#phone ⇒ String
29 30 31 |
# File 'lib/nsastorage/facility.rb', line 29 def phone @phone end |
#url ⇒ String
21 22 23 |
# File 'lib/nsastorage/facility.rb', line 21 def url @url end |
Class Method Details
.fetch(url:) ⇒ Facility
55 56 57 58 |
# File 'lib/nsastorage/facility.rb', line 55 def self.fetch(url:) document = Crawler.html(url:) parse(url:, document:) end |
.parse(url:, document:) ⇒ Facility
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/nsastorage/facility.rb', line 64 def self.parse(url:, document:) id = Integer(document.at_css('[data-facility-id]')['data-facility-id']) name = document.at_css('.section-title').text.strip geocode = Geocode.parse(document:) address = Address.parse(document:) prices = Price.fetch(facility_id: id) new(id:, url:, name:, address:, geocode:, prices:) end |
.parse_ld_json_script(document:) ⇒ Hash
80 81 82 83 84 |
# File 'lib/nsastorage/facility.rb', line 80 def self.parse_ld_json_script(document:) parse_ld_json_scripts(document:).find do |data| data['@type'] == 'SelfStorage' end || raise(ParseError, 'missing ld+json') end |
.parse_ld_json_scripts(document:) ⇒ Array<Hash>
89 90 91 92 93 |
# File 'lib/nsastorage/facility.rb', line 89 def self.parse_ld_json_scripts(document:) elements = document.xpath('//script[@type="application/ld+json"]') elements.map { |element| element.text.empty? ? {} : JSON.parse(element.text) } end |
.sitemap ⇒ Sitemap
48 49 50 |
# File 'lib/nsastorage/facility.rb', line 48 def self.sitemap Sitemap.fetch(url: SITEMAP_URL) end |
Instance Method Details
#inspect ⇒ String
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/nsastorage/facility.rb', line 115 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
129 130 131 |
# File 'lib/nsastorage/facility.rb', line 129 def text "#{@id} | #{@name} | #{@phone} | #{@email} | #{@address.text} | #{@geocode ? @geocode.text : 'N/A'}" end |