Skip to content

Commit

Permalink
use versioned resource classes where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
radamson committed Apr 11, 2019
1 parent cf4be13 commit 1021e89
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 37 deletions.
1 change: 1 addition & 0 deletions lib/fhir_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require file
end

require_relative 'fhir_client/version_management'
require_relative 'fhir_client/client'
require_relative 'fhir_client/resource_address'
require_relative 'fhir_client/resource_format'
Expand Down
9 changes: 1 addition & 8 deletions lib/fhir_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Client
include FHIR::Sections::Search
include FHIR::Sections::Operations
include FHIR::Sections::Transactions
include FHIR::VersionManagement

attr_accessor :reply
attr_accessor :use_format_param
Expand Down Expand Up @@ -101,14 +102,6 @@ def use_representation_preference
@return_preference = FHIR::Formats::ReturnPreferences::REPRESENTATION
end

def versioned_resource_class(klass)
if @fhir_version == :stu3
FHIR::STU3.const_get(klass)
else
FHIR::DSTU2.const_get(klass)
end
end

def detect_version
cap = capability_statement
if cap.is_a?(FHIR::STU3::CapabilityStatement)
Expand Down
16 changes: 5 additions & 11 deletions lib/fhir_client/model/client_reply.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module FHIR
class ClientReply

include FHIR::VersionManagement

@@path_regexes = {
'[type]' => "(#{FHIR::RESOURCES.join('|')})",
'[id]' => FHIR::PRIMITIVES['id']['regex'],
Expand Down Expand Up @@ -157,11 +160,7 @@ def validate_body(name, body, body_rules)
if body_rules['types']
body_type_match = false
begin
content = if @fhir_version == :stu3
FHIR.from_contents(body)
else
FHIR::DSTU2.from_contents(body)
end
content = versioned_resource_class().from_contents(body)
body_rules['types'].each do |type|
body_type_match = true if content.resourceType == type
body_type_match = true if type == 'Resource' && validate_resource(content.resourceType)
Expand All @@ -182,12 +181,7 @@ def validate_body(name, body, body_rules)
end

def validate_resource(resource_type)
if @fhir_version == :stu3
return true if FHIR::RESOURCES.include?(resource_type)
else
return true if FHIR::DSTU2::RESOURCES.include?(resource_type)
end
false
versioned_resource_class(:RESOURCES).include?(resource_type)? true : false
end

private :validate_headers, :validate_body, :validate_resource
Expand Down
14 changes: 4 additions & 10 deletions lib/fhir_client/sections/crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,11 @@ def base_create(resource, options, format = nil, additional_header = {})
type = reply.response[:headers].detect{|x, _y| x.downcase=='content-type'}.try(:last)
if !type.nil?
reply.resource = if type.include?('xml') && !reply.body.empty?
if @fhir_version == :stu3
FHIR::STU3::Xml.from_xml(reply.body)
else
FHIR::DSTU2::Xml.from_xml(reply.body)
end
klass = self.versioned_resource_class(:Xml)
klass.from_xml(reply.body)
elsif type.include?('json') && !reply.body.empty?
if @fhir_version == :stu3
FHIR::STU3::Json.from_json(reply.body)
else
FHIR::DSTU2::Json.from_json(reply.body)
end
klass = self.versioned_resource_class(:Json)
klass.from_json(reply.body)
else
resource # just send back the submitted resource
end
Expand Down
6 changes: 1 addition & 5 deletions lib/fhir_client/sections/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ def history(options)
reply = get resource_url(options), fhir_headers

# The history reply should be a bundle
bundle_klass = if @fhir_version == :stu3
FHIR::STU3::Bundle
else
FHIR::DSTU2::Bundle
end
bundle_klass = self.versioned_resource_class(:Bundle)

reply.resource = parse_reply(bundle_klass, options[:format], reply)
reply.resource_class = options[:resource]
Expand Down
6 changes: 3 additions & 3 deletions lib/fhir_client/sections/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def value_set_code_validation(params = {}, format = @default_format)

# Concept Look Up [base]/CodeSystem/$lookup
def code_system_lookup(params = {}, format = @default_format)
klass = if @fhir_version == :stu3
FHIR::STU3::CodeSystem
else
klass = if @fhir_version == :dstu2
FHIR::DSTU2::ValueSet
else
self.versioned_resource_class(:CodeSystem)
end
options = { resource: klass, operation: { name: :code_system_lookup } }
options.deep_merge!(params)
Expand Down
14 changes: 14 additions & 0 deletions lib/fhir_client/version_management.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module FHIR
module VersionManagement

def versioned_resource_class(klass = nil)
return FHIR if klass.nil?
if @fhir_version == :stu3
FHIR::STU3.const_get(klass)
else
FHIR::DSTU2.const_get(klass)
end
end

end
end

0 comments on commit 1021e89

Please sign in to comment.