Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand base record #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions lib/zoho_hub/base_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ class BaseRecord
# Minimum number of records to fetch when fetching all.
MIN_RECORDS = 2

# Valid attributes for search
SEARCH_ATTRS = [:criteria, :email, :phone, :word, :converted, :approved, :page, :per_page]

class << self
def request_path(name = nil)
@request_path = name if name
@request_path ||= StringUtils.pluralize(StringUtils.demodulize(to_s))
@request_path
end

# block will be passed the farady request, to use for further configuration
# example: Account.where('...zoho_id...'){|req| req}
def find(id)
body = get(File.join(request_path, id.to_s))
# @example
body = get(File.join(request_path, id.to_s), &block)
response = build_response(body)

if response.empty?
Expand All @@ -39,34 +45,37 @@ def find(id)
new(response.data.first)
end

def where(params)
# block will be passed the farady request, to use for further configuration
# example: Account.where({account_name_: 'Whiting'}){|req| req.params['limit'] = 25}
# note, ending a criteria name with an underscore use start_with instead of equals
def where(params, &block)
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
# fix what happens if params[:criteria] is already set
whereParams = params.entries.reduce({criteria: []}) do |hsh, (k, v)|
if SEARCH_ATTRS.include? k
hsh[k] = v
return hsh
end
if k.ends_with? '_'
hsh[:criteria].push("(#{attr_to_zoho_key(k.gsub(/_$/,''))}:starts_with:#{v})")
else
hsh[:criteria].push("(#{attr_to_zoho_key(k)}:equals:#{v})")
end
end
whereParams[:criteria] = whereParams[:criteria].join('and')

body = get(path, params)
body = get(path, params, &block)
response = build_response(body)

data = response.nil? ? [] : response.data

data.map { |json| new(json) }
end

def find_by(params)
records = where(params)
# block will be passed the farady request, to use for further configuration
# example: Account.where('...zoho_id...'){|req| req.approved = false}
def find_by(params, &block)
records = where(params, &block)
records.first
end

Expand All @@ -87,12 +96,14 @@ def add_note(id:, title: '', content: '')
post(path, data: [{ Note_Title: title, Note_Content: content }])
end

def all(params = {})
# block will be passed the farady request, to use for further configuration
# ZohoHub::Account.all{|req| req.headers['If-Modified-Since'] = ::Account.maximum(:created_at).iso8601}
def all(params = {}, &block)
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)
body = get(request_path, params, &block)
response = build_response(body)

data = response.nil? ? [] : response.data
Expand Down
16 changes: 8 additions & 8 deletions lib/zoho_hub/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,31 @@ def initialize(access_token:, api_domain: nil, expires_in: 3600, refresh_token:
@refresh_token ||= refresh_token # do not overwrite if it's already set
end

def get(path, params = {})
def get(path, params = {}, &block)
log "GET #{path} with #{params}"

response = with_refresh { adapter.get(path, params) }
response = with_refresh { adapter.get(path, params, &block) }
response.body
end

def post(path, params = {})
def post(path, params = {}, &block)
log "POST #{path} with #{params}"

response = with_refresh { adapter.post(path, params) }
response = with_refresh { adapter.post(path, params, &block) }
response.body
end

def put(path, params = {})
def put(path, params = {}, &block)
log "PUT #{path} with #{params}"

response = with_refresh { adapter.put(path, params) }
response = with_refresh { adapter.put(path, params, &block) }
response.body
end

def delete(path, params = {})
def delete(path, params = {}, &block)
log "DELETE #{path} with #{params}"

response = with_refresh { adapter.delete(path, params) }
response = with_refresh { adapter.delete(path, params, &block) }
response.body
end

Expand Down
32 changes: 16 additions & 16 deletions lib/zoho_hub/with_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,37 @@ def self.included(base)
end

module ClassMethods
def get(path, params = {})
ZohoHub.connection.get(path, params)
def get(path, params = {}, &block)
ZohoHub.connection.get(path, params, &block)
end

def post(path, params = {})
ZohoHub.connection.post(path, params.to_json)
def post(path, params = {}, &block)
ZohoHub.connection.post(path, params.to_json, &block)
end

def put(path, params = {})
ZohoHub.connection.put(path, params.to_json)
def put(path, params = {}, &block)
ZohoHub.connection.put(path, params.to_json, &block)
end

def delete(path, params = {})
ZohoHub.connection.delete(path, params)
def delete(path, params = {}, &block)
ZohoHub.connection.delete(path, params, &block)
end
end

def get(path, params = {})
self.class.get(path, params)
def get(path, params = {}, &block)
self.class.get(path, params, &block)
end

def post(path, params = {})
self.class.post(path, params)
def post(path, params = {}, &block)
self.class.post(path, params, &block)
end

def put(path, params = {})
self.class.put(path, params)
def put(path, params = {}, &block)
self.class.put(path, params, &block)
end

def delete(path, params = {})
self.class.delete(path, params)
def delete(path, params = {}, &block)
self.class.delete(path, params, &block)
end
end
end