Class: Attio::Note

Inherits:
APIResource show all
Defined in:
lib/attio/resources/note.rb

Overview

Represents a note attached to a record in Attio

Constant Summary

Constants inherited from APIResource

APIResource::SKIP_KEYS

Instance Attribute Summary collapse

Attributes inherited from APIResource

#created_at, #id

Class Method Summary collapse

Instance Method Summary collapse

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_markdownObject (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_plaintextObject (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_actorObject (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

#formatObject (readonly)

Read-only attributes - notes are immutable



17
18
19
# File 'lib/attio/resources/note.rb', line 17

def format
  @format
end

#metadataObject (readonly)

Read-only attributes - notes are immutable



17
18
19
# File 'lib/attio/resources/note.rb', line 17

def 
  @metadata
end

#parent_objectObject (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_idObject (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

#tagsObject (readonly)

Read-only attributes - notes are immutable



17
18
19
# File 'lib/attio/resources/note.rb', line 17

def tags
  @tags
end

#titleObject (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_pathString

API endpoint path for notes

Returns:

  • (String)

    The API path



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

#contentObject

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

Returns:

  • (Boolean)


61
62
63
# File 'lib/attio/resources/note.rb', line 61

def html?
  format == "html"
end

#parent_recordObject

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

Returns:

  • (Boolean)


66
67
68
# File 'lib/attio/resources/note.rb', line 66

def plaintext?
  format == "plaintext"
end

#resource_pathObject



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

#saveObject

Notes cannot be updated

Raises:

  • (NotImplementedError)


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_hHash

Convert note to hash representation

Returns:

  • (Hash)

    Note data as a hash



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_plaintextObject

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

#updateObject

Raises:

  • (NotImplementedError)


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