Module: Common::Client::Concerns::MHVSessionBasedClient

Extended by:
ActiveSupport::Concern
Includes:
SentryLogging
Included in:
BB::Client, MHVLogging::Client, Rx::Client, Rx::MedicationsClient, SM::Client
Defined in:
lib/common/client/concerns/mhv_session_based_client.rb

Overview

Module mixin for overriding session logic when making MHV client connections

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SentryLogging

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

Instance Attribute Details

#sessionHash (readonly)

Returns a hash containing session information.

Returns:

  • (Hash)

    a hash containing session information



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 17

module MHVSessionBasedClient
  extend ActiveSupport::Concern
  include SentryLogging

  ##
  # @param session [Hash] a hash containing user_id with which the session will be found or built
  #
  def initialize(session:)
    @session = self.class.client_session.find_or_build(session)
  end

  attr_reader :session

  ##
  # Ensures the MHV based session is not expired
  #
  # @return [MHVSessionBasedClient] instance of `self`
  #
  def authenticate
    if session.expired?
      @session = get_session
      @session.save
    end
    self
  end

  ##
  # Creates a session from the request headers
  #
  # @return [Rx::ClientSession] if an Rx (Prescription) client session
  # @return [SM::ClientSession] if a SM (Secure Messaging) client session
  #
  def get_session
    env = get_session_tagged
    req_headers = env.request_headers
    res_headers = env.response_headers
    @session.class.new(user_id: req_headers['mhvCorrelationId'],
                       expires_at: res_headers['expires'],
                       token: res_headers['token'])
  end

  ##
  # Override client_session method to use extended ::ClientSession classes
  #
  module ClassMethods
    ##
    # @return [Rx::ClientSession] if an Rx (Prescription) client session
    # @return [SM::ClientSession] if a SM (Secure Messaging) client session
    #
    def client_session(klass = nil)
      @client_session ||= klass
    end
  end

  private

  def get_session_tagged
    Raven.tags_context(error: 'mhv_session')
    env = perform(:get, 'session', nil, auth_headers)
    Raven.context.tags.delete(:error)
    env
  end

  def token_headers
    config.base_request_headers.merge('Token' => session.token)
  end

  def auth_headers
    config.base_request_headers.merge('appToken' => config.app_token, 'mhvCorrelationId' => session.user_id.to_s)
  end
end

Instance Method Details

#authenticateMHVSessionBasedClient

Ensures the MHV based session is not expired

Returns:



35
36
37
38
39
40
41
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 35

def authenticate
  if session.expired?
    @session = get_session
    @session.save
  end
  self
end

#get_sessionRx::ClientSession, SM::ClientSession

Creates a session from the request headers

Returns:



49
50
51
52
53
54
55
56
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 49

def get_session
  env = get_session_tagged
  req_headers = env.request_headers
  res_headers = env.response_headers
  @session.class.new(user_id: req_headers['mhvCorrelationId'],
                     expires_at: res_headers['expires'],
                     token: res_headers['token'])
end

#initialize(session:) ⇒ Object

Parameters:

  • session (Hash)

    a hash containing user_id with which the session will be found or built



24
25
26
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 24

def initialize(session:)
  @session = self.class.client_session.find_or_build(session)
end