Class: PipeDrive::ResourceBase

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

Direct Known Subclasses

Activity, Deal, Organization, OverallSourceBase, Person

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(attrs) ⇒ ResourceBase

Returns a new instance of ResourceBase.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pipe_drive/resource_base.rb', line 4

def initialize(attrs)
  unless self.class.field_names.nil?
    data = attrs[:data] || PipeDrive.hash_except(attrs, [:additional_data])
    processed_data = {}
    data.each_pair do |key, value|
      key = key.to_s
      if key.end_with?('_id')
        field_name = key
      else
        field_name_map = self.class.field_names[key]
        field_name = field_name_map.nil? ? key : field_name_map[:name]
      end
      processed_data[field_name] = value
    end 
    attrs[:data] = processed_data
  end

  super(attrs)
end

Class Method Details

.field_classObject



48
49
50
# File 'lib/pipe_drive/resource_base.rb', line 48

def field_class
  Object.const_get("#{name}Field")
end

.field_keysObject



36
37
38
# File 'lib/pipe_drive/resource_base.rb', line 36

def field_keys
  PipeDrive.field_keys[resource_name.to_sym]
end

.field_namesObject



40
41
42
# File 'lib/pipe_drive/resource_base.rb', line 40

def field_names
  PipeDrive.field_names[resource_name.to_sym]
end

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



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pipe_drive/resource_base.rb', line 83

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



52
53
54
55
56
57
# File 'lib/pipe_drive/resource_base.rb', line 52

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



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pipe_drive/resource_base.rb', line 59

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_classObject



44
45
46
# File 'lib/pipe_drive/resource_base.rb', line 44

def resource_class
  self
end

.resource_nameObject



32
33
34
# File 'lib/pipe_drive/resource_base.rb', line 32

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

.search(type, opts) ⇒ Object

Raises:



72
73
74
75
76
77
78
79
80
81
# File 'lib/pipe_drive/resource_base.rb', line 72

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