Module: FHIR::Sections::Transactions

Included in:
Client
Defined in:
lib/fhir_client/sections/transactions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#transaction_bundleObject

Returns the value of attribute transaction_bundle.



4
5
6
# File 'lib/fhir_client/sections/transactions.rb', line 4

def transaction_bundle
  @transaction_bundle
end

Instance Method Details

#add_batch_request(method, url, resource = nil, if_none_exist = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fhir_client/sections/transactions.rb', line 28

def add_batch_request(method, url, resource = nil, if_none_exist = nil)
  request = versioned_resource_class('Bundle::Entry::Request').new
  request.local_method = if versioned_resource_class('Bundle::Entry::Request::METADATA')['method']['valid_codes'].values.first.include?(method.upcase)
                           method.upcase
                         else
                           'POST'
                         end
  request.ifNoneExist = if_none_exist unless if_none_exist.nil?
  if url.nil? && !resource.nil?
    options = {}
    options[:resource] = resource.class
    options[:id] = resource.id if request.local_method != 'POST'
    request.url = resource_url(options)
    request.url = request.url[1..-1] if request.url.starts_with?('/')
  else
    request.url = url
  end

  entry = versioned_resource_class('Bundle::Entry').new
  entry.resource = resource
  entry.request = request

  @transaction_bundle.entry << entry
  entry
end

#add_transaction_request(method, url, resource = nil, if_none_exist = nil) ⇒ Object

syntactic sugar for add_batch_request

Parameters:

  • method

    one of [‘GET’,‘POST’,‘PUT’,‘DELETE’]

  • url

    relative path, such as ‘Patient/123’. Do not include the [base]

  • resource (defaults to: nil)

    The resource if a POST or PUT



24
25
26
# File 'lib/fhir_client/sections/transactions.rb', line 24

def add_transaction_request(method, url, resource = nil, if_none_exist = nil)
  add_batch_request(method, url, resource, if_none_exist)
end

#begin_batchObject



13
14
15
16
17
18
# File 'lib/fhir_client/sections/transactions.rb', line 13

def begin_batch
  @transaction_bundle = versioned_resource_class('Bundle').new
  @transaction_bundle.type = 'batch'
  @transaction_bundle.entry ||= []
  @transaction_bundle
end

#begin_transactionObject



6
7
8
9
10
11
# File 'lib/fhir_client/sections/transactions.rb', line 6

def begin_transaction
  @transaction_bundle = versioned_resource_class('Bundle').new
  @transaction_bundle.type = 'transaction'
  @transaction_bundle.entry ||= []
  @transaction_bundle
end

#end_batch(format = @default_format) ⇒ Object

submit the batch/transaction to the server

Parameters:

  • format (defaults to: @default_format)

Returns:

  • FHIR::ClientReply



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fhir_client/sections/transactions.rb', line 63

def end_batch(format = @default_format)
  headers = {prefer: FHIR::Formats::ReturnPreferences::REPRESENTATION}
  headers[:content_type] =  "#{format}"
  options = { format: format}
  reply = post resource_url(options), @transaction_bundle, fhir_headers(headers)
  begin
    reply.resource = if format.downcase.include?('xml')
                       versioned_resource_class('Xml').from_xml(reply.body)
                     else
                       versioned_resource_class('Json').from_json(reply.body)
                     end
  rescue
    reply.resource = nil
  end
  set_client_on_resource(reply.resource)
  reply.resource_class = reply.resource.class
  reply
end

#end_transaction(format = @default_format) ⇒ Object

syntactic sugar for end_batch



55
56
57
# File 'lib/fhir_client/sections/transactions.rb', line 55

def end_transaction(format = @default_format)
  end_batch(format)
end