Class: Australia::Postcode

Inherits:
Object
  • Object
show all
Defined in:
lib/australia/postcode.rb,
lib/australia/postcode/version.rb

Overview

The Australia::Postcode class represents Australian postcodes and provides methods for manipulating them. Right now you can find the distance between two postcodes and that’s about it.

Constant Summary collapse

VERSION =

The version of the australia_postcode gem. Whodathunkit

"0.1.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(postcode, suburb, state, delivery_center, type, latitude, longitude) ⇒ Postcode

Returns a new instance of Postcode.



13
14
15
16
17
18
19
20
21
# File 'lib/australia/postcode.rb', line 13

def initialize(postcode, suburb, state, delivery_center, type, latitude, longitude)
  @postcode         = postcode.to_i
  @suburb           = suburb.strip
  @state            = state.strip
  @delivery_center  = delivery_center.strip
  @type             = type.strip
  @latitude         = latitude.to_f
  @longitude        = longitude.to_f
end

Instance Attribute Details

#delivery_centerObject (readonly)

Returns the value of attribute delivery_center.



11
12
13
# File 'lib/australia/postcode.rb', line 11

def delivery_center
  @delivery_center
end

#latitudeObject (readonly)

Returns the value of attribute latitude.



11
12
13
# File 'lib/australia/postcode.rb', line 11

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude.



11
12
13
# File 'lib/australia/postcode.rb', line 11

def longitude
  @longitude
end

#postcodeObject (readonly)

Returns the value of attribute postcode.



11
12
13
# File 'lib/australia/postcode.rb', line 11

def postcode
  @postcode
end

#stateObject (readonly)

Returns the value of attribute state.



11
12
13
# File 'lib/australia/postcode.rb', line 11

def state
  @state
end

#suburbObject (readonly)

Returns the value of attribute suburb.



11
12
13
# File 'lib/australia/postcode.rb', line 11

def suburb
  @suburb
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/australia/postcode.rb', line 11

def type
  @type
end

Class Method Details

.find(postcode) ⇒ Array[Postcode]

Finds all postcodes/suburbs for the given postcode.

Returns:



82
83
84
# File 'lib/australia/postcode.rb', line 82

def find(postcode)
  indexed_on_postcode[postcode.to_i]
end

.find_by_coords(latitude, longitude) ⇒ Postcode

Finds the closest postcode to the given coordinates

Returns:



96
97
98
99
100
# File 'lib/australia/postcode.rb', line 96

def find_by_coords(latitude, longitude)
  data.min_by { |postcode|
    (latitude - postcode.latitude) ** 2 + (longitude - postcode.longitude) ** 2
  }
end

.find_by_suburb(suburb) ⇒ Array[Postcode]

Finds all the postcodes/suburbs for the given suburb name.

Returns:



89
90
91
# File 'lib/australia/postcode.rb', line 89

def find_by_suburb(suburb)
  indexed_on_suburb[suburb.strip.upcase]
end

Instance Method Details

#coordsArray

Returns a [latitude, longitude] tuple

Returns:

  • (Array)


26
27
28
# File 'lib/australia/postcode.rb', line 26

def coords
  [latitude, longitude]
end

#distance(other) ⇒ Float Also known as: -

Computes the distance from this postcode to another postcode using the Haversine formula

Returns:

  • (Float)


34
35
36
37
38
39
40
41
42
43
# File 'lib/australia/postcode.rb', line 34

def distance(other)
  earth_radius = 6371
  Δlat = radians(other.latitude - latitude)
  Δlong = radians(other.longitude - longitude)
  a = sin(Δlat / 2) * sin(Δlat / 2) +
      cos(radians(latitude)) * cos(radians(other.latitude)) *
      sin(Δlong / 2) * sin(Δlong / 2)
  c = 2 * atan2((a), (1 - a))
  earth_radius * c
end

#inspectObject

Inspects the [Postcode]



48
49
50
# File 'lib/australia/postcode.rb', line 48

def inspect
  "#<#{self.class} postcode=#{postcode.inspect} suburb=#{suburb.inspect} latitude=#{latitude.inspect} longitude=#{longitude.inspect}>"
end