Class: Trophonius::Record
- Inherits:
-
Hash
- Object
- Hash
- Trophonius::Record
- 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
-
#layout_name ⇒ Object
Returns the value of attribute layout_name.
-
#modifiable_fields ⇒ Object
Returns the value of attribute modifiable_fields.
-
#modified_fields ⇒ Object
Returns the value of attribute modified_fields.
-
#record_id ⇒ Object
Returns the value of attribute record_id.
Instance Method Summary collapse
- #[]=(field, new_val) ⇒ Object
-
#delete ⇒ True
Deletes the corresponding record from FileMaker Throws a FileMaker error if save failed.
-
#initialize ⇒ Record
constructor
Initializes a new Record.
-
#save ⇒ True
Saves the last changes made to the Record to FileMaker.
-
#update(fieldData) ⇒ True
Changes and saves the corresponding record in FileMaker Throws a FileMaker error if save failed.
-
#upload(container_name:, container_repetition: 1, file:) ⇒ True
Uploads a file to a container field of the record Throws a FileMaker error if upload failed.
Constructor Details
#initialize ⇒ Record
Initializes a new Record
15 16 17 18 |
# File 'lib/trophonius_record.rb', line 15 def initialize @modifiable_fields = {} @modified_fields = {} end |
Instance Attribute Details
#layout_name ⇒ Object
Returns the value of attribute layout_name.
11 12 13 |
# File 'lib/trophonius_record.rb', line 11 def layout_name @layout_name end |
#modifiable_fields ⇒ Object
Returns the value of attribute modifiable_fields.
11 12 13 |
# File 'lib/trophonius_record.rb', line 11 def modifiable_fields @modifiable_fields end |
#modified_fields ⇒ Object
Returns the value of attribute modified_fields.
11 12 13 |
# File 'lib/trophonius_record.rb', line 11 def modified_fields @modified_fields end |
#record_id ⇒ Object
Returns the value of attribute record_id.
11 12 13 |
# File 'lib/trophonius_record.rb', line 11 def record_id @record_id end |
Instance Method Details
#[]=(field, new_val) ⇒ Object
20 21 22 23 |
# File 'lib/trophonius_record.rb', line 20 def []=(field, new_val) modifiable_fields[field] = new_val super end |
#delete ⇒ True
Deletes the corresponding record from FileMaker Throws a FileMaker error if save failed
46 47 48 49 50 51 52 53 54 |
# File 'lib/trophonius_record.rb', line 46 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 true end end |
#save ⇒ True
Saves the last changes made to the Record to FileMaker. Throws a FileMaker error if save failed
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/trophonius_record.rb', line 30 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 true end end |
#update(fieldData) ⇒ True
Changes and saves the corresponding record in FileMaker Throws a FileMaker error if save failed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/trophonius_record.rb', line 63 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' if response["messages"][0]["code"] == "102" results = Request.retrieve_first(layout_name) if results["messages"][0]["code"] != "0" Error.throw_error("102") else r_results = results["response"]["data"] ret_val = r_results.empty? ? Error.throw_error("102") : r_results[0]['fieldData'] Error.throw_error("102", (fieldData.keys.map(&:downcase) - ret_val.keys.map(&:downcase)).flatten.join(', '), layout_name) end end Error.throw_error(response["messages"][0]["code"]) else true end end |
#upload(container_name:, container_repetition: 1, file:) ⇒ True
Uploads a file to a container field of the record Throws a FileMaker error if upload failed
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/trophonius_record.rb', line 96 def upload(container_name:, container_repetition: 1, file:) url = URI("http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{layout_name}/records/#{record_id}/containers/#{container_name}/#{container_repetition}") response = Request.upload_file_request(url, "Bearer #{Request.get_token}", file) if response['messages'][0]['code'] != '0' Error.throw_error(response['messages'][0]['code']) else true end end |