Class: Medivo::Appointment

Inherits:
Object
  • Object
show all
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

Class Method Details

.build_date(date, time) ⇒ Object



115
116
117
# File 'app/models/medivo/appointment.rb', line 115

def build_date(date, time)
  "#{date.strftime(MDY_FORMAT)}|#{time}"
end

.build_fake_data(date) ⇒ Object



106
107
108
109
110
111
112
113
# File 'app/models/medivo/appointment.rb', line 106

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

.cancel(confirmation, first_name, last_name) ⇒ Object

Cancel the labcorp appointment

Parameters:

  • confirmation

    confirmation number

  • user

    user info must include first_name, last_name,

Returns:

  • boolean true if success



66
67
68
69
70
71
# File 'app/models/medivo/appointment.rb', line 66

def cancel(confirmation, first_name, last_name)
  data = resource.delete params: { confirmation: confirmation,
                                  first_name: first_name,
                                  last_name: last_name }
  JSON.parse(data)['success']
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



93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/medivo/appointment.rb', line 93

def filter_data(times, am_pm, search_date)
  dates = times
      .collect { |time| AppointmentTime.new(time) } # wrap times in model
      .select { |at| at.match(am_pm, Time.now) } # filter out times
      .group_by(&:date)  # get the times into groups by date
  # order the dates by nearness to search date
  keys = dates.keys.sort_by {|date| (date - search_date).to_i.abs }
  keys
    .collect{|date| dates[date].sort_by(&:time) } # order the times in each day
    .flatten
    .collect { |at| at.time_str } # return the times as a strings
end

.find(lab_code, date, am_pm = '') ⇒ Object

Find a labcorp appointment

Parameters:

  • lab_code

    lab code

  • date

    date to seek

  • am_pm (defaults to: '')

    optional time of day value ( ‘AM’ or ‘PM’ )

Returns:

  • Array of times [‘time1’, ‘time2’]



22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/medivo/appointment.rb', line 22

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

Parameters:

  • lab_code

    lab code

  • time

    time

  • user

    user info must include date_of_birth, first_name, last_name, email_address, phone_number

Returns:

  • String confirmation number or nil if none made



46
47
48
49
50
51
52
53
54
55
# File 'app/models/medivo/appointment.rb', line 46

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

Returns:

  • (Boolean)


85
86
87
88
# File 'app/models/medivo/appointment.rb', line 85

def real_data?
  resource # to init the resource and config file
  true unless !Rails.env.test? and @config.real_data == false
end

.resourceObject



73
74
75
76
77
78
79
80
81
# File 'app/models/medivo/appointment.rb', line 73

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