Class: OData::CreateOperation

Inherits:
Operation show all
Defined in:
lib/odata/create_operation.rb

Direct Known Subclasses

UpdateOperation

Instance Attribute Summary

Attributes inherited from Operation

#ar

Instance Method Summary collapse

Methods inherited from Operation

#base_url, #check_response_errors, #entity_name, #initialize, #operation_headers, #operation_password, #operation_username, #run, #send_odata, #table_pluralize

Constructor Details

This class inherits a constructor from OData::Operation

Instance Method Details

#handle_operation_response(response) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/odata/create_operation.rb', line 4

def handle_operation_response(response)
  # Grab the id, and put back into the active record instance
  if response.headers['OData-EntityId']
    id = response.headers['OData-EntityId'].scan(/\(([\w-]*)\)/)
    @ar.id = id[0][0] unless id.nil? || id[0].nil?
    @ar.errors[:base] << "Failed to #{operation_callback_name} entity. [http code #{response.code}]" if @ar.id.nil?
  else
    @ar.errors[:base] << "Could not #{operation_callback_name} entity. [http code #{response.code}]" if @ar.id.nil?
  end
  check_response_errors(response)
end

#operation_bodyObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/odata/create_operation.rb', line 16

def operation_body
  body = {}
  # Add changed fields and values
  @ar.changes.each do |field, values|
    # If a belongs to field, add association the way OData wants it
    if @ar.class.belongs_to_field?(field)
      belongs_to_field = @ar.class.belongs_to_field(field)
      odata_table_ref = @ar.class.odata_table_reference || table_pluralize(belongs_to_field.table_name).downcase
      body["#{belongs_to_field.options[:crm_key]}@odata.bind"] = "/#{odata_table_ref}(#{values[1]})"
    else
      body[field.downcase] = values[1]
    end
  end
  body.to_json
end

#operation_callback_nameObject



40
41
42
# File 'lib/odata/create_operation.rb', line 40

def operation_callback_name
  :create
end

#operation_methodObject



32
33
34
# File 'lib/odata/create_operation.rb', line 32

def operation_method
  :post
end

#operation_urlObject



36
37
38
# File 'lib/odata/create_operation.rb', line 36

def operation_url
  "#{base_url}#{entity_name}"
end