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
# 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][:content_type]
    if !type.nil?
      reply.resource = if type.include?('xml') && !reply.body.empty?
                         resource.class.from_xml(reply.body)
                       elsif type.include?('json') && !reply.body.empty?
                         resource.class.from_fhir_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
  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



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

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



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

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, 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, format = @default_format)
  base_create(resource, nil, format)
end

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

Delete the resource with the given ID.



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

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

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

Partial update using a patchset (PATCH)



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

def partial_update(klass, id, patchset, options = {}, format = @default_format)
  options = { resource: klass, id: id, format: format }.merge options

  if format == FHIR::Formats::ResourceFormat::RESOURCE_XML
    options[:format] = FHIR::Formats::PatchFormat::PATCH_XML
    options[:Accept] = format
  elsif format == FHIR::Formats::ResourceFormat::RESOURCE_JSON
    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
41
# File 'lib/fhir_client/sections/crud.rb', line 38

def raw_read(options)
  reply = get resource_url(options), fhir_headers(options)
  reply.body
end

#raw_read_url(url) ⇒ Object



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

def raw_read_url(url)
  reply = get url, fhir_headers({})
  reply.body
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



51
52
53
# File 'lib/fhir_client/sections/crud.rb', line 51

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