Class: NSAStorage::Price

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(id:, dimensions:, features:, rates:) ⇒ Price

Returns a new instance of Price.

Parameters:



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

#dimensionsDimensions

Returns:



15
16
17
# File 'lib/nsastorage/price.rb', line 15

def dimensions
  @dimensions
end

#featuresFeatures

Returns:



19
20
21
# File 'lib/nsastorage/price.rb', line 19

def features
  @features
end

#idString

Returns:

  • (String)


11
12
13
# File 'lib/nsastorage/price.rb', line 11

def id
  @id
end

#ratesRates

Returns:



23
24
25
# File 'lib/nsastorage/price.rb', line 23

def rates
  @rates
end

Class Method Details

.fetch(facility_id:) ⇒ Array<Price>

Parameters:

  • facility_id (Integer)

Returns:



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

Parameters:

  • element (Nokogiri::XML::Element)

Returns:



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

#inspectString

Returns:

  • (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

#textString

Returns e.g. “123 | 5’ × 5’ (25 sqft) | $100 (street) / $90 (web)”.

Returns:

  • (String)

    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