Class: Attio::Note
- Inherits:
-
APIResource
- Object
- APIResource
- Attio::Note
- Defined in:
- lib/attio/resources/note.rb
Overview
Represents a note attached to a record in Attio
Constant Summary
Constants inherited from APIResource
Instance Attribute Summary collapse
-
#content_markdown ⇒ Object
readonly
Read-only attributes - notes are immutable.
-
#content_plaintext ⇒ Object
readonly
Read-only attributes - notes are immutable.
-
#created_by_actor ⇒ Object
(also: #created_by)
readonly
Read-only attributes - notes are immutable.
-
#format ⇒ Object
readonly
Read-only attributes - notes are immutable.
-
#metadata ⇒ Object
readonly
Read-only attributes - notes are immutable.
-
#parent_object ⇒ Object
readonly
Read-only attributes - notes are immutable.
-
#parent_record_id ⇒ Object
readonly
Read-only attributes - notes are immutable.
-
#tags ⇒ Object
readonly
Read-only attributes - notes are immutable.
-
#title ⇒ Object
readonly
Read-only attributes - notes are immutable.
Attributes inherited from APIResource
Class Method Summary collapse
-
.create(**kwargs) ⇒ Object
Override create to handle validation and parameter mapping.
-
.for_record(params = {}, object:, record_id:) ⇒ Object
Get notes for a record.
-
.prepare_params_for_create(params) ⇒ Object
Override create to handle validation.
-
.resource_path ⇒ String
API endpoint path for notes.
-
.retrieve(id, **opts) ⇒ Object
Override retrieve to handle nested ID.
Instance Method Summary collapse
-
#content ⇒ Object
Convenience method to get content based on format.
-
#destroy(**opts) ⇒ Object
Override destroy to handle nested ID.
-
#html? ⇒ Boolean
Check if note is in HTML format.
-
#initialize(attributes = {}, opts = {}) ⇒ Note
constructor
A new instance of Note.
-
#parent_record ⇒ Object
Get the parent record.
-
#plaintext? ⇒ Boolean
Check if note is in plaintext format.
- #resource_path ⇒ Object
-
#save ⇒ Object
Notes cannot be updated.
-
#to_h ⇒ Hash
Convert note to hash representation.
-
#to_plaintext ⇒ Object
Get plaintext version of content.
- #update ⇒ Object
Methods inherited from APIResource
#==, #[], #[]=, api_operations, attr_attio, #changed, #changed?, #changed_attributes, #changes, #each, execute_request, #fetch, #hash, id_param_name, #inspect, #key?, #keys, #persisted?, prepare_params_for_update, #reset_changes!, resource_name, #revert!, #to_json, #update_attributes, #update_from, validate_id!, #values
Constructor Details
#initialize(attributes = {}, opts = {}) ⇒ Note
Returns a new instance of Note.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/attio/resources/note.rb', line 35 def initialize(attributes = {}, opts = {}) super normalized_attrs = normalize_attributes(attributes) @parent_object = normalized_attrs[:parent_object] @parent_record_id = normalized_attrs[:parent_record_id] @title = normalized_attrs[:title] @content_plaintext = normalized_attrs[:content_plaintext] @content_markdown = normalized_attrs[:content_markdown] @tags = normalized_attrs[:tags] || [] @metadata = normalized_attrs[:metadata] || {} @format = normalized_attrs[:format] || "plaintext" @created_by_actor = normalized_attrs[:created_by_actor] end |
Instance Attribute Details
#content_markdown ⇒ Object (readonly)
Read-only attributes - notes are immutable
17 18 19 |
# File 'lib/attio/resources/note.rb', line 17 def content_markdown @content_markdown end |
#content_plaintext ⇒ Object (readonly)
Read-only attributes - notes are immutable
17 18 19 |
# File 'lib/attio/resources/note.rb', line 17 def content_plaintext @content_plaintext end |
#created_by_actor ⇒ Object (readonly) Also known as: created_by
Read-only attributes - notes are immutable
17 18 19 |
# File 'lib/attio/resources/note.rb', line 17 def created_by_actor @created_by_actor end |
#format ⇒ Object (readonly)
Read-only attributes - notes are immutable
17 18 19 |
# File 'lib/attio/resources/note.rb', line 17 def format @format end |
#metadata ⇒ Object (readonly)
Read-only attributes - notes are immutable
17 18 19 |
# File 'lib/attio/resources/note.rb', line 17 def @metadata end |
#parent_object ⇒ Object (readonly)
Read-only attributes - notes are immutable
17 18 19 |
# File 'lib/attio/resources/note.rb', line 17 def parent_object @parent_object end |
#parent_record_id ⇒ Object (readonly)
Read-only attributes - notes are immutable
17 18 19 |
# File 'lib/attio/resources/note.rb', line 17 def parent_record_id @parent_record_id end |
#tags ⇒ Object (readonly)
Read-only attributes - notes are immutable
17 18 19 |
# File 'lib/attio/resources/note.rb', line 17 def @tags end |
#title ⇒ Object (readonly)
Read-only attributes - notes are immutable
17 18 19 |
# File 'lib/attio/resources/note.rb', line 17 def title @title end |
Class Method Details
.create(**kwargs) ⇒ Object
Override create to handle validation and parameter mapping
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/attio/resources/note.rb', line 129 def create(**kwargs) # Extract options from kwargs opts = {} opts[:api_key] = kwargs.delete(:api_key) if kwargs.key?(:api_key) # Map object/record_id to parent_object/parent_record_id normalized_params = { parent_object: kwargs[:object] || kwargs[:parent_object], parent_record_id: kwargs[:record_id] || kwargs[:parent_record_id], title: kwargs[:title] || kwargs[:content] || "Note", content: kwargs[:content], format: kwargs[:format] } prepared_params = prepare_params_for_create(normalized_params) response = execute_request(:POST, resource_path, prepared_params, opts) new(response["data"] || response, opts) end |
.for_record(params = {}, object:, record_id:) ⇒ Object
Get notes for a record
166 167 168 169 170 171 172 173 174 |
# File 'lib/attio/resources/note.rb', line 166 def for_record(params = {}, object:, record_id:, **) list( params.merge( parent_object: object, parent_record_id: record_id ), ** ) end |
.prepare_params_for_create(params) ⇒ Object
Override create to handle validation
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/attio/resources/note.rb', line 149 def prepare_params_for_create(params) validate_parent!(params[:parent_object], params[:parent_record_id]) validate_content!(params[:content]) validate_format!(params[:format]) if params[:format] { data: { title: params[:title], parent_object: params[:parent_object], parent_record_id: params[:parent_record_id], content: params[:content], format: params[:format] || "plaintext" } } end |
.resource_path ⇒ String
API endpoint path for notes
12 13 14 |
# File 'lib/attio/resources/note.rb', line 12 def self.resource_path "notes" end |
.retrieve(id, **opts) ⇒ Object
Override retrieve to handle nested ID
121 122 123 124 125 126 |
# File 'lib/attio/resources/note.rb', line 121 def retrieve(id, **opts) note_id = id.is_a?(Hash) ? (id[:note_id] || id["note_id"]) : id validate_id!(note_id) response = execute_request(:GET, "#{resource_path}/#{note_id}", {}, opts) new(response["data"] || response, opts) end |
Instance Method Details
#content ⇒ Object
Convenience method to get content based on format
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/attio/resources/note.rb', line 24 def content case format when "plaintext" content_plaintext when "html", "markdown" content_markdown else content_plaintext end end |
#destroy(**opts) ⇒ Object
Override destroy to handle nested ID
88 89 90 91 92 93 94 95 |
# File 'lib/attio/resources/note.rb', line 88 def destroy(**opts) raise InvalidRequestError, "Cannot destroy a note without an ID" unless persisted? note_id = id.is_a?(Hash) ? (id[:note_id] || id["note_id"]) : id self.class.delete(note_id, **opts) freeze true end |
#html? ⇒ Boolean
Check if note is in HTML format
61 62 63 |
# File 'lib/attio/resources/note.rb', line 61 def html? format == "html" end |
#parent_record ⇒ Object
Get the parent record
50 51 52 53 54 55 56 57 58 |
# File 'lib/attio/resources/note.rb', line 50 def parent_record(**) return nil unless parent_object && parent_record_id Internal::Record.retrieve( object: parent_object, record_id: parent_record_id, ** ) end |
#plaintext? ⇒ Boolean
Check if note is in plaintext format
66 67 68 |
# File 'lib/attio/resources/note.rb', line 66 def plaintext? format == "plaintext" end |
#resource_path ⇒ Object
81 82 83 84 85 |
# File 'lib/attio/resources/note.rb', line 81 def resource_path raise InvalidRequestError, "Cannot generate path without an ID" unless persisted? note_id = id.is_a?(Hash) ? (id[:note_id] || id["note_id"]) : id "#{self.class.resource_path}/#{note_id}" end |
#save ⇒ Object
Notes cannot be updated
98 99 100 |
# File 'lib/attio/resources/note.rb', line 98 def save(*) raise NotImplementedError, "Notes cannot be updated. Create a new note instead." end |
#to_h ⇒ Hash
Convert note to hash representation
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/attio/resources/note.rb', line 108 def to_h super.merge( parent_object: parent_object, parent_record_id: parent_record_id, content: content, format: format, created_by_actor: created_by_actor, content_plaintext: content_plaintext ).compact end |
#to_plaintext ⇒ Object
Get plaintext version of content
71 72 73 74 75 76 77 78 79 |
# File 'lib/attio/resources/note.rb', line 71 def to_plaintext return content_plaintext if content_plaintext # If no plaintext, try to get markdown/html content and strip HTML html_content = content_markdown || content return nil unless html_content strip_html(html_content) end |
#update ⇒ Object
102 103 104 |
# File 'lib/attio/resources/note.rb', line 102 def update(*) raise NotImplementedError, "Notes cannot be updated. Create a new note instead." end |