Class: Medivo::Appointment
- Inherits:
-
Object
- Object
- Medivo::Appointment
- Defined in:
- app/models/medivo/appointment.rb
Constant Summary collapse
- LABCORP_FORMAT =
"%m/%d/%Y|%H:%M %p"
- MDY_FORMAT =
"%m/%d/%Y"
Class Method Summary collapse
- .build_date(date, time) ⇒ Object
- .build_fake_data(date) ⇒ Object
-
.filter_data(times, am_pm, search_date) ⇒ Object
ordering the times, firstly by proximity to search date, and then by the time in the day.
-
.find(lab_code, date, am_pm = '') ⇒ Object
Find a labcorp appointment.
-
.make(lab_code, time, user) ⇒ Object
Make the labcorp appointment.
-
.real_data? ⇒ Boolean
using real data? the flag is set in the config/appointment_resource.yml.
- .resource ⇒ Object
Class Method Details
.build_date(date, time) ⇒ Object
100 101 102 |
# File 'app/models/medivo/appointment.rb', line 100 def build_date(date, time) "#{date.strftime(MDY_FORMAT)}|#{time}" end |
.build_fake_data(date) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'app/models/medivo/appointment.rb', line 91 def build_fake_data(date) date = date.is_a?(String) ? Date.strptime(date, MDY_FORMAT) : date [ build_date(date, "08:30 AM"), build_date(date, "10:30 AM"), build_date((date + 1), "03:30 PM"), build_date((date+1), "01:30 PM"), build_date((date + 2), "10:30 AM"), build_date((date+1), "03:00 PM"), ] end |
.filter_data(times, am_pm, search_date) ⇒ Object
ordering the times, firstly by proximity to search date, and then by the time in the day
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/medivo/appointment.rb', line 76 def filter_data(times, am_pm, search_date) # get the times into groups by date dates = times .collect { |time| AppointmentTime.new(time) } .select { |at| at.match(am_pm, Time.now) } .group_by(&:date) # order the dates by nearness to search date keys = dates.keys.sort_by {|date| (date - search_date).to_i.abs } # order the times in each day and return the times as a strings keys .collect{|date| dates[date].sort_by(&:time) } .flatten .collect { |at| at.time_str } end |
.find(lab_code, date, am_pm = '') ⇒ Object
Find a labcorp appointment
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/medivo/appointment.rb', line 21 def find(lab_code, date, am_pm='') if real_data? data = resource.get :params=>{:labcorp_id=>lab_code, :appointment_date=>date.strftime("%m/%d/%Y")} # data is json encoded hash: { :times => ['time1', 'time2'] } data = JSON.parse(data)['times'] else data = build_fake_data(date) end # filter the hash before returning filter_data(data, am_pm, date) end |
.make(lab_code, time, user) ⇒ Object
Make the labcorp appointment
45 46 47 48 49 50 51 52 53 54 |
# File 'app/models/medivo/appointment.rb', line 45 def make(lab_code, time, user) time_id = time.strftime(LABCORP_FORMAT) if real_data? data = resource.post :labcorp_id=>lab_code, :time_id=>time_id, :user=>user # data is json { :confimation => ( 'real number' or nil if no appointment made ) } JSON.parse(data)['confirmation'] else nil end end |
.real_data? ⇒ Boolean
using real data? the flag is set in the config/appointment_resource.yml
68 69 70 71 |
# File 'app/models/medivo/appointment.rb', line 68 def real_data? resource # to init the resource and config file true unless !Rails.env.test? and @config.real_data == false end |
.resource ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'app/models/medivo/appointment.rb', line 56 def resource @resource ||= begin @config = ResourceConfig.find 'appointment_resource.yml' RestClient::Resource.new @config.href, :timeout => (@config.timeout || 12) rescue => e Rails.logger.error e.inspect # blow up later, so the server can start end end |