Class: Australia::Postcode
- Inherits:
-
Object
- Object
- Australia::Postcode
- 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
-
#delivery_center ⇒ Object
readonly
Returns the value of attribute delivery_center.
-
#latitude ⇒ Object
readonly
Returns the value of attribute latitude.
-
#longitude ⇒ Object
readonly
Returns the value of attribute longitude.
-
#postcode ⇒ Object
readonly
Returns the value of attribute postcode.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#suburb ⇒ Object
readonly
Returns the value of attribute suburb.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.find(postcode) ⇒ Array[Postcode]
Finds all postcodes/suburbs for the given postcode.
-
.find_by_coords(latitude, longitude) ⇒ Postcode
Finds the closest postcode to the given coordinates.
-
.find_by_suburb(suburb) ⇒ Array[Postcode]
Finds all the postcodes/suburbs for the given suburb name.
Instance Method Summary collapse
-
#coords ⇒ Array
Returns a [latitude, longitude] tuple.
-
#distance(other) ⇒ Float
(also: #-)
Computes the distance from this postcode to another postcode using the Haversine formula.
-
#initialize(postcode, suburb, state, delivery_center, type, latitude, longitude) ⇒ Postcode
constructor
A new instance of Postcode.
-
#inspect ⇒ Object
Inspects the [Postcode].
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_center ⇒ Object (readonly)
Returns the value of attribute delivery_center.
11 12 13 |
# File 'lib/australia/postcode.rb', line 11 def delivery_center @delivery_center end |
#latitude ⇒ Object (readonly)
Returns the value of attribute latitude.
11 12 13 |
# File 'lib/australia/postcode.rb', line 11 def latitude @latitude end |
#longitude ⇒ Object (readonly)
Returns the value of attribute longitude.
11 12 13 |
# File 'lib/australia/postcode.rb', line 11 def longitude @longitude end |
#postcode ⇒ Object (readonly)
Returns the value of attribute postcode.
11 12 13 |
# File 'lib/australia/postcode.rb', line 11 def postcode @postcode end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
11 12 13 |
# File 'lib/australia/postcode.rb', line 11 def state @state end |
#suburb ⇒ Object (readonly)
Returns the value of attribute suburb.
11 12 13 |
# File 'lib/australia/postcode.rb', line 11 def suburb @suburb end |
#type ⇒ Object (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.
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
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.
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
#coords ⇒ Array
Returns a [latitude, longitude] tuple
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
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 |
#inspect ⇒ Object
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 |