Module: FHIR::Sections::Crud
- Included in:
- Client
- Defined in:
- lib/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, ifNoneExistParameters, format = @default_format) ⇒ Object
Conditionally create a new resource with a server assigned id.
-
#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.
-
#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_bundle) ⇒ 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.
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, , 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? 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, , 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, 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 = {} ['If-None-Exist'] = query base_create(resource, , 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) = { :search => { :flag => false, :compartment => nil, :parameters => {} } } searchParams.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.
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, ={}) = { 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)
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, ={}, 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
39 40 41 42 |
# File 'lib/sections/crud.rb', line 39 def raw_read() reply = get resource_url(), fhir_headers() 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, = {}) = { 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_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) = { 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
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) = { 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 |