Class: ZohoService::ApiCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/zoho_service/api_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, attrs = {}) ⇒ ApiCollection

Returns a new instance of ApiCollection.



5
6
7
8
9
10
11
12
13
14
# File 'lib/zoho_service/api_collection.rb', line 5

def initialize(parent, attrs = {})
  @loaded = false
  @parent = parent
  @request_params = attrs # request_params writing only in init proc!
  super()
  if accepted_queries.include?('departmentId')
    @request_params[:query] ||= {}
    @request_params[:query][:departmentId] ||= parent.connector.client_params[:departmentId]
  end
end

Instance Attribute Details

#loadedObject (readonly)

Returns the value of attribute loaded.



3
4
5
# File 'lib/zoho_service/api_collection.rb', line 3

def loaded
  @loaded
end

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/zoho_service/api_collection.rb', line 3

def parent
  @parent
end

#request_paramsObject (readonly)

Returns the value of attribute request_params.



3
4
5
# File 'lib/zoho_service/api_collection.rb', line 3

def request_params
  @request_params
end

Instance Method Details

#accepted_queriesObject



78
79
80
81
82
83
84
85
# File 'lib/zoho_service/api_collection.rb', line 78

def accepted_queries
  if request_params[:query] && request_params[:searchStr]
    ['from', 'limit', 'sortBy', 'departmentId']
  else
    mod = request_params[:items_class]
    mod.model_params &&  mod.model_params[:queries] ? mod.model_params[:queries] : []
  end
end

#allObject



74
75
76
# File 'lib/zoho_service/api_collection.rb', line 74

def all
  run_request(__method__)
end

#by_id(id) ⇒ Object



65
66
67
# File 'lib/zoho_service/api_collection.rb', line 65

def by_id(id)
  request_params[:items_class].new_by_id(parent, id)
end

#collection_urlObject



99
100
101
# File 'lib/zoho_service/api_collection.rb', line 99

def collection_url
  parent.resource_path + request_params[:items_class].class_path
end

#create(item_params) ⇒ Object



46
47
48
# File 'lib/zoho_service/api_collection.rb', line 46

def create(item_params)
  new(item_params).save!
end

#find(params) ⇒ Object



54
55
56
57
58
59
# File 'lib/zoho_service/api_collection.rb', line 54

def find(params)
  # this method not normal! It is temporary! Search method not working normal on the desk.zoho.com.
  # use "search" method if you want search only in texts of a model.
  params = params ? params.deep_symbolize : {}
  select { |x| x.to_h.deep_symbolize.deep_include?(params) }
end

#find_or_initialize_by(params, create_params = {}) ⇒ Object



50
51
52
# File 'lib/zoho_service/api_collection.rb', line 50

def find_or_initialize_by(params, create_params = {})
  find(params).first || create(params.deep_merge(create_params))
end

#find_with_str(searchStr, params = {}) ⇒ Object



61
62
63
# File 'lib/zoho_service/api_collection.rb', line 61

def find_with_str(searchStr, params = {})
  search(searchStr).find(params)
end

#first(*args, &block) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/zoho_service/api_collection.rb', line 87

def first(*args, &block)
  if !@loaded && accepted_queries.include?('limit')
    return new_collection({ query: { limit: 1 } }).run_request(__method__).first(*args, &block)
  end
  run_request(__method__)
  super(*args, &block)
end

#new(item_params) ⇒ Object



39
40
41
42
43
44
# File 'lib/zoho_service/api_collection.rb', line 39

def new(item_params)
  if accepted_queries.include?('departmentId')
    item_params[:departmentId] ||= parent.connector.client_params[:departmentId]
  end
  request_params[:items_class].new(parent, item_params)
end

#new_collection(more_params) ⇒ Object



95
96
97
# File 'lib/zoho_service/api_collection.rb', line 95

def new_collection(more_params)
  self.class.new(parent, request_params.deep_merge(more_params))
end

#run_request(eval_method) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zoho_service/api_collection.rb', line 16

def run_request(eval_method)
  return self if @loaded
  @loaded = true
  req_query = @request_params[:query] || {}
  if accepted_queries.include?('limit') && !(req_query[:from] || req_query[:limit] || request_params[:skip_pages])
    items_per_page = 50
    (0..100).each do |page|
      query = req_query.merge(from: 0 + (items_per_page * page), limit: items_per_page)
      query[:from] += 1 if query[:from] > 0 # WTF bug with from-limit on zoho server!
      query.merge!(sortBy: 'createdTime') if accepted_queries.include?('sortBy') && !query[:sortBy]
      arr = new_collection(query: query).run_request(__method__)
      arr.each { |x| self.push(x) }
      break unless arr.count == items_per_page
    end
  else
    parent.connector.load_by_api(collection_url, req_query)&.each do |item_data|
      raise("ERROR in ZohoService gem. item_data=[#{item_data}]") unless item_data.is_a?(Hash)
      self.push(request_params[:items_class].new(parent, item_data))
    end
  end
  self
end

#search(searchStr) ⇒ Object

Search method can search only searchStr in texts of a model :( . it`s not good practice :(.



69
70
71
72
# File 'lib/zoho_service/api_collection.rb', line 69

def search(searchStr) # Search method can search only searchStr in texts of a model :( . it`s not good practice :(.
  new_collection(query: { searchStr: searchStr, 'module': request_params[:items_class].models_name,
                          sortBy: 'relevance' })
end