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) ⇒ Object

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/fhir_client/sections/crud.rb', line 142

def base_create(resource, options, format)
  options = {} if options.nil?
  options[:resource] = resource.class
  options[:format] = format
  reply = post resource_url(options), resource, fhir_headers(options)
  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?
                         if @fhir_version == :stu3
                           FHIR::Xml.from_xml(reply.body)
                         else
                           FHIR::DSTU2::Xml.from_xml(reply.body)
                         end
                       elsif type.include?('json') && !reply.body.empty?
                         if @fhir_version == :stu3
                           FHIR::Json.from_json(reply.body)
                         else
                           FHIR::DSTU2::Json.from_json(reply.body)
                         end
                       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
  reply.resource.client = self
  reply.resource_class = resource.class
  reply
end

#base_update(resource, id, options, format) ⇒ Object

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



73
74
75
76
77
78
79
80
81
82
# File 'lib/fhir_client/sections/crud.rb', line 73

def base_update(resource, id, options, format)
  options = {} if options.nil?
  options[:resource] = resource.class
  options[:format] = format
  options[:id] = id
  reply = put resource_url(options), resource, fhir_headers(options)
  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 = @default_format) ⇒ Object

Conditionally create a new resource with a server assigned id.



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

def conditional_create(resource, if_none_exist_parameters, format = @default_format)
  query = ''
  if_none_exist_parameters.each do |key, value|
    query += "#{key}=#{value}&"
  end
  query = query[0..-2] # strip off the trailing ampersand
  options = {}
  options['If-None-Exist'] = query
  base_create(resource, options, format)
end

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

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



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

def conditional_update(resource, id, search_params, format = @default_format)
  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 = @default_format) ⇒ Object

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



120
121
122
# File 'lib/fhir_client/sections/crud.rb', line 120

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

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

Delete the resource with the given ID.



107
108
109
110
111
112
113
114
# File 'lib/fhir_client/sections/crud.rb', line 107

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

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

Partial update using a patchset (PATCH)



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fhir_client/sections/crud.rb', line 87

def partial_update(klass, id, patchset, options = {}, 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
    options[:Accept] = format
  elsif [FHIR::Formats::ResourceFormat::RESOURCE_JSON, FHIR::Formats::ResourceFormat::RESOURCE_JSON_DSTU2].include?(format)
    options[:format] = FHIR::Formats::PatchFormat::PATCH_JSON
    options[:Accept] = format
  end

  reply = patch resource_url(options), patchset, fhir_headers(options)
  reply.resource = parse_reply(klass, format, reply)
  reply.resource_class = klass
  reply
end

#raw_read(options) ⇒ Object



38
39
40
# File 'lib/fhir_client/sections/crud.rb', line 38

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

#raw_read_url(url, format = @default_format) ⇒ Object



42
43
44
# File 'lib/fhir_client/sections/crud.rb', line 42

def raw_read_url(url, format = @default_format)
  get url, fhir_headers(format: format)
end

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

Read the current state of a resource.



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

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

#read_feed(klass, format = @default_format) ⇒ Object

Read a resource bundle (an XML ATOM feed)



19
20
21
22
23
24
25
# File 'lib/fhir_client/sections/crud.rb', line 19

def read_feed(klass, format = @default_format)
  options = { resource: klass, format: format }
  reply = get resource_url(options), fhir_headers(options)
  reply.resource = parse_reply(klass, format, reply)
  reply.resource_class = klass
  reply
end

#update(resource, id, format = @default_format) ⇒ Object

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



49
50
51
# File 'lib/fhir_client/sections/crud.rb', line 49

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

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

Read the state of a specific version of the resource



30
31
32
33
34
35
36
# File 'lib/fhir_client/sections/crud.rb', line 30

def vread(klass, id, version_id, format = @default_format)
  options = { resource: klass, id: id, format: format, history: { id: version_id } }
  reply = get resource_url(options), fhir_headers(options)
  reply.resource = parse_reply(klass, format, reply)
  reply.resource_class = klass
  reply
end