Class: ZohoHub::Attachment

Inherits:
BaseRecord show all
Defined in:
lib/zoho_hub/records/attachment.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseRecord

all, attribute_translation, attributes, #attributes, build_response, #build_response, create, find_by, #get, #new_record?, request_path, where, zoho_key_translation

Constructor Details

#initialize(params) ⇒ Attachment

Returns a new instance of Attachment.



18
19
20
21
22
23
24
25
26
# File 'lib/zoho_hub/records/attachment.rb', line 18

def initialize(params)
  attributes.each do |attr|
    zoho_key = attr_to_zoho_key(attr)

    send("#{attr}=", params[zoho_key] || params[attr])
  end

  @parent_id = params.dig(:Parent_Id, :id)
end

Class Method Details

.exists?(id, parent:) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/zoho_hub/records/attachment.rb', line 29

def exists?(id, parent:)
  !find(id, parent: parent).nil?
rescue RecordNotFound
  false
end

.find(id, parent:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/zoho_hub/records/attachment.rb', line 35

def find(id, parent:)
  body = get(File.join(request_path, id.to_s), parent: parent)
  response = build_response(body)

  if response.empty?
    raise RecordNotFound, "Couldn't find #{request_path.singularize} with 'id'=#{id}"
  end

  new(response.data)
end

.get(path, parent:, **params) ⇒ Object



46
47
48
49
50
# File 'lib/zoho_hub/records/attachment.rb', line 46

def get(path, parent:, **params)
  # remove /search added by where method
  path = path.sub('/search', '')
  ZohoHub.connection.get(parent_module_path(path, parent), params)
end

.parent_module_path(path, parent) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/zoho_hub/records/attachment.rb', line 63

def parent_module_path(path, parent)
  File.join(
    parent.class.request_path,
    parent.id,
    path
  )
end

.post(path, parent:, **params) ⇒ Object



52
53
54
55
56
57
# File 'lib/zoho_hub/records/attachment.rb', line 52

def post(path, parent:, **params)
  ZohoHub.connection.post(parent_module_path(path, parent), params) do |conn|
    conn.request :multipart
    conn.request :url_encoded
  end
end

.put(_path, _params = {}) ⇒ Object

Raises:

  • (NotImplementedError)


59
60
61
# File 'lib/zoho_hub/records/attachment.rb', line 59

def put(_path, _params = {})
  raise NotImplementedError
end

Instance Method Details

#post(path, _params = {}) ⇒ Object



72
73
74
# File 'lib/zoho_hub/records/attachment.rb', line 72

def post(path, _params = {})
  self.class.post(path, parent: parent, **to_params)
end

#put(_path, _params = {}) ⇒ Object

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/zoho_hub/records/attachment.rb', line 76

def put(_path, _params = {})
  raise NotImplementedError
end

#saveObject



80
81
82
83
84
85
86
87
88
# File 'lib/zoho_hub/records/attachment.rb', line 80

def save(*)
  super
rescue => e
  if e.message.include?('Attachment link already exists')
    raise AttachmentLinkTakenError, e.message
  else
    raise e
  end
end

#to_paramsObject



90
91
92
# File 'lib/zoho_hub/records/attachment.rb', line 90

def to_params
  { attachmentUrl: attachment_url }
end