Class: Abortron::ClinicFinder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yml_file) ⇒ ClinicFinder

Returns a new instance of ClinicFinder.



10
11
12
# File 'lib/clinic_finder.rb', line 10

def initialize(yml_file)
@clinics = ::YAML.load_file(yml_file)
end

Instance Attribute Details

#clinicsObject (readonly)

Returns the value of attribute clinics.



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

def clinics
  @clinics
end

Instance Method Details

#calculate_distanceObject



39
40
41
42
43
44
45
46
47
# File 'lib/clinic_finder.rb', line 39

def calculate_distance
  distances = []
  @coordinates_hash.each do |name, coordinates|
    ll = Geokit::LatLng.new(coordinates[0], coordinates[1])
    distances << {name: name, distance: ll.distance_to(@patient_float_coordinates)}
    # distances = [ {name: "Oakland", distance: 2}, {name: "San Francisco", distance: 1} ]
  end
  @distances = distances.sort_by {|distance| distance[:distance]}
end

#clinics_coordinates_conversionObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/clinic_finder.rb', line 23

def clinics_coordinates_conversion
  @coordinates_hash = {}
  @clinic_addresses.map! do |address| # {name: 'Oakland Clinic', address: '101 Main St, Oakland, CA'}
    location = ::Geokit::Geocoders::GoogleGeocoder.geocode(address[:address])
    float_coordinates = location.ll.split(',').map(&:to_f)
    @coordinates_hash[address[:name]] = float_coordinates
    sleep(0.5)
  end
  @coordinates_hash
end

#create_full_address(gestational_age) ⇒ Object

need to test filtering gestational limit



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

def create_full_address(gestational_age) # need to test filtering gestational limit
  @clinic_addresses = []
  filtered_clinics = @clinics.keep_if { |name, info| gestational_age < info['gestational_limit']}
  filtered_clinics.each do |clinic, info|
    @clinic_addresses << {name: clinic, address: "#{info['street_address']}, #{info['city']}, #{info['state']}"}
  end
  @clinic_addresses
end

#find_closest_clinicsObject



50
51
52
# File 'lib/clinic_finder.rb', line 50

def find_closest_clinics
  @distances[0..2]
end

#locate_cheapest_clinic(gestational_age:, naf_clinics_only: false) ⇒ Object



63
64
65
66
67
# File 'lib/clinic_finder.rb', line 63

def locate_cheapest_clinic(gestational_age:, naf_clinics_only: false)
  @helper = ::ClinicFinder::GestationHelper.new(gestational_age)
  @gestational_tier = @helper.gestational_tier
  decorate_data(available_clinics)
end

#locate_nearest_clinic(patient_zipcode:, gestational_age:) ⇒ Object

need to write test to make sure everything gets called



55
56
57
58
59
60
61
# File 'lib/clinic_finder.rb', line 55

def locate_nearest_clinic(patient_zipcode:, gestational_age:)
  patient_coordinates_conversion(patient_zipcode)
  create_full_address(gestational_age)
  clinics_coordinates_conversion
  calculate_distance
  find_closest_clinics
end

#patient_coordinates_conversion(patient_zipcode) ⇒ Object



34
35
36
37
# File 'lib/clinic_finder.rb', line 34

def patient_coordinates_conversion(patient_zipcode)
  @patient_location = ::Geokit::Geocoders::GoogleGeocoder.geocode(patient_zipcode)
  @patient_float_coordinates = @patient_location.ll
end