Class: CubeSmart::Facility
- Inherits:
-
Object
- Object
- CubeSmart::Facility
- Defined in:
- lib/cubesmart/facility.rb
Overview
A facility (address + geocode + prices) on cubesmart.com.
e.g. www.cubesmart.com/arizona-self-storage/chandler-self-storage/2.html
Defined Under Namespace
Classes: ParseError
Constant Summary collapse
- DEFAULT_EMAIL =
'webleads@cubesmart.com'
- DEFAULT_PHONE =
'1-877-279-7585'
- SITEMAP_URL =
'https://www.cubesmart.com/sitemap-facility.xml'
- PRICE_SELECTOR =
%w[small medium large].map do |group| ".#{group}-group ul.csStorageSizeDimension li.csStorageSizeDimension" end.join(', ')
- ID_REGEX =
/(?<id>\d+)\.html/
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_json_ld(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.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/cubesmart/facility.rb', line 100 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
39 40 41 |
# File 'lib/cubesmart/facility.rb', line 39 def email @email end |
#id ⇒ String
23 24 25 |
# File 'lib/cubesmart/facility.rb', line 23 def id @id end |
#name ⇒ String
31 32 33 |
# File 'lib/cubesmart/facility.rb', line 31 def name @name end |
#phone ⇒ String
35 36 37 |
# File 'lib/cubesmart/facility.rb', line 35 def phone @phone end |
#url ⇒ String
27 28 29 |
# File 'lib/cubesmart/facility.rb', line 27 def url @url end |
Class Method Details
.fetch(url:) ⇒ Facility
61 62 63 64 |
# File 'lib/cubesmart/facility.rb', line 61 def self.fetch(url:) document = Crawler.html(url:) parse(url:, document:) end |
.parse(url:, document:) ⇒ Facility
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cubesmart/facility.rb', line 70 def self.parse(url:, document:) data = parse_json_ld(document:) id = ID_REGEX.match(url)[: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: element) } new(id:, url:, name:, address:, geocode:, prices:) end |
.parse_json_ld(document:) ⇒ Hash
87 88 89 90 |
# File 'lib/cubesmart/facility.rb', line 87 def self.parse_json_ld(document:) graph = JSON.parse(document.at_xpath('//script[contains(text(), "@graph")]').text)['@graph'] graph.find { |entry| entry['@type'] == 'SelfStorage' } || raise(ParseError, 'missing @graph') end |
.sitemap ⇒ Sitemap
54 55 56 |
# File 'lib/cubesmart/facility.rb', line 54 def self.sitemap Sitemap.fetch(url: SITEMAP_URL) end |
Instance Method Details
#inspect ⇒ String
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/cubesmart/facility.rb', line 112 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
126 127 128 |
# File 'lib/cubesmart/facility.rb', line 126 def text "#{@id} | #{@name} | #{@phone} | #{@email} | #{@address.text} | #{@geocode.text}" end |