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



89
90
91
# File 'app/models/medivo/appointment.rb', line 89

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

.build_fake_data(date) ⇒ Object



80
81
82
83
84
85
86
87
# File 'app/models/medivo/appointment.rb', line 80

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



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

def filter_data(times, am_pm, search_date)
  times.collect { |time| AppointmentTime.new(time) }
      .select { |at| at.match(am_pm, Time.now) }
      .sort_by {|at| at.nearness_to_date(search_date) }
      .collect { |at| at.time_str }
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’]



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}
    # 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
  search_date = Time.parse(date)
  filter_data(data, am_pm, search_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



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

Returns:

  • (Boolean)


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

.resourceObject



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