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

STATSD_KEY_PREFIX =
'api.mhv.rxrefill'
CACHE_TTL =

1 hour cache

3600 * 1
CACHE_TTL_ZERO =
0

Constants included from Common::Client::Concerns::MhvLockedSessionClient

Common::Client::Concerns::MhvLockedSessionClient::LOCK_RETRY_DELAY, Common::Client::Concerns::MhvLockedSessionClient::RETRY_ATTEMPTS

Instance Attribute Summary

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

#session

Attributes included from Common::Client::Concerns::MhvLockedSessionClient

#session

Instance Method Summary collapse

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

#auth_headers, #get_session, #get_session_tagged, #invalid?, #session_config_key, #token_headers, #user_key

Methods included from SentryLogging

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

Methods included from Common::Client::Concerns::MhvLockedSessionClient

#authenticate, #invalid?, #lock_and_get_session, #obtain_redis_lock, #refresh_session, #release_redis_lock

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #sanitize_headers!, #service_name

Constructor Details

#initialize(session:, upstream_request: nil) ⇒ Client

Returns a new instance of Client.



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

def initialize(session:, upstream_request: nil)
  @upstream_request = upstream_request
  super(session:)
end

Instance Method Details

#cache_key(action) ⇒ Object (private)



195
196
197
198
199
200
# File 'lib/rx/client.rb', line 195

def cache_key(action)
  return nil unless config.caching_enabled?
  return nil if session.user_id.blank?

  "#{session.user_id}:#{action}"
end

#get_active_rxsCommon::Collection[Prescription]

Get a list of active Prescriptions



40
41
42
43
44
# File 'lib/rx/client.rb', line 40

def get_active_rxs
  Common::Collection.fetch(::Prescription, cache_key: cache_key('getactiverx'), ttl: CACHE_TTL_ZERO) 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



51
52
53
54
55
# File 'lib/rx/client.rb', line 51

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



74
75
76
77
78
# File 'lib/rx/client.rb', line 74

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



62
63
64
65
66
# File 'lib/rx/client.rb', line 62

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

#get_notification_email_addressObject (private)

Current Email Account that receives notifications



213
214
215
# File 'lib/rx/client.rb', line 213

def get_notification_email_address
  config.parallel_connection.get('preferences/email', nil, token_headers).body
end

#get_preferencesPrescriptionPreference

TODO:

Might need better error handling around this.

Get Prescription preferences



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

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:



95
96
97
98
# File 'lib/rx/client.rb', line 95

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:



106
107
108
109
# File 'lib/rx/client.rb', line 106

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

#get_rx_documentation(ndc) ⇒ Common::Collection[PrescriptionDocumentation]

Get documentation for a single prescription



85
86
87
# File 'lib/rx/client.rb', line 85

def get_rx_documentation(ndc)
  perform(:get, "prescription/getrxdoc/#{ndc}", nil, token_headers).body
end

#get_rx_preference_flagObject (private)

Current Rx preference setting



218
219
220
# File 'lib/rx/client.rb', line 218

def get_rx_preference_flag
  config.parallel_connection.get('preferences/rx', nil, token_headers).body
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:



129
130
131
132
133
# File 'lib/rx/client.rb', line 129

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:



117
118
119
120
121
# File 'lib/rx/client.rb', line 117

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

#increment_refill(count = 1) ⇒ Object (private)



202
203
204
205
206
# File 'lib/rx/client.rb', line 202

def increment_refill(count = 1)
  tags = []
  tags.append("source_app:#{@upstream_request.env['SOURCE_APP']}") if @upstream_request
  StatsD.increment("#{STATSD_KEY_PREFIX}.refills.requested", count, tags:)
end

#post_notification_email_address(params) ⇒ Object (private)

Change Email Account that receives notifications



223
224
225
# File 'lib/rx/client.rb', line 223

def post_notification_email_address(params)
  config.parallel_connection.post('preferences/email', params, token_headers)
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:



186
187
188
189
190
191
# File 'lib/rx/client.rb', line 186

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)


154
155
156
157
158
159
160
161
# File 'lib/rx/client.rb', line 154

def post_refill_rx(id)
  if (result = perform(:post, "prescription/rxrefill/#{id}", nil, token_headers))
    keys = [cache_key('getactiverx'), cache_key('gethistoryrx')].compact
    Common::Collection.bust(keys) unless keys.empty?
    increment_refill
  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)


141
142
143
144
145
146
# File 'lib/rx/client.rb', line 141

def post_refill_rxs(ids)
  if (result = perform(:post, 'prescription/rxrefill', ids, token_headers))
    increment_refill(ids.size)
  end
  result
end

#post_rx_preference_flag(params) ⇒ Object (private)

Change Rx preference setting



228
229
230
231
# File 'lib/rx/client.rb', line 228

def post_rx_preference_flag(params)
  params = { flag: params[:rx_flag] }
  config.parallel_connection.post('preferences/rx', params, token_headers)
end

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



29
30
31
32
33
# File 'lib/rx/client.rb', line 29

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