Class: NSAStorage::Facility

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, url:, name:, address:, geocode:, phone: DEFAULT_PHONE, email: DEFAULT_EMAIL, prices: []) ⇒ Facility

Returns a new instance of Facility.

Parameters:

  • id (String)
  • url (String)
  • name (String)
  • address (Address)
  • geocode (Geocode)
  • phone (String) (defaults to: DEFAULT_PHONE)
  • email (String) (defaults to: DEFAULT_EMAIL)
  • prices (Array<Price>) (defaults to: [])


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

#addressAddress

Returns:



37
38
39
# File 'lib/nsastorage/facility.rb', line 37

def address
  @address
end

#emailString

Returns:

  • (String)


33
34
35
# File 'lib/nsastorage/facility.rb', line 33

def email
  @email
end

#geocodeGeocode?

Returns:



41
42
43
# File 'lib/nsastorage/facility.rb', line 41

def geocode
  @geocode
end

#idString

Returns:

  • (String)


17
18
19
# File 'lib/nsastorage/facility.rb', line 17

def id
  @id
end

#nameString

Returns:

  • (String)


25
26
27
# File 'lib/nsastorage/facility.rb', line 25

def name
  @name
end

#phoneString

Returns:

  • (String)


29
30
31
# File 'lib/nsastorage/facility.rb', line 29

def phone
  @phone
end

#pricesArray<Price>

Returns:



45
46
47
# File 'lib/nsastorage/facility.rb', line 45

def prices
  @prices
end

#urlString

Returns:

  • (String)


21
22
23
# File 'lib/nsastorage/facility.rb', line 21

def url
  @url
end

Class Method Details

.fetch(url:) ⇒ Facility

Parameters:

  • url (String)

Returns:



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

Parameters:

  • url (String)
  • document (Nokogiri::HTML::Document)

Returns:



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

Parameters:

  • document (Nokogiri::HTML::Document)

Returns:

  • (Hash)

Raises:



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>

Parameters:

  • document (Nokogiri::HTML::Document)

Returns:

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

.sitemapSitemap

Returns:



48
49
50
# File 'lib/nsastorage/facility.rb', line 48

def self.sitemap
  Sitemap.fetch(url: SITEMAP_URL)
end

Instance Method Details

#inspectString

Returns:

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

#textString

Returns:

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