Class: Trophonius::Record

Inherits:
Hash
  • Object
show all
Defined in:
lib/trophonius_record.rb

Overview

This class will hold a singular record

A Record is contained in a RecordSet and has methods to retrieve data from the fields inside the Record-hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRecord

Initializes a new Record



13
14
15
16
# File 'lib/trophonius_record.rb', line 13

def initialize
  @modifiable_fields = {}
  @modified_fields = {}
end

Instance Attribute Details

#layout_nameObject

Returns the value of attribute layout_name.



9
10
11
# File 'lib/trophonius_record.rb', line 9

def layout_name
  @layout_name
end

#modifiable_fieldsObject

Returns the value of attribute modifiable_fields.



9
10
11
# File 'lib/trophonius_record.rb', line 9

def modifiable_fields
  @modifiable_fields
end

#modified_fieldsObject

Returns the value of attribute modified_fields.



9
10
11
# File 'lib/trophonius_record.rb', line 9

def modified_fields
  @modified_fields
end

#record_idObject

Returns the value of attribute record_id.



9
10
11
# File 'lib/trophonius_record.rb', line 9

def record_id
  @record_id
end

Instance Method Details

#[]=(field, new_val) ⇒ Object



18
19
20
21
# File 'lib/trophonius_record.rb', line 18

def []=(field, new_val)
  modifiable_fields[field] = new_val
  super
end

#deleteTrue

Deletes the corresponding record from FileMaker Throws a FileMaker error if save failed

Returns:

  • (True)

    if successful



44
45
46
47
48
49
50
51
52
# File 'lib/trophonius_record.rb', line 44

def delete
  url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records/#{record_id}")
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'delete', '{}')
  if response['messages'][0]['code'] != '0'
    Error.throw_error(response['messages'][0]['code'])
  else
    return true
  end
end

#saveTrue

Saves the last changes made to the Record to FileMaker. Throws a FileMaker error if save failed

Returns:

  • (True)

    if successful



28
29
30
31
32
33
34
35
36
37
# File 'lib/trophonius_record.rb', line 28

def save
  url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records/#{record_id}")
  body = "{\"fieldData\": #{modified_fields.to_json}}"
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'patch', body)
  if response['messages'][0]['code'] != '0'
    Error.throw_error(response['messages'][0]['code'])
  else
    return true
  end
end

#update(fieldData) ⇒ True

Changes and saves the corresponding record in FileMaker Throws a FileMaker error if save failed

Parameters:

  • fieldData: (Hash)

    Fields to be changed and data to fill the fields with

Returns:

  • (True)

    if successful



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/trophonius_record.rb', line 61

def update(fieldData)
  url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records/#{record_id}")
  fieldData.keys.each do |field|
    modifiable_fields[field] = fieldData[field]
  end
  body = "{\"fieldData\": #{fieldData.to_json}}"
  response = Request.make_request(url, "Bearer #{Request.get_token}", 'patch', body)
  if response['messages'][0]['code'] != '0'
    Error.throw_error(response['messages'][0]['code'])
  else
    return true
  end
end