Class: NSAStorage::Price
- Inherits:
-
Object
- Object
- NSAStorage::Price
- Defined in:
- lib/nsastorage/price.rb
Overview
The price (id + dimensions + rate) for a facility.
Constant Summary collapse
- ID_REGEX =
%r{(?<id>\d+)/(?:rent|reserve)/}
- PRICE_SELECTOR =
'[data-unit-size="small"],[data-unit-size="medium"],[data-unit-size="large"]'
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(id:, dimensions:, features:, rates:) ⇒ Price
constructor
A new instance of Price.
- #inspect ⇒ String
-
#text ⇒ String
E.g.
Constructor Details
#initialize(id:, dimensions:, features:, rates:) ⇒ Price
Returns a new instance of Price.
41 42 43 44 45 46 |
# File 'lib/nsastorage/price.rb', line 41 def initialize(id:, dimensions:, features:, rates:) @id = id @dimensions = dimensions @features = features @rates = rates end |
Instance Attribute Details
#dimensions ⇒ Dimensions
15 16 17 |
# File 'lib/nsastorage/price.rb', line 15 def dimensions @dimensions end |
#id ⇒ String
11 12 13 |
# File 'lib/nsastorage/price.rb', line 11 def id @id end |
Class Method Details
.fetch(facility_id:) ⇒ Array<Price>
28 29 30 31 32 33 34 35 |
# File 'lib/nsastorage/price.rb', line 28 def self.fetch(facility_id:) url = "https://www.nsastorage.com/facility-units/#{facility_id}" data = Crawler.json(url:)['data'] return [] if data['error'] html = data['html']['units'] Nokogiri::HTML(html).css(PRICE_SELECTOR).map { |element| parse(element:) } end |
.parse(element:) ⇒ Price
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/nsastorage/price.rb', line 67 def self.parse(element:) link = element.at_xpath(".//a[contains(text(), 'Rent')]|//a[contains(text(), 'Reserve')]") dimensions = Dimensions.parse(element:) features = Features.parse(element:) rates = Rates.parse(element:) id = link ? ID_REGEX.match(link['href'])[:id] : "#{dimensions.id}-#{features.id}" new(id:, dimensions:, features:, rates:) end |
Instance Method Details
#inspect ⇒ String
49 50 51 52 53 54 55 56 57 |
# File 'lib/nsastorage/price.rb', line 49 def inspect props = [ "id=#{@id.inspect}", "dimensions=#{@dimensions.inspect}", "features=#{@features.inspect}", "rates=#{@rates.inspect}" ] "#<#{self.class.name} #{props.join(' ')}>" end |
#text ⇒ String
Returns e.g. “123 | 5’ × 5’ (25 sqft) | $100 (street) / $90 (web)”.
60 61 62 |
# File 'lib/nsastorage/price.rb', line 60 def text "#{@id} | #{@dimensions.text} | #{@rates.text} | #{@features.text}" end |