Module: FHIR::Sections::Crud

Included in:
Client
Defined in:
lib/fhir_client/sections/crud.rb

Instance Method Summary collapse

Instance Method Details

#base_create(resource, options, format = nil, additional_header = {}) ⇒ Object

Create a new resource with a server assigned id. Return the newly created resource with the id the server assigned.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/fhir_client/sections/crud.rb', line 197

def base_create(resource, options, format = nil, additional_header = {})
  headers = {}
  headers[:accept] = "#{format}" if format
  format ||= @default_format
  headers = {content_type: "#{format}"}
  headers[:prefer] = @return_preference if @use_return_preference
  headers.merge!(additional_header)
  options = {} if options.nil?
  options[:resource] = resource.class
  options[:format] = format || @default_format
  reply = post resource_url(options), resource, fhir_headers(headers)
  if [200, 201].include? reply.code
    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?
                         klass = self.versioned_resource_class(:Xml)
                         klass.from_xml(reply.body)
                       elsif type.include?('json') && !reply.body.empty?
                         klass = self.versioned_resource_class(:Json)
                         klass.from_json(reply.body)
                       else
                         resource # just send back the submitted resource
                       end
      resource.id = FHIR::ResourceAddress.pull_out_id(resource.class.name.demodulize, reply.self_link)
    else
      resource.id = FHIR::ResourceAddress.pull_out_id(resource.class.name.demodulize, reply.self_link)
      reply.resource = resource # don't know the content type, so return the resource provided
    end
  else
    resource.id = FHIR::ResourceAddress.pull_out_id(resource.class.name.demodulize, reply.self_link)
    reply.resource = resource # just send back the submitted resource
  end
  set_client_on_resource(reply.resource)
  reply.resource_class = resource.class
  reply
end

#base_update(resource, id, options, format = nil, headers = nil) ⇒ Object

Update an existing resource by its id or create it if it is a new resource, not present on the server



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/fhir_client/sections/crud.rb', line 124

def base_update(resource, id, options, format = nil, headers = nil)
  headers ||= {}
  headers[:accept] = "#{format}" if format
  format ||= @default_format
  headers[:content_type] =  "#{format}"
  headers[:prefer] = @return_preference if @use_return_preference
  options = {} if options.nil?
  options[:resource] = resource.class
  options[:format] = format
  options[:id] = id
  reply = put resource_url(options), resource, fhir_headers(headers)
  reply.resource = parse_reply(resource.class, format, reply) if reply.body.present?
  reply.resource_class = resource.class
  reply
end

#conditional_create(resource, if_none_exist_parameters, format = nil) ⇒ Object

Conditionally create a new resource with a server assigned id.



183
184
185
186
187
188
189
190
191
# File 'lib/fhir_client/sections/crud.rb', line 183

def conditional_create(resource, if_none_exist_parameters, format = nil)
  query = ''
  if_none_exist_parameters.each do |key, value|
    query += "#{key}=#{value}&"
  end
  query = query[0..-2] # strip off the trailing ampersand
  header = {if_none_exist: query}
  base_create(resource, nil, format, header)
end

#conditional_read_since(klass, id, since_date, options = {}) ⇒ Object

Conditionally Read the resource if it has been modified since the supplied date

See If-Modified-Since RRC 7232 tools.ietf.org/html/rfc7232#section-3.3 and See HTTP-date tools.ietf.org/html/rfc7231#section-7.1.1.1

Parameters:

  • klass

    the FHIR Resource class

  • id

    the resource id

  • since_date

    the date



26
27
28
29
30
31
32
33
34
# File 'lib/fhir_client/sections/crud.rb', line 26

def conditional_read_since(klass, id, since_date, options = {})

  options = { resource: klass, id: id}.merge(options)
  headers = { if_modified_since: since_date }
  reply = get resource_url(options), fhir_headers(headers)
  reply.resource = parse_reply(klass, @default_format, reply)
  reply.resource_class = klass
  reply
end

#conditional_read_version(klass, id, version_id, options = {}) ⇒ Object

Conditionally Read the resource based on the provided ETag

Parameters:

  • klass

    the FHIR Resource class

  • id

    the resource id

  • version_id

    the version_id used for the ETag



42
43
44
45
46
47
48
49
50
# File 'lib/fhir_client/sections/crud.rb', line 42

def conditional_read_version(klass, id, version_id, options = {})

  options = { resource: klass, id: id}.merge(options)
  headers = { if_none_match: "W/#{version_id}" }
  reply = get resource_url(options), fhir_headers(headers)
  reply.resource = parse_reply(klass, @default_format, reply)
  reply.resource_class = klass
  reply
end

#conditional_update(resource, id, search_params, format = nil) ⇒ Object

Update an existing resource by its id or create it if it is a new resource, not present on the server



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fhir_client/sections/crud.rb', line 98

def conditional_update(resource, id, search_params, format = nil)
  options = {
    search: {
      flag: false,
      compartment: nil,
      parameters: {}
    }
  }
  search_params.each do |key, value|
    options[:search][:parameters][key] = value
  end
  base_update(resource, id, options, format)
end

#create(resource, options = {}, format = nil) ⇒ Object

Create a new resource with a server assigned id. Return the newly created resource with the id the server assigned.



176
177
178
# File 'lib/fhir_client/sections/crud.rb', line 176

def create(resource, options = {}, format = nil)
  base_create(resource, options, format)
end

#destroy(klass, id = nil, options = {}) ⇒ Object

Delete the resource with the given ID.



165
166
167
168
169
170
# File 'lib/fhir_client/sections/crud.rb', line 165

def destroy(klass, id = nil, options = {})
  options = { resource: klass, id: id, format: @default_format }.merge options
  reply = delete resource_url(options), fhir_headers
  reply.resource_class = klass
  reply
end

#partial_update(klass, id, patchset, options = {}, format = nil) ⇒ Object

Partial update using a patchset (PATCH)



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/fhir_client/sections/crud.rb', line 143

def partial_update(klass, id, patchset, options = {}, format = nil)
  headers = {}
  headers[:accept] =  "#{format}" if format
  format ||= @default_format
  options = { resource: klass, id: id, format: format}.merge options
  if [FHIR::Formats::ResourceFormat::RESOURCE_XML, FHIR::Formats::ResourceFormat::RESOURCE_XML_DSTU2].include?(format)
    options[:format] = FHIR::Formats::PatchFormat::PATCH_XML
    headers[:content_type] =  "#{FHIR::Formats::PatchFormat::PATCH_XML}"
  elsif [FHIR::Formats::ResourceFormat::RESOURCE_JSON, FHIR::Formats::ResourceFormat::RESOURCE_JSON_DSTU2].include?(format)
    options[:format] = FHIR::Formats::PatchFormat::PATCH_JSON
    headers[:content_type] =  "#{FHIR::Formats::PatchFormat::PATCH_JSON}"
  end
  headers[:prefer] = @return_preference if @use_return_preference
  reply = patch resource_url(options), patchset, fhir_headers(headers)
  reply.resource = parse_reply(klass, format, reply)
  reply.resource_class = klass
  reply
end

#raw_read(options) ⇒ Object



79
80
81
# File 'lib/fhir_client/sections/crud.rb', line 79

def raw_read(options)
  get resource_url(options), fhir_headers
end

#raw_read_url(url, format = nil) ⇒ Object



83
84
85
86
# File 'lib/fhir_client/sections/crud.rb', line 83

def raw_read_url(url, format = nil)
  headers = { accept: "#{format}" } if format
  get url, fhir_headers(headers)
end

#read(klass, id, format = nil, summary = nil, options = {}) ⇒ Object

Read the current state of a resource.



7
8
9
10
11
12
13
14
15
# File 'lib/fhir_client/sections/crud.rb', line 7

def read(klass, id, format = nil, summary = nil, options = {})
  options = { resource: klass, id: id, format: format || @default_format}.merge(options)
  options[:summary] = summary if summary
  headers = { accept: "#{format}" } if format
  reply = get resource_url(options), fhir_headers(headers)
  reply.resource = parse_reply(klass, format || @default_format, reply)
  reply.resource_class = klass
  reply
end

#read_feed(klass, format = nil) ⇒ Object

Read a resource bundle (an XML ATOM feed)



56
57
58
59
60
61
62
63
64
# File 'lib/fhir_client/sections/crud.rb', line 56

def read_feed(klass, format = nil)
  headers = { accept: "#{format}" } if format
  format ||= @default_format
  options = { resource: klass, format: format}
  reply = get resource_url(options), fhir_headers(headers)
  reply.resource = parse_reply(klass, format, reply)
  reply.resource_class = klass
  reply
end

#update(resource, id, format = nil) ⇒ Object

Update an existing resource by its id or create it if it is a new resource, not present on the server



91
92
93
# File 'lib/fhir_client/sections/crud.rb', line 91

def update(resource, id, format = nil)
  base_update(resource, id, nil, format)
end

#version_aware_update(resource, id, version_id, format = nil, options = {}) ⇒ Object

Version Aware Update using version_id prevents Lost Update Problem www.w3.org/1999/04/Editing/

Parameters:

  • resource

    the FHIR resource object

  • id

    the resource id



117
118
119
# File 'lib/fhir_client/sections/crud.rb', line 117

def version_aware_update(resource, id, version_id, format = nil, options = {})
  base_update(resource, id, options, format, {if_match: "W/#{version_id}"})
end

#vread(klass, id, version_id, format = nil) ⇒ Object

Read the state of a specific version of the resource



69
70
71
72
73
74
75
76
77
# File 'lib/fhir_client/sections/crud.rb', line 69

def vread(klass, id, version_id, format = nil)
  headers = { accept: "#{format}" } if format
  format ||= @default_format
  options = { resource: klass, id: id, format: format, history: { id: version_id } }
  reply = get resource_url(options), fhir_headers(headers)
  reply.resource = parse_reply(klass, format, reply)
  reply.resource_class = klass
  reply
end