Class: MedicalCopays::VBS::ResponseData

Inherits:
Object
  • Object
show all
Defined in:
app/services/medical_copays/vbs/response_data.rb

Overview

Object for handling VBS responses

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.mcp.vbs'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ ResponseData

Returns a new instance of ResponseData.



27
28
29
30
# File 'app/services/medical_copays/vbs/response_data.rb', line 27

def initialize(opts)
  @body = opts[:response].body || []
  @status = opts[:response].status
end

Instance Attribute Details

#bodyString

Returns:

  • (String)


12
13
14
15
16
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/services/medical_copays/vbs/response_data.rb', line 12

class ResponseData
  attr_reader :body, :status

  STATSD_KEY_PREFIX = 'api.mcp.vbs'

  ##
  # Builds a ResponseData instance
  #
  # @param opts [Hash]
  # @return [ResponseData] an instance of this class
  #
  def self.build(opts = {})
    new(opts)
  end

  def initialize(opts)
    @body = opts[:response].body || []
    @status = opts[:response].status
  end

  ##
  # The response hash to be returned to the {MedicalCopaysController}
  #
  # @return [Hash]
  #
  def handle
    case status
    when 200
      StatsD.increment("#{STATSD_KEY_PREFIX}.success")
      { data: transformed_body, status: }
    when 400
      StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
      { data: { message: 'Bad request' }, status: }
    when 401
      StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
      { data: { message: 'Unauthorized' }, status: }
    when 403
      StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
      { data: { message: 'Forbidden' }, status: }
    when 404
      StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
      { data: { message: 'Resource not found' }, status: }
    else
      StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
      { data: { message: 'Something went wrong' }, status: }
    end
  end

  ##
  # Camelize and lowercase all keys in the response body
  #
  # @return [Array]
  #
  def transformed_body
    statements = Flipper.enabled?(:medical_copays_six_mo_window) ? last_six_months_statements : body
    statements.each do |copay|
      (copay)
      copay.deep_transform_keys! { |key| key.camelize(:lower) }
    end
  end

  private

  ##
  # Filter statements by only the last six months
  #
  # @return [Array]
  #
  def last_six_months_statements
    cutoff_date = Time.zone.today - 6.months
    body.select do |statement|
      statement_date(statement) > cutoff_date
    end
  end

  ##
  # Custom cerner account number to match PDF
  #
  # @return [Hash]
  #
  def (statement)
    return unless (statement['pH_CERNER_ACCOUNT_NUMBER'])

    facility_id = statement['pS_FACILITY_NUM']
    patient_id = statement['pH_CERNER_PATIENT_ID']
    offset = 15 - (facility_id + patient_id).length
    padding = '0' * offset if offset >= 0

    statement['pH_CERNER_ACCOUNT_NUMBER'] = "#{facility_id}1#{padding}#{patient_id}"
  end

  ##
  # The Date object of the statement date string
  #
  # @return [Date]
  #
  def statement_date(statement)
    Time.zone.strptime(statement['pS_STATEMENT_DATE'], '%m%d%Y')
  end

  def (n)
    n.present? && n != 0 && n != '0'
  end
end

#statusInteger

Returns:

  • (Integer)


12
13
14
15
16
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/services/medical_copays/vbs/response_data.rb', line 12

class ResponseData
  attr_reader :body, :status

  STATSD_KEY_PREFIX = 'api.mcp.vbs'

  ##
  # Builds a ResponseData instance
  #
  # @param opts [Hash]
  # @return [ResponseData] an instance of this class
  #
  def self.build(opts = {})
    new(opts)
  end

  def initialize(opts)
    @body = opts[:response].body || []
    @status = opts[:response].status
  end

  ##
  # The response hash to be returned to the {MedicalCopaysController}
  #
  # @return [Hash]
  #
  def handle
    case status
    when 200
      StatsD.increment("#{STATSD_KEY_PREFIX}.success")
      { data: transformed_body, status: }
    when 400
      StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
      { data: { message: 'Bad request' }, status: }
    when 401
      StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
      { data: { message: 'Unauthorized' }, status: }
    when 403
      StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
      { data: { message: 'Forbidden' }, status: }
    when 404
      StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
      { data: { message: 'Resource not found' }, status: }
    else
      StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
      { data: { message: 'Something went wrong' }, status: }
    end
  end

  ##
  # Camelize and lowercase all keys in the response body
  #
  # @return [Array]
  #
  def transformed_body
    statements = Flipper.enabled?(:medical_copays_six_mo_window) ? last_six_months_statements : body
    statements.each do |copay|
      (copay)
      copay.deep_transform_keys! { |key| key.camelize(:lower) }
    end
  end

  private

  ##
  # Filter statements by only the last six months
  #
  # @return [Array]
  #
  def last_six_months_statements
    cutoff_date = Time.zone.today - 6.months
    body.select do |statement|
      statement_date(statement) > cutoff_date
    end
  end

  ##
  # Custom cerner account number to match PDF
  #
  # @return [Hash]
  #
  def (statement)
    return unless (statement['pH_CERNER_ACCOUNT_NUMBER'])

    facility_id = statement['pS_FACILITY_NUM']
    patient_id = statement['pH_CERNER_PATIENT_ID']
    offset = 15 - (facility_id + patient_id).length
    padding = '0' * offset if offset >= 0

    statement['pH_CERNER_ACCOUNT_NUMBER'] = "#{facility_id}1#{padding}#{patient_id}"
  end

  ##
  # The Date object of the statement date string
  #
  # @return [Date]
  #
  def statement_date(statement)
    Time.zone.strptime(statement['pS_STATEMENT_DATE'], '%m%d%Y')
  end

  def (n)
    n.present? && n != 0 && n != '0'
  end
end

Class Method Details

.build(opts = {}) ⇒ ResponseData

Builds a ResponseData instance

Parameters:

  • opts (Hash) (defaults to: {})

Returns:



23
24
25
# File 'app/services/medical_copays/vbs/response_data.rb', line 23

def self.build(opts = {})
  new(opts)
end

Instance Method Details

#account_number_present?(n) ⇒ Boolean (private)

Returns:

  • (Boolean)


112
113
114
# File 'app/services/medical_copays/vbs/response_data.rb', line 112

def (n)
  n.present? && n != 0 && n != '0'
end

#calculate_cerner_account_number(statement) ⇒ Hash (private)

Custom cerner account number to match PDF

Returns:

  • (Hash)


92
93
94
95
96
97
98
99
100
101
# File 'app/services/medical_copays/vbs/response_data.rb', line 92

def (statement)
  return unless (statement['pH_CERNER_ACCOUNT_NUMBER'])

  facility_id = statement['pS_FACILITY_NUM']
  patient_id = statement['pH_CERNER_PATIENT_ID']
  offset = 15 - (facility_id + patient_id).length
  padding = '0' * offset if offset >= 0

  statement['pH_CERNER_ACCOUNT_NUMBER'] = "#{facility_id}1#{padding}#{patient_id}"
end

#handleHash

The response hash to be returned to the MedicalCopaysController

Returns:

  • (Hash)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/medical_copays/vbs/response_data.rb', line 37

def handle
  case status
  when 200
    StatsD.increment("#{STATSD_KEY_PREFIX}.success")
    { data: transformed_body, status: }
  when 400
    StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
    { data: { message: 'Bad request' }, status: }
  when 401
    StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
    { data: { message: 'Unauthorized' }, status: }
  when 403
    StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
    { data: { message: 'Forbidden' }, status: }
  when 404
    StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
    { data: { message: 'Resource not found' }, status: }
  else
    StatsD.increment("#{STATSD_KEY_PREFIX}.failure")
    { data: { message: 'Something went wrong' }, status: }
  end
end

#last_six_months_statementsArray (private)

Filter statements by only the last six months

Returns:

  • (Array)


80
81
82
83
84
85
# File 'app/services/medical_copays/vbs/response_data.rb', line 80

def last_six_months_statements
  cutoff_date = Time.zone.today - 6.months
  body.select do |statement|
    statement_date(statement) > cutoff_date
  end
end

#statement_date(statement) ⇒ Date (private)

The Date object of the statement date string

Returns:

  • (Date)


108
109
110
# File 'app/services/medical_copays/vbs/response_data.rb', line 108

def statement_date(statement)
  Time.zone.strptime(statement['pS_STATEMENT_DATE'], '%m%d%Y')
end

#transformed_bodyArray

Camelize and lowercase all keys in the response body

Returns:

  • (Array)


65
66
67
68
69
70
71
# File 'app/services/medical_copays/vbs/response_data.rb', line 65

def transformed_body
  statements = Flipper.enabled?(:medical_copays_six_mo_window) ? last_six_months_statements : body
  statements.each do |copay|
    (copay)
    copay.deep_transform_keys! { |key| key.camelize(:lower) }
  end
end