Class: PipeDrive::ResourceBase

Inherits:
Base
  • Object
show all
Defined in:
lib/pipe_drive/resource_base.rb

Direct Known Subclasses

Deal, Organization, OverallSourceBase, Person

Class Method Summary collapse

Methods inherited from Base

create, delete, #delete, find_by_id, #initialize, #parameterize, parameterize, requester, #requester, search_and_setup_by, #update, update

Constructor Details

This class inherits a constructor from PipeDrive::Base

Class Method Details

.field_keysObject



16
17
18
# File 'lib/pipe_drive/resource_base.rb', line 16

def field_keys
  PipeDrive.field_keys[resource_name.to_sym]
end

.find_by(type, opts, strict = false) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pipe_drive/resource_base.rb', line 51

def find_by(type, opts, strict=false)
  targets = search(type, opts)
  return if targets.nil? || targets.empty?
  if strict
    targets.find do |target|
      target.send(type) == opts[type]
    end
  else
    targets.first
  end
end

.list(options = {}, &block) ⇒ Object



20
21
22
23
24
25
# File 'lib/pipe_drive/resource_base.rb', line 20

def list(options={}, &block)
  path = "/#{resource_name}s"
  params = {start_from: 0, limit: DEFAULT_PER_PAGE}
  params.merge!(options)
  pagination(path, params, &block)
end

.pagination_list(start_from = 0, per_page = DEFAULT_PER_PAGE, options = {}, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pipe_drive/resource_base.rb', line 27

def pagination_list(start_from=0, per_page=DEFAULT_PER_PAGE, options={}, &block)
  path = "/#{resource_name}s"
  params = {start: start_from, limit: per_page}
  params.merge!(options)
  resources = requester.http_get(path, params) do |result|
    result[:data].nil? ? nil : list_objects(result)
  end
  resources.each do |resource|
    yield resource
  end if block_given?
  resources
end

.resource_nameObject



12
13
14
# File 'lib/pipe_drive/resource_base.rb', line 12

def resource_name
  name.split('::').last.downcase
end

.search(type, opts) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
# File 'lib/pipe_drive/resource_base.rb', line 40

def search(type, opts)
  raise NotAllowSearchType.new(type) unless const_get('ALLOW_FOR_SEARCH_TERMS').include?(type)
  raise NotProvideAssignType.new(type) if opts[type].nil?
  params = {term: opts[type]}
  allow_search_opts = const_get('ALLOW_FOR_ADDITION_SEARCH_OPTS')
  params.merge!(opts.slice(*allow_search_opts))
  requester.http_get("/#{resource_name}s/find", params) do |result|
    result[:data].nil? ? nil : list_objects(result)
  end
end