Module: FHIR::Sections::Crud
- Included in:
- Client
- Defined in:
- lib/fhir_client/sections/crud.rb
Instance Method Summary collapse
-
#base_create(resource, options, format) ⇒ Object
Create a new resource with a server assigned id.
-
#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.
-
#conditional_create(resource, if_none_exist_parameters, format = @default_format) ⇒ Object
Conditionally create a new resource with a server assigned id.
-
#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.
-
#create(resource, format = @default_format) ⇒ Object
Create a new resource with a server assigned id.
-
#destroy(klass, id = nil, options = {}) ⇒ Object
Delete the resource with the given ID.
-
#partial_update(klass, id, patchset, options = {}, format = @default_format) ⇒ Object
Partial update using a patchset (PATCH).
- #raw_read(options) ⇒ Object
- #raw_read_url(url) ⇒ Object
-
#read(klass, id, format = @default_format, summary = nil, options = {}) ⇒ Object
Read the current state of a resource.
-
#read_feed(klass, format = @default_format) ⇒ Object
Read a resource bundle (an XML ATOM feed).
-
#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.
-
#vread(klass, id, version_id, format = @default_format) ⇒ Object
Read the state of a specific version of the resource.
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, , format) = {} if .nil? [:resource] = resource.class [:format] = format reply = post resource_url(), resource, fhir_headers() 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, , format) = {} if .nil? [:resource] = resource.class [:format] = format [:id] = id reply = put resource_url(), resource, fhir_headers() 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 = {} ['If-None-Exist'] = query base_create(resource, , 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) = { search: { flag: false, compartment: nil, parameters: {} } } search_params.each do |key, value| [:search][:parameters][key] = value end base_update(resource, id, , 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, = {}) = { resource: klass, id: id, format: nil }.merge reply = delete resource_url(), fhir_headers() 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, = {}, format = @default_format) = { resource: klass, id: id, format: format }.merge if format == FHIR::Formats::ResourceFormat::RESOURCE_XML [:format] = FHIR::Formats::PatchFormat::PATCH_XML [:Accept] = format elsif format == FHIR::Formats::ResourceFormat::RESOURCE_JSON [:format] = FHIR::Formats::PatchFormat::PATCH_JSON [:Accept] = format end reply = patch resource_url(), patchset, fhir_headers() 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() reply = get resource_url(), fhir_headers() 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, = {}) = { resource: klass, id: id, format: format }.merge() [:summary] = summary if summary reply = get resource_url(), fhir_headers() 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) = { resource: klass, format: format } reply = get resource_url(), fhir_headers() 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) = { resource: klass, id: id, format: format, history: { id: version_id } } reply = get resource_url(), fhir_headers() reply.resource = parse_reply(klass, format, reply) reply.resource_class = klass reply end |