Class: Rx::Client

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::MHVSessionBasedClient
Defined in:
lib/rx/client.rb

Overview

Core class responsible for Rx API interface operations

Direct Known Subclasses

MedicationsClient

Constant Summary collapse

CACHE_TTL =

1 hour cache

3600 * 1

Instance Attribute Summary

Attributes included from Common::Client::Concerns::MHVSessionBasedClient

#session

Instance Method Summary collapse

Methods included from Common::Client::Concerns::MHVSessionBasedClient

#authenticate, #get_session, #initialize

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger

Methods inherited from Common::Client::Base

configuration, #raise_backend_exception

Instance Method Details

#get_active_rxsCommon::Collection[Prescription]

Get a list of active Prescriptions



33
34
35
36
37
# File 'lib/rx/client.rb', line 33

def get_active_rxs
  Common::Collection.fetch(::Prescription, cache_key: cache_key('getactiverx'), ttl: CACHE_TTL) do
    perform(:get, 'prescription/getactiverx', nil, token_headers).body
  end
end

#get_active_rxs_with_detailsCommon::Collection[PrescriptionDetails]

Get a list of active Prescriptions using new model PrescriptionDetails



44
45
46
47
48
# File 'lib/rx/client.rb', line 44

def get_active_rxs_with_details
  Common::Collection.fetch(::PrescriptionDetails, cache_key: cache_key('getactiverx'), ttl: CACHE_TTL) do
    perform(:get, 'prescription/getactiverx', nil, token_headers).body
  end
end

#get_all_rxsCommon::Collection[PrescriptionDetails]

Get a list of all Prescriptions using different api endpoint that returns additional data per rx compared to /gethistoryrx



67
68
69
70
71
# File 'lib/rx/client.rb', line 67

def get_all_rxs
  Common::Collection.fetch(::PrescriptionDetails, cache_key: cache_key('medications'), ttl: CACHE_TTL) do
    perform(:get, 'prescription/medications', nil, token_headers).body
  end
end

#get_history_rxsCommon::Collection[Prescription]

Get a list of all Prescriptions



55
56
57
58
59
# File 'lib/rx/client.rb', line 55

def get_history_rxs
  Common::Collection.fetch(::Prescription, cache_key: cache_key('gethistoryrx'), ttl: CACHE_TTL) do
    perform(:get, 'prescription/gethistoryrx', nil, token_headers).body
  end
end

#get_preferencesPrescriptionPreference

TODO:

Might need better error handling around this.

Get Prescription preferences



151
152
153
154
155
156
157
158
# File 'lib/rx/client.rb', line 151

def get_preferences
  response = {}
  config.parallel_connection.in_parallel do
    response.merge!(get_notification_email_address)
    response.merge!(rx_flag: get_rx_preference_flag[:flag])
  end
  PrescriptionPreference.new(response)
end

#get_rx(id) ⇒ Prescription

Get a single Prescription

Parameters:

  • id (Fixnum)

    An Rx id

Returns:



79
80
81
82
# File 'lib/rx/client.rb', line 79

def get_rx(id)
  collection = get_history_rxs
  collection.find_first_by('prescription_id' => { 'eq' => id })
end

#get_rx_details(id) ⇒ Prescription

Get a single Prescription using different api endpoint that returns additional data compared to /gethistoryrx

Parameters:

  • id (Fixnum)

    An Rx id

Returns:



90
91
92
93
# File 'lib/rx/client.rb', line 90

def get_rx_details(id)
  collection = get_all_rxs
  collection.find_first_by('prescription_id' => { 'eq' => id })
end

#get_tracking_history_rx(id) ⇒ Common::Collection[Tracking]

Get a list of tracking history for a Prescription

Parameters:

  • id (Fixnum)

    an Rx id

Returns:



113
114
115
116
117
# File 'lib/rx/client.rb', line 113

def get_tracking_history_rx(id)
  json = perform(:get, "prescription/rxtracking/#{id}", nil, token_headers).body
  tracking_history = json[:data].map { |t| t.to_h.merge(prescription_id: id) }
  Common::Collection.new(::Tracking, **json.merge(data: tracking_history))
end

#get_tracking_rx(id) ⇒ Tracking

Get tracking for a Prescription

Parameters:

  • id (Fixnum)

    an Rx id

Returns:



101
102
103
104
105
# File 'lib/rx/client.rb', line 101

def get_tracking_rx(id)
  json = perform(:get, "prescription/rxtracking/#{id}", nil, token_headers).body
  data = json[:data].first.merge(prescription_id: id)
  Tracking.new(json.merge(data:))
end

#post_preferences(params) ⇒ PrescriptionPreference

Note:

Don’t do this one in parallel since you want it to behave like a single atomic operation.

Set Prescription preferences

Returns:

Raises:



168
169
170
171
172
173
# File 'lib/rx/client.rb', line 168

def post_preferences(params)
  mhv_params = PrescriptionPreference.new(params).mhv_params
  post_notification_email_address(mhv_params.slice(:email_address))
  post_rx_preference_flag(mhv_params.slice(:rx_flag))
  get_preferences
end

#post_refill_rx(id) ⇒ Faraday::Env

Post a Prescription refill

Parameters:

  • id (Fixnum)

    an Rx id

Returns:

  • (Faraday::Env)


138
139
140
141
142
143
# File 'lib/rx/client.rb', line 138

def post_refill_rx(id)
  if (result = perform(:post, "prescription/rxrefill/#{id}", nil, token_headers))
    Common::Collection.bust([cache_key('getactiverx'), cache_key('gethistoryrx')])
  end
  result
end

#post_refill_rxs(ids) ⇒ Faraday::Env

Post a list of Prescription refills

Parameters:

  • ids (Array)

    an array of Rx ids

Returns:

  • (Faraday::Env)


125
126
127
128
129
130
# File 'lib/rx/client.rb', line 125

def post_refill_rxs(ids)
  if (result = perform(:post, 'prescription/rxrefill', ids, token_headers))
    Common::Collection.bust([cache_key('getactiverx'), cache_key('gethistoryrx')])
  end
  result
end

#request(method, path, params = {}, headers = {}, options = {}) ⇒ Object



22
23
24
25
26
# File 'lib/rx/client.rb', line 22

def request(method, path, params = {}, headers = {}, options = {})
  super(method, path, params, headers, options)
rescue Common::Exceptions::GatewayTimeout
  raise Rx::RxGatewayTimeout
end