class ZohoHub::BaseRecord
Constants
- DEFAULT_PAGE
Default page number when fetching all.
- DEFAULT_RECORDS_PER_PAGE
Default number of records when fetching all.
- MIN_RECORDS
Minimum number of records to fetch when fetching all.
Public Class Methods
add_note(id:, title: '', content: '')
click to toggle source
# File lib/zoho_hub/base_record.rb, line 89 def add_note(id:, title: '', content: '') path = File.join(request_path, id, 'Notes') post(path, data: [{ Note_Title: title, Note_Content: content }]) end
all(params = {})
click to toggle source
# File lib/zoho_hub/base_record.rb, line 132 def all(params = {}) params[:page] ||= DEFAULT_PAGE params[:per_page] ||= DEFAULT_RECORDS_PER_PAGE params[:per_page] = MIN_RECORDS if params[:per_page] < MIN_RECORDS body = get(request_path, params) response = build_response(body) data = response.nil? ? [] : response.data data.map { |json| new(json) } end
blueprint_transition(id, transition_id, data = {})
click to toggle source
# File lib/zoho_hub/base_record.rb, line 81 def blueprint_transition(id, transition_id, data = {}) new(id: id).blueprint_transition(transition_id, data) end
blueprint_transitions(id)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 85 def blueprint_transitions(id) new(id: id).blueprint_transitions end
build_response(body)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 153 def build_response(body) response = Response.new(body) raise InvalidTokenError, response.msg if response.invalid_token? raise InternalError, response.msg if response.internal_error? raise RecordInvalid, response.msg if response.invalid_data? raise InvalidModule, response.msg if response.invalid_module? raise NoPermission, response.msg if response.no_permission? raise MandatoryNotFound, response.msg if response.mandatory_not_found? raise RecordInBlueprint, response.msg if response.record_in_blueprint? response end
create(params)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 73 def create(params) new(params).save end
download_attachment(parent_id:, attachment_id:)
click to toggle source
# File lib/zoho_hub/modules/attachment.rb, line 17 def download_attachment(parent_id:, attachment_id:) attachment = related_attachments(parent_id: parent_id).find { |a| a.id == attachment_id } uri = File.join(request_path, parent_id, 'Attachments', attachment_id) res = ZohoHub.connection.adapter.get(uri) attachment.content_type = res.headers['content-type'] extension = File.extname(attachment.file_name) basename = File.basename(attachment.file_name, extension) file = Tempfile.new([basename, extension]) file.binmode file.write(res.body) file.rewind attachment.file = file attachment end
exists?(id)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 145 def exists?(id) !find(id).nil? rescue RecordNotFound false end
Also aliased as: exist?
find(id)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 31 def find(id) body = get(File.join(request_path, id.to_s)) response = build_response(body) if response.empty? raise RecordNotFound, "Couldn't find #{request_path.singularize} with 'id'=#{id}" end new(response.data.first) end
find_by(params)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 68 def find_by(params) records = where(params) records.first end
new(params = {})
click to toggle source
# File lib/zoho_hub/base_record.rb, line 168 def initialize(params = {}) attributes.each do |attr| zoho_key = attr_to_zoho_key(attr) value = params[zoho_key].nil? ? params[attr] : params[zoho_key] send("#{attr}=", value) end end
request_path(name = nil)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 25 def request_path(name = nil) @request_path = name if name @request_path ||= StringUtils.pluralize(StringUtils.demodulize(to_s)) @request_path end
update(id, params)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 77 def update(id, params) new(id: id).update(params) end
update_all(records)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 124 def update_all(records) zoho_params = records.transform_keys { |key| attr_to_zoho_key(key) } body = put(File.join(request_path), data: zoho_params) build_response(body) end
where(params)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 42 def where(params) path = File.join(request_path, 'search') if params.size == 1 params = case params.keys.first when :criteria, :email, :phone, :word # these attributes are directly handled by Zoho # see https://www.zoho.com/crm/help/developer/api/search-records.html params else key = attr_to_zoho_key(params.keys.first) { criteria: "#{key}:equals:#{params.values.first}" } end end body = get(path, params) response = build_response(body) data = response.nil? ? [] : response.data data.map { |json| new(json) } end
Public Instance Methods
blueprint_transition(transition_id, data = {})
click to toggle source
# File lib/zoho_hub/base_record.rb, line 196 def blueprint_transition(transition_id, data = {}) body = put(File.join(self.class.request_path, id, 'actions/blueprint'), blueprint: [{ transition_id: transition_id, data: data }]) build_response(body) end
blueprint_transitions()
click to toggle source
# File lib/zoho_hub/base_record.rb, line 203 def blueprint_transitions body = get(File.join(self.class.request_path, id, 'actions/blueprint')) build_response(body) end
build_response(body)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 224 def build_response(body) self.class.build_response(body) end
new_record?()
click to toggle source
# File lib/zoho_hub/base_record.rb, line 208 def new_record? !id end
save()
click to toggle source
# File lib/zoho_hub/base_record.rb, line 177 def save body = if new_record? # create new record post(self.class.request_path, data: [to_params]) else # update existing record put(File.join(self.class.request_path, id), data: [to_params]) end response = build_response(body) response.data.first.dig(:details, :id) end
to_params()
click to toggle source
# File lib/zoho_hub/base_record.rb, line 212 def to_params params = {} attributes.each do |attr| key = attr_to_zoho_key(attr) params[key] = send(attr) end params end
update(params)
click to toggle source
# File lib/zoho_hub/base_record.rb, line 189 def update(params) zoho_params = params.transform_keys { |key| attr_to_zoho_key(key) } body = put(File.join(self.class.request_path, id), data: [zoho_params]) build_response(body) end