Module: FHIR::Sections::Crud

Included in:
Client
Defined in:
lib/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.



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/sections/crud.rb', line 143

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?
      if type.include?('xml') && !reply.body.empty?
        reply.resource = resource.class.from_xml(reply.body)
      elsif type.include?('json') && !reply.body.empty?
        reply.resource = resource.class.from_fhir_json(reply.body)
      else
        reply.resource = resource # just send back the submitted resource
      end
    else
      resource.id = FHIR::ResourceAddress.pull_out_id(resource.class.name.demodulize, reply.response[:headers]['location'])
      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.response[:headers]['location'])
    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



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

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, ifNoneExistParameters, format = @default_format) ⇒ Object

Conditionally create a new resource with a server assigned id.



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

def conditional_create(resource, ifNoneExistParameters, format=@default_format)
  query = ''
  ifNoneExistParameters.each do |key,value|
    query += "#{key.to_s}=#{value.to_s}&"
  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, searchParams, 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



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

def conditional_update(resource, id, searchParams, format=@default_format)
  options = {
    :search => {
      :flag => false,
      :compartment => nil,
      :parameters => {}
    }
  }
  searchParams.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.



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

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.



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

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)



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

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



39
40
41
42
# File 'lib/sections/crud.rb', line 39

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

#raw_read_url(url) ⇒ Object



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

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.



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

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

Read a resource bundle (an XML ATOM feed)



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

def read_feed(klass, format=@default_format_bundle)
  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



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

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



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

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