change_healthcare-eligibility 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +130 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +59 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +6 -0
- data/bin/console +11 -0
- data/bin/rspec +29 -0
- data/bin/setup +8 -0
- data/change_healthcare-eligibility.gemspec +44 -0
- data/lib/change_healthcare/eligibility.rb +13 -0
- data/lib/change_healthcare/eligibility/inspector.rb +82 -0
- data/lib/change_healthcare/eligibility/swagger_client.rb +81 -0
- data/lib/change_healthcare/eligibility/swagger_client/api_client.rb +388 -0
- data/lib/change_healthcare/eligibility/swagger_client/api_error.rb +40 -0
- data/lib/change_healthcare/eligibility/swagger_client/configuration.rb +202 -0
- data/lib/change_healthcare/eligibility/swagger_client/eligibility_api.rb +198 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/additional_identification.rb +266 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/additional_information.rb +185 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/address.rb +218 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/benefits_additional_information.rb +376 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/benefits_date_information.rb +340 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/benefits_information.rb +338 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/benefits_related_entity.rb +240 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/benefits_service_delivery.rb +220 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/composite_medical_procedure_identifier.rb +218 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/eligibility_additional_information.rb +200 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/encounter.rb +359 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/error.rb +205 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/health_care_diagnosis_code.rb +190 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/health_care_information.rb +229 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/health_check.rb +190 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/information_receiver_name.rb +303 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/loop_2000_cd2100_cd_segment_nm1_ref_dmg_ins_mpi_prv_notes_response_member.rb +429 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/loop_2100_a_segments_nm1_and_per_notes_information_source_name.rb +293 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/loop_2100_a_segments_per_element_per03_8_notes_communication.rb +194 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/loop_2100_a_segments_per_notes_information_source_contact_information.rb +198 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/loop_2100_bcd_segment_nm1_prv_notes_provider.rb +322 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/loop_2100_cd_segment_trn_notes_subscriber_trace_numbers.rb +218 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/loop_2110_cd_segments_ref_notes_subscriber_dependent_additional_identification.rb +488 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/medical_eligibility.rb +272 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/medical_procedure.rb +254 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/plan_date_information.rb +340 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/plan_status.rb +204 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/provider.rb +304 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/provider_information.rb +190 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/raw_x12_request.rb +190 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/request_dependent.rb +452 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/request_subscriber.rb +456 -0
- data/lib/change_healthcare/eligibility/swagger_client/models/response.rb +322 -0
- data/lib/change_healthcare/eligibility/swagger_client/version.rb +17 -0
- data/lib/change_healthcare/eligibility/version.rb +5 -0
- data/lib/change_healthcare/eligibility/wrapper.rb +124 -0
- metadata +214 -0
@@ -0,0 +1,322 @@
|
|
1
|
+
# #Eligibility
|
2
|
+
#
|
3
|
+
# Reference for developers
|
4
|
+
#
|
5
|
+
# OpenAPI spec version: v3
|
6
|
+
#
|
7
|
+
# Generated by: https://github.com/swagger-api/swagger-codegen.git
|
8
|
+
# Swagger Codegen version: 2.4.17
|
9
|
+
#
|
10
|
+
|
11
|
+
require 'date'
|
12
|
+
|
13
|
+
module ChangeHealthcare
|
14
|
+
module Eligibility
|
15
|
+
module SwaggerClient
|
16
|
+
class Response
|
17
|
+
attr_accessor :benefits_information, :errors, :implementation_transaction_set_syntax_error, :plan_date_information, :plan_status, :status, :transaction_set_acknowledgement, :x12
|
18
|
+
|
19
|
+
# Segment: ISA, Element: ISA13, Notes: Interchange Control Number original request
|
20
|
+
attr_accessor :control_number
|
21
|
+
|
22
|
+
# Loop: 2100D, Notes: Dependent Details
|
23
|
+
attr_accessor :dependents
|
24
|
+
|
25
|
+
# Loop: 2100A, Segments: NM1 and PER, Notes: Information Source Name
|
26
|
+
attr_accessor :payer
|
27
|
+
|
28
|
+
# Loop: 2110[C|D], Segments: REF, Notes: Subscriber/Dependent Additional Identification
|
29
|
+
attr_accessor :plan_information
|
30
|
+
|
31
|
+
# Loop: 2000B/2100B, Segment: NM1 and PRV, Notes: Information Receiver
|
32
|
+
attr_accessor :provider
|
33
|
+
|
34
|
+
# Segment: ISA, Element: ISA13, Notes: Interchange Control Number
|
35
|
+
attr_accessor :reassociation_key
|
36
|
+
|
37
|
+
# Loop: 2000C/2100C, Segment: NM1, REF, DMG, INS, MPI, PRV, Notes: Subscriber Details
|
38
|
+
attr_accessor :subscriber
|
39
|
+
|
40
|
+
# Loop: 2100[C|D], Segment: TRN, Notes: Subscriber Trace Numbers
|
41
|
+
attr_accessor :subscriber_trace_numbers
|
42
|
+
|
43
|
+
# This property is a feature of Trading Partner API, if you are not using Trading Partner API
|
44
|
+
attr_accessor :trading_partner_id
|
45
|
+
|
46
|
+
# This is the payorId or Identification Code that was sent in the 270
|
47
|
+
attr_accessor :trading_partner_service_id
|
48
|
+
|
49
|
+
# Attribute mapping from ruby-style variable name to JSON key.
|
50
|
+
def self.attribute_map
|
51
|
+
{
|
52
|
+
'benefits_information': :benefitsInformation,
|
53
|
+
'control_number': :controlNumber,
|
54
|
+
'dependents': :dependents,
|
55
|
+
'errors': :errors,
|
56
|
+
'implementation_transaction_set_syntax_error': :implementationTransactionSetSyntaxError,
|
57
|
+
'payer': :payer,
|
58
|
+
'plan_date_information': :planDateInformation,
|
59
|
+
'plan_information': :planInformation,
|
60
|
+
'plan_status': :planStatus,
|
61
|
+
'provider': :provider,
|
62
|
+
'reassociation_key': :reassociationKey,
|
63
|
+
'status': :status,
|
64
|
+
'subscriber': :subscriber,
|
65
|
+
'subscriber_trace_numbers': :subscriberTraceNumbers,
|
66
|
+
'trading_partner_id': :tradingPartnerId,
|
67
|
+
'trading_partner_service_id': :tradingPartnerServiceId,
|
68
|
+
'transaction_set_acknowledgement': :transactionSetAcknowledgement,
|
69
|
+
'x12': :x12
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
# Attribute type mapping.
|
74
|
+
def self.swagger_types
|
75
|
+
{
|
76
|
+
'benefits_information': :'Array<BenefitsInformation>',
|
77
|
+
'control_number': :String,
|
78
|
+
'dependents': :'Array<Loop2000CD2100CDSegmentNM1REFDMGINSMPIPRVNotesResponseMember>',
|
79
|
+
'errors': :'Array<Error>',
|
80
|
+
'implementation_transaction_set_syntax_error': :String,
|
81
|
+
'payer': :Loop2100ASegmentsNM1AndPERNotesInformationSourceName,
|
82
|
+
'plan_date_information': :PlanDateInformation,
|
83
|
+
'plan_information': :Loop2110CDSegmentsREFNotesSubscriberDependentAdditionalIdentification,
|
84
|
+
'plan_status': :'Array<PlanStatus>',
|
85
|
+
'provider': :Loop2100BCDSegmentNM1PRVNotesProvider,
|
86
|
+
'reassociation_key': :String,
|
87
|
+
'status': :String,
|
88
|
+
'subscriber': :Loop2000CD2100CDSegmentNM1REFDMGINSMPIPRVNotesResponseMember,
|
89
|
+
'subscriber_trace_numbers': :'Array<Loop2100CDSegmentTRNNotesSubscriberTraceNumbers>',
|
90
|
+
'trading_partner_id': :String,
|
91
|
+
'trading_partner_service_id': :String,
|
92
|
+
'transaction_set_acknowledgement': :String,
|
93
|
+
'x12': :String
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
# Initializes the object
|
98
|
+
# @param [Hash] attributes Model attributes in the form of hash
|
99
|
+
def initialize(attributes = {})
|
100
|
+
return unless attributes.is_a?(Hash)
|
101
|
+
|
102
|
+
# convert string to symbol for hash key
|
103
|
+
attributes = attributes.transform_keys(&:to_sym)
|
104
|
+
|
105
|
+
if attributes.key?(:benefitsInformation)
|
106
|
+
if (value = attributes[:benefitsInformation]).is_a?(Array)
|
107
|
+
self.benefits_information = value
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
self.control_number = attributes[:controlNumber] if attributes.key?(:controlNumber)
|
112
|
+
|
113
|
+
if attributes.key?(:dependents)
|
114
|
+
if (value = attributes[:dependents]).is_a?(Array)
|
115
|
+
self.dependents = value
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
if attributes.key?(:errors)
|
120
|
+
if (value = attributes[:errors]).is_a?(Array)
|
121
|
+
self.errors = value
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
if attributes.key?(:implementationTransactionSetSyntaxError)
|
126
|
+
self.implementation_transaction_set_syntax_error = attributes[:implementationTransactionSetSyntaxError]
|
127
|
+
end
|
128
|
+
|
129
|
+
self.payer = attributes[:payer] if attributes.key?(:payer)
|
130
|
+
|
131
|
+
self.plan_date_information = attributes[:planDateInformation] if attributes.key?(:planDateInformation)
|
132
|
+
|
133
|
+
self.plan_information = attributes[:planInformation] if attributes.key?(:planInformation)
|
134
|
+
|
135
|
+
if attributes.key?(:planStatus)
|
136
|
+
if (value = attributes[:planStatus]).is_a?(Array)
|
137
|
+
self.plan_status = value
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
self.provider = attributes[:provider] if attributes.key?(:provider)
|
142
|
+
|
143
|
+
self.reassociation_key = attributes[:reassociationKey] if attributes.key?(:reassociationKey)
|
144
|
+
|
145
|
+
self.status = attributes[:status] if attributes.key?(:status)
|
146
|
+
|
147
|
+
self.subscriber = attributes[:subscriber] if attributes.key?(:subscriber)
|
148
|
+
|
149
|
+
if attributes.key?(:subscriberTraceNumbers)
|
150
|
+
if (value = attributes[:subscriberTraceNumbers]).is_a?(Array)
|
151
|
+
self.subscriber_trace_numbers = value
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
self.trading_partner_id = attributes[:tradingPartnerId] if attributes.key?(:tradingPartnerId)
|
156
|
+
|
157
|
+
self.trading_partner_service_id = attributes[:tradingPartnerServiceId] if attributes.key?(:tradingPartnerServiceId)
|
158
|
+
|
159
|
+
self.transaction_set_acknowledgement = attributes[:transactionSetAcknowledgement] if attributes.key?(:transactionSetAcknowledgement)
|
160
|
+
|
161
|
+
self.x12 = attributes[:x12] if attributes.key?(:x12)
|
162
|
+
end
|
163
|
+
|
164
|
+
# Show invalid properties with the reasons. Usually used together with valid?
|
165
|
+
# @return Array for valid properties with the reasons
|
166
|
+
def list_invalid_properties
|
167
|
+
[]
|
168
|
+
end
|
169
|
+
|
170
|
+
# Check to see if the all the properties in the model are valid
|
171
|
+
# @return true if the model is valid
|
172
|
+
def valid?
|
173
|
+
true
|
174
|
+
end
|
175
|
+
|
176
|
+
# Checks equality by comparing each attribute.
|
177
|
+
# @param [Object] Object to be compared
|
178
|
+
def ==(o)
|
179
|
+
return true if equal?(o)
|
180
|
+
|
181
|
+
self.class == o.class &&
|
182
|
+
benefits_information == o.benefits_information &&
|
183
|
+
control_number == o.control_number &&
|
184
|
+
dependents == o.dependents &&
|
185
|
+
errors == o.errors &&
|
186
|
+
implementation_transaction_set_syntax_error == o.implementation_transaction_set_syntax_error &&
|
187
|
+
payer == o.payer &&
|
188
|
+
plan_date_information == o.plan_date_information &&
|
189
|
+
plan_information == o.plan_information &&
|
190
|
+
plan_status == o.plan_status &&
|
191
|
+
provider == o.provider &&
|
192
|
+
reassociation_key == o.reassociation_key &&
|
193
|
+
status == o.status &&
|
194
|
+
subscriber == o.subscriber &&
|
195
|
+
subscriber_trace_numbers == o.subscriber_trace_numbers &&
|
196
|
+
trading_partner_id == o.trading_partner_id &&
|
197
|
+
trading_partner_service_id == o.trading_partner_service_id &&
|
198
|
+
transaction_set_acknowledgement == o.transaction_set_acknowledgement &&
|
199
|
+
x12 == o.x12
|
200
|
+
end
|
201
|
+
|
202
|
+
# @see the `==` method
|
203
|
+
# @param [Object] Object to be compared
|
204
|
+
def eql?(o)
|
205
|
+
self == o
|
206
|
+
end
|
207
|
+
|
208
|
+
# Calculates hash code according to all attributes.
|
209
|
+
# @return [Fixnum] Hash code
|
210
|
+
def hash
|
211
|
+
[benefits_information, control_number, dependents, errors, implementation_transaction_set_syntax_error, payer, plan_date_information, plan_information, plan_status, provider, reassociation_key, status, subscriber, subscriber_trace_numbers, trading_partner_id, trading_partner_service_id, transaction_set_acknowledgement, x12].hash
|
212
|
+
end
|
213
|
+
|
214
|
+
# Builds the object from hash
|
215
|
+
# @param [Hash] attributes Model attributes in the form of hash
|
216
|
+
# @return [Object] Returns the model itself
|
217
|
+
def build_from_hash(attributes)
|
218
|
+
return nil unless attributes.is_a?(Hash)
|
219
|
+
|
220
|
+
self.class.swagger_types.each_pair do |key, type|
|
221
|
+
if type =~ /\AArray<(.*)>/i
|
222
|
+
# check to ensure the input is an array given that the attribute
|
223
|
+
# is documented as an array but the input is not
|
224
|
+
if attributes[self.class.attribute_map[key]].is_a?(Array)
|
225
|
+
send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize(Regexp.last_match(1), v) })
|
226
|
+
end
|
227
|
+
elsif !attributes[self.class.attribute_map[key]].nil?
|
228
|
+
send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
|
229
|
+
end # or else data not found in attributes(hash), not an issue as the data can be optional
|
230
|
+
end
|
231
|
+
|
232
|
+
self
|
233
|
+
end
|
234
|
+
|
235
|
+
# Deserializes the data based on type
|
236
|
+
# @param string type Data type
|
237
|
+
# @param string value Value to be deserialized
|
238
|
+
# @return [Object] Deserialized data
|
239
|
+
def _deserialize(type, value)
|
240
|
+
case type.to_sym
|
241
|
+
when :DateTime
|
242
|
+
DateTime.parse(value)
|
243
|
+
when :Date
|
244
|
+
Date.parse(value)
|
245
|
+
when :String
|
246
|
+
value.to_s
|
247
|
+
when :Integer
|
248
|
+
value.to_i
|
249
|
+
when :Float
|
250
|
+
value.to_f
|
251
|
+
when :BOOLEAN
|
252
|
+
if value.to_s =~ /\A(true|t|yes|y|1)\z/i
|
253
|
+
true
|
254
|
+
else
|
255
|
+
false
|
256
|
+
end
|
257
|
+
when :Object
|
258
|
+
# generic object (usually a Hash), return directly
|
259
|
+
value
|
260
|
+
when /\AArray<(?<inner_type>.+)>\z/
|
261
|
+
inner_type = Regexp.last_match[:inner_type]
|
262
|
+
value.map { |v| _deserialize(inner_type, v) }
|
263
|
+
when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
|
264
|
+
k_type = Regexp.last_match[:k_type]
|
265
|
+
v_type = Regexp.last_match[:v_type]
|
266
|
+
{}.tap do |hash|
|
267
|
+
value.each do |k, v|
|
268
|
+
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
|
269
|
+
end
|
270
|
+
end
|
271
|
+
else # model
|
272
|
+
temp_model = SwaggerClient.const_get(type).new
|
273
|
+
temp_model.build_from_hash(value)
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
# Returns the string representation of the object
|
278
|
+
# @return [String] String presentation of the object
|
279
|
+
def to_s
|
280
|
+
to_hash.to_s
|
281
|
+
end
|
282
|
+
|
283
|
+
# to_body is an alias to to_hash (backward compatibility)
|
284
|
+
# @return [Hash] Returns the object in the form of hash
|
285
|
+
def to_body
|
286
|
+
to_hash
|
287
|
+
end
|
288
|
+
|
289
|
+
# Returns the object in the form of hash
|
290
|
+
# @return [Hash] Returns the object in the form of hash
|
291
|
+
def to_hash
|
292
|
+
hash = {}
|
293
|
+
self.class.attribute_map.each_pair do |attr, param|
|
294
|
+
value = send(attr)
|
295
|
+
next if value.nil?
|
296
|
+
|
297
|
+
hash[param] = _to_hash(value)
|
298
|
+
end
|
299
|
+
hash
|
300
|
+
end
|
301
|
+
|
302
|
+
# Outputs non-array value in the form of hash
|
303
|
+
# For object, use to_hash. Otherwise, just return the value
|
304
|
+
# @param [Object] value Any valid value
|
305
|
+
# @return [Hash] Returns the value in the form of hash
|
306
|
+
def _to_hash(value)
|
307
|
+
if value.is_a?(Array)
|
308
|
+
value.compact.map { |v| _to_hash(v) }
|
309
|
+
elsif value.is_a?(Hash)
|
310
|
+
{}.tap do |hash|
|
311
|
+
value.each { |k, v| hash[k] = _to_hash(v) }
|
312
|
+
end
|
313
|
+
elsif value.respond_to? :to_hash
|
314
|
+
value.to_hash
|
315
|
+
else
|
316
|
+
value
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# #Eligibility
|
2
|
+
#
|
3
|
+
# Reference for developers
|
4
|
+
#
|
5
|
+
# OpenAPI spec version: v3
|
6
|
+
#
|
7
|
+
# Generated by: https://github.com/swagger-api/swagger-codegen.git
|
8
|
+
# Swagger Codegen version: 2.4.17
|
9
|
+
#
|
10
|
+
|
11
|
+
module ChangeHealthcare
|
12
|
+
module Eligibility
|
13
|
+
module SwaggerClient
|
14
|
+
VERSION = '1.0.0'.freeze
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
module ChangeHealthcare
|
5
|
+
module Eligibility
|
6
|
+
##
|
7
|
+
# Wrapper module, wraps the auto-generated swagger code
|
8
|
+
# in a slightly nicer-to-use format.
|
9
|
+
#
|
10
|
+
# This is a *semi-stateful*, *thread-safe* wrapper.
|
11
|
+
# It will get auth tokens for you when needed.
|
12
|
+
class Wrapper
|
13
|
+
def initialize(client_id:, client_secret:)
|
14
|
+
@client_id = client_id
|
15
|
+
@client_secret = client_secret
|
16
|
+
@api = SwaggerClient::EligibilityApi.new
|
17
|
+
# fake auth token that is expired
|
18
|
+
@auth_token = AuthToken.new('', Time.at(0))
|
19
|
+
@mutex = Mutex.new
|
20
|
+
end
|
21
|
+
|
22
|
+
##
|
23
|
+
# Check eligibility.
|
24
|
+
#
|
25
|
+
# Will refresh auth token if needed (holding a lock while it does so for thread-safety).
|
26
|
+
#
|
27
|
+
# @param request [ChangeHealthcare::Eligibility::SwaggerClient::MedicalEligibility] the request to send
|
28
|
+
# @param opts [Hash] client options
|
29
|
+
#
|
30
|
+
# @raise [ChangeHealthcare::Eligibility::Wrapper::BadAuthRequestError] if we couldn't auth due to a bug
|
31
|
+
# @raise [ChangeHealthcare::Eligibility::Wrapper::BadAuthorizationError] if we couldn't auth due to bad creds
|
32
|
+
# @raise [ChangeHealthcare::Eligibility::Wrapper::UnknownAuthError] if something really weird happens
|
33
|
+
#
|
34
|
+
# @return [ChangeHealthcare::Eligibility::SwaggerClient::Response] the response from change
|
35
|
+
def eligibility(request, opts = {})
|
36
|
+
@api.eligibility(auth_token.auth, request, opts)
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Check health of the API
|
41
|
+
#
|
42
|
+
# @return [ChangeHealthcare::Eligibility::SwaggerClient::HealthCheck]
|
43
|
+
def health_check
|
44
|
+
@api.health_check_using_get(auth_token.auth)
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# Thrown when your authentication credentials are invalid
|
49
|
+
class BadAuthorizationError < Error; end
|
50
|
+
##
|
51
|
+
# Thrown when the authentication request is in some way malformed.
|
52
|
+
# Probably indicates a gem bug.
|
53
|
+
class BadAuthRequestError < Error; end
|
54
|
+
##
|
55
|
+
# Thrown when authentication goes wrong for some reason.
|
56
|
+
# Probably either a gem bug, or a bug in change healthcare.
|
57
|
+
class UnknownAuthError < Error; end
|
58
|
+
|
59
|
+
##
|
60
|
+
# Obtain a valid authentication token.
|
61
|
+
# This method *may* request a new token if the in-use token is expired.
|
62
|
+
# It will do this in a thread-safe manner.
|
63
|
+
#
|
64
|
+
# @raise [ChangeHealthcare::Eligibility::Wrapper::BadAuthRequestError] if we couldn't auth due to a bug
|
65
|
+
# @raise [ChangeHealthcare::Eligibility::Wrapper::BadAuthorizationError] if we couldn't auth due to bad creds
|
66
|
+
# @raise [ChangeHealthcare::Eligibility::Wrapper::UnknownAuthError] if something really weird happens
|
67
|
+
def auth_token
|
68
|
+
@mutex.synchronize do
|
69
|
+
fetch_new_token! unless @auth_token.valid?
|
70
|
+
|
71
|
+
@auth_token
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# Struct used to store auth tokens so we don't need to fetch one every request.
|
77
|
+
AuthToken = Struct.new(:token, :expires_at) do
|
78
|
+
##
|
79
|
+
# Is this token still usable?
|
80
|
+
def valid?
|
81
|
+
expires_at > (Time.new - 20)
|
82
|
+
end
|
83
|
+
|
84
|
+
##
|
85
|
+
# The actual auth header to pass in to things
|
86
|
+
def auth
|
87
|
+
"Bearer #{token}"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
def fetch_new_token!
|
94
|
+
path = "https://#{api_config.host}/apip/auth/v2/token"
|
95
|
+
uri = URI(path)
|
96
|
+
res = Net::HTTP.post_form(
|
97
|
+
uri,
|
98
|
+
'client_id' => @client_id,
|
99
|
+
'client_secret' => @client_secret,
|
100
|
+
'grant_type' => 'client_credentials'
|
101
|
+
)
|
102
|
+
body = JSON.parse(res.body)
|
103
|
+
handle_auth(body, res.code)
|
104
|
+
end
|
105
|
+
|
106
|
+
def handle_auth(body, code)
|
107
|
+
case code
|
108
|
+
when '200'
|
109
|
+
@auth_token = AuthToken.new(body['access_token'], Time.new + body['expires_in'])
|
110
|
+
when '400'
|
111
|
+
raise BadAuthorizationError, "bad authorization request (#{body['error_description']})"
|
112
|
+
when '401'
|
113
|
+
raise BadAuthRequestError, "bad auth request: (#{body['error_description']})"
|
114
|
+
else
|
115
|
+
raise UnknownAuthError, "internal problem (auth request had status #{code})"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def api_config
|
120
|
+
@api.api_client.config
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|