Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Refactor Australia::Postcode.data
Browse files Browse the repository at this point in the history
- smoother CSV handling
- avoids unnecessary Postcode instantiation
- more explicit Postcode positional instantiation
  • Loading branch information
atcruice committed Aug 21, 2024
1 parent da13546 commit 88541b4
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/australia/postcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Australia
# methods for manipulating them. Right now you can find the distance between
# two postcodes and that's about it.
class Postcode
DELIVERY_AREA = "Delivery Area".freeze

attr_reader :postcode, :suburb, :state, :delivery_center, :type, :latitude, :longitude

def initialize(postcode, suburb, state, delivery_center, type, latitude, longitude)
Expand Down Expand Up @@ -85,6 +87,7 @@ def find_by_coords(latitude, longitude)
}
end

# @return [Array<Australia::Postcode>]
def all
data
end
Expand All @@ -99,12 +102,23 @@ def indexed_on_suburb
@indexed_on_suburb ||= data.group_by(&:suburb)
end

# @return [Array<Australia::Postcode>]
def data
@data ||= raw_data.map { |data| new(*data) }.select { |postcode| postcode.type == "Delivery Area" }
end

def raw_data
CSV.parse(File.read(data_filename)).drop(1)
@data ||= CSV
.table(data_filename)
.each_with_object([]) do |row, acc|
next unless row[:type].start_with?(DELIVERY_AREA)

acc.push(new(*row.fields(
:postcode,
:suburb,
:state,
:dc,
:type,
:lat,
:lon,
)))
end
end

def data_filename
Expand Down

0 comments on commit 88541b4

Please sign in to comment.