Class: UHaul::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/uhaul/address.rb

Overview

The address (street + city + state + zip) of a facility.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(street:, city:, state:, zip:) ⇒ Address

Returns a new instance of Address.

Parameters:

  • street (String)
  • city (String)
  • state (String)
  • zip (String)


26
27
28
29
30
31
# File 'lib/uhaul/address.rb', line 26

def initialize(street:, city:, state:, zip:)
  @street = street
  @city = city
  @state = state
  @zip = zip
end

Instance Attribute Details

#cityString

Returns:

  • (String)


12
13
14
# File 'lib/uhaul/address.rb', line 12

def city
  @city
end

#stateString

Returns:

  • (String)


16
17
18
# File 'lib/uhaul/address.rb', line 16

def state
  @state
end

#streetString

Returns:

  • (String)


8
9
10
# File 'lib/uhaul/address.rb', line 8

def street
  @street
end

#zipString

Returns:

  • (String)


20
21
22
# File 'lib/uhaul/address.rb', line 20

def zip
  @zip
end

Class Method Details

.parse(document:, data:) ⇒ Address

Parameters:

  • document (String)
  • data (Hash)

    optional

Returns:



53
54
55
# File 'lib/uhaul/address.rb', line 53

def self.parse(document:, data:)
  parse_by_data(data:) || parse_by_document(document:)
end

.parse_by_data(data:) ⇒ Address

Parameters:

  • data (Hash)

Returns:



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/uhaul/address.rb', line 60

def self.parse_by_data(data:)
  address = data&.dig('address')
  return unless address

  new(
    street: address['streetAddress'],
    city: address['addressLocality'],
    state: address['addressRegion'],
    zip: address['postalCode']
  )
end

.parse_by_document(document:) ⇒ Address

Parameters:

  • document (Nokogiri::HTML::Document)

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/uhaul/address.rb', line 75

def self.parse_by_document(document:)
  element = document.at_css('address')
  return unless element

  element.text.match(/(?<street>.+)[\r\n,]+(?<city>.+)[\r\n,]+(?<state>.+)[\r\n\s,]+(?<zip>\d{5})/) do |match|
    new(
      street: strip(match[:street]),
      city: strip(match[:city]),
      state: strip(match[:state]),
      zip: strip(match[:zip])
    )
  end
end

.strip(text) ⇒ String

Parameters:

  • text (String)

Returns:

  • (String)


92
93
94
95
96
97
98
99
100
# File 'lib/uhaul/address.rb', line 92

def self.strip(text)
  return unless text

  text
    .strip
    .gsub(/^[\s\p{Space},],+/, '')
    .gsub(/[\s\p{Space},]+$/, '')
    .gsub(/[\s\p{Space}]+/, ' ')
end

Instance Method Details

#inspectString

Returns:

  • (String)


34
35
36
37
38
39
40
41
42
# File 'lib/uhaul/address.rb', line 34

def inspect
  props = [
    "street=#{@street.inspect}",
    "city=#{@city.inspect}",
    "state=#{@state.inspect}",
    "zip=#{@zip.inspect}"
  ]
  "#<#{self.class.name} #{props.join(' ')}>"
end

#textString

Returns:

  • (String)


45
46
47
# File 'lib/uhaul/address.rb', line 45

def text
  "#{street}, #{city}, #{state} #{zip}"
end