Class: Formulary::FormularyItemFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/formulary/formulary_item_factory.rb

Overview

Class to build FormularyItem (FHIR Basic) resources from a QHPPlanDrug

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plan, plan_drug) ⇒ FormularyItemFactory

Returns a new instance of FormularyItemFactory.



12
13
14
15
# File 'lib/formulary/formulary_item_factory.rb', line 12

def initialize(plan, plan_drug)
  @plan = plan
  @plan_drug = plan_drug
end

Instance Attribute Details

#planObject (readonly)

Returns the value of attribute plan.



10
11
12
# File 'lib/formulary/formulary_item_factory.rb', line 10

def plan
  @plan
end

#plan_drugObject (readonly)

Returns the value of attribute plan_drug.



10
11
12
# File 'lib/formulary/formulary_item_factory.rb', line 10

def plan_drug
  @plan_drug
end

Instance Method Details

#availability_period_extensionObject (private)



97
98
99
100
101
102
103
104
105
106
# File 'lib/formulary/formulary_item_factory.rb', line 97

def availability_period_extension
  current_year = Date.today.year
  {
    url: AVAILABILITY_PERIOD_EXTENSION,
    valuePeriod: {
      start: "#{current_year}-01-01",
      end: "#{current_year}-12-31",
    },
  }
end

#availability_status_extensionObject (private)



90
91
92
93
94
95
# File 'lib/formulary/formulary_item_factory.rb', line 90

def availability_status_extension
  {
    url: AVAILABILITY_STATUS_EXTENSION,
    valueCode: "active",
  }
end

#buildObject

Construct a FHIR Basic instance



18
19
20
21
22
23
24
25
26
27
# File 'lib/formulary/formulary_item_factory.rb', line 18

def build
  FHIR::Basic.new(
    id: FORMULARY_ITEM_ID_PREFIX + plan.id + "-" + plan_drug.rxnorm_code,
    meta: meta,
    text: text,
    extension: extension,
    code: code,
    subject: subject,
  )
end

#codeObject (private)



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/formulary/formulary_item_factory.rb', line 61

def code
  {
    coding: [
      {
        system: FORMULARY_ITEM_SYSTEM,
        code: "formulary-item",
        display: "Formulary Item",
      },
    ],
  }
end

#drug_tier_extensionObject (private)



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/formulary/formulary_item_factory.rb', line 139

def drug_tier_extension
  {
    url: DRUG_TIER_ID_EXTENSION,
    valueCodeableConcept: {
      coding: [
        {
          code: plan_drug.tier,
          display: DRUG_TIER_DISPLAY[plan_drug.tier],
          system: DRUG_TIER_SYSTEM,
        },
      ],
    },
  }
end

#extensionObject (private)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/formulary/formulary_item_factory.rb', line 45

def extension
  [
    formulary_reference_extension,
    availability_status_extension,
    availability_period_extension,
    pharmacy_type_extensions,
    drug_tier_extension,
    prior_auth_extension,
    prior_auth_new_start_extension,
    step_therapy_extension,
    step_therapy_new_start_extension,
    quantity_limit_extension,
    quantity_limit_detail_extension,
  ].flatten.compact
end

#formulary_reference_extensionObject (private)



80
81
82
83
84
85
86
87
88
# File 'lib/formulary/formulary_item_factory.rb', line 80

def formulary_reference_extension
  {
    url: FORMULARY_REFERENCE_EXTENSION,
    valueReference: {
      reference: "InsurancePlan/" + FORMULARY_ID_PREFIX + plan.id,
      type: "InsurancePlan",
    },
  }
end

#metaObject (private)



31
32
33
34
35
36
# File 'lib/formulary/formulary_item_factory.rb', line 31

def meta
  {
    profile: [FORMULARY_ITEM_PROFILE],
    lastUpdated: "2021-08-31T10:03:10Z",
  }
end

#pharmacy_type_extension(pharmacy_type) ⇒ Object (private)



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/formulary/formulary_item_factory.rb', line 122

def pharmacy_type_extension(pharmacy_type)
  return if pharmacy_type.nil?

  {
    url: PHARMACY_TYPE_EXTENSION,
    valueCodeableConcept: {
      coding: [
        {
          code: pharmacy_type,
          display: PHARMACY_TYPE_DISPLAY[pharmacy_type],
          system: PHARMACY_TYPE_SYSTEM,
        },
      ],
    },
  }
end

#pharmacy_type_extensionsObject (private)



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/formulary/formulary_item_factory.rb', line 108

def pharmacy_type_extensions
  pharmacy_list = Array.new
  # Put all pharmacy types into an array
  plan.tiers.each do |tier|
    qhp_drug = QHPDrugTier.new(tier)
    qhp_drug.cost_sharing.each do |cost_sharing|
      if (pharmacy_list.include?(cost_sharing.pharmacy_type) == false)
        pharmacy_list.push(cost_sharing.pharmacy_type)
      end
    end
  end
  pharmacy_list.map { |pharmacy_type| pharmacy_type_extension(pharmacy_type) }
end

#prior_auth_extensionObject (private)



154
155
156
157
158
159
# File 'lib/formulary/formulary_item_factory.rb', line 154

def prior_auth_extension
  {
    url: PRIOR_AUTH_EXTENSION,
    valueBoolean: plan_drug.prior_auth?,
  }
end

#prior_auth_new_start_extensionObject (private)



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/formulary/formulary_item_factory.rb', line 161

def prior_auth_new_start_extension
  if plan_drug.prior_auth?
    # Compute a number to get a new start only value related to this formulary item
    compnumber = plan_drug.rxnorm_code.tr("^0-9", "") + plan.id.tr("^0-9", "") #.to_i.digits.sum.to_s
    while compnumber.length > 1
      compnumber = compnumber.to_i.digits.sum.to_s
    end
    compnumber = compnumber.to_i % 2

    {
      url: PRIOR_AUTH_NEW_START_EXTENSION,
      valueBoolean: compnumber.zero?,
    }
  end
end

#quantity_limit_detail_extensionObject (private)



206
207
208
209
210
211
# File 'lib/formulary/formulary_item_factory.rb', line 206

def quantity_limit_detail_extension
  if plan_drug.quantity_limit?
    ext = QuantityLimitDetailExtensionFactory.new(plan_drug.name)
    ext.build
  end
end

#quantity_limit_extensionObject (private)



199
200
201
202
203
204
# File 'lib/formulary/formulary_item_factory.rb', line 199

def quantity_limit_extension
  {
    url: QUANTITY_LIMIT_EXTENSION,
    valueBoolean: plan_drug.quantity_limit?,
  }
end

#step_therapy_extensionObject (private)



177
178
179
180
181
182
# File 'lib/formulary/formulary_item_factory.rb', line 177

def step_therapy_extension
  {
    url: STEP_THERAPY_EXTENSION,
    valueBoolean: plan_drug.step_therapy_limit?,
  }
end

#step_therapy_new_start_extensionObject (private)



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/formulary/formulary_item_factory.rb', line 184

def step_therapy_new_start_extension
  if plan_drug.step_therapy_limit?
    # Compute a number to get a new start only value related to this formulary item
    compnumber = plan_drug.rxnorm_code.tr("^0-5", "") + plan.id.tr("^0-5", "") #.to_i.digits.sum.to_s
    while compnumber.length > 1
      compnumber = compnumber.to_i.digits.sum.to_s
    end
    compnumber = compnumber.to_i % 2
    {
      url: STEP_THERAPY_NEW_START_EXTENSION,
      valueBoolean: compnumber.zero?,
    }
  end
end

#subjectObject (private)



73
74
75
76
77
78
# File 'lib/formulary/formulary_item_factory.rb', line 73

def subject
  {
    reference: "MedicationKnowledge/" + DRUG_ID_PREFIX + plan_drug.rxnorm_code,
    type: "MedicationKnowledge",
  }
end

#textObject (private)



38
39
40
41
42
43
# File 'lib/formulary/formulary_item_factory.rb', line 38

def text
  {
    status: "generated",
    div: "<div xmlns=\"http://www.w3.org/1999/xhtml\">Formulary Item for Plan: #{plan.id}; RxNorm: #{plan_drug.rxnorm_code}</div>",
  }
end