Class: PipeDrive::ResourceBase
- Inherits:
-
Base
- Object
- OpenStruct
- Base
- PipeDrive::ResourceBase
show all
- Defined in:
- lib/pipe_drive/resource_base.rb
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
Returns a new instance of ResourceBase.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# 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|
field_name_map = self.class.field_names[key.to_s]
field_name = field_name_map.nil? ? key : field_name_map[:name]
processed_data[field_name] = value
end
attrs[:data] = processed_data
end
super(attrs)
end
|
Class Method Details
.field_class ⇒ Object
43
44
45
|
# File 'lib/pipe_drive/resource_base.rb', line 43
def field_class
Object.const_get("#{name}Field")
end
|
.field_keys ⇒ Object
31
32
33
|
# File 'lib/pipe_drive/resource_base.rb', line 31
def field_keys
PipeDrive.field_keys[resource_name.to_sym]
end
|
.field_names ⇒ Object
35
36
37
|
# File 'lib/pipe_drive/resource_base.rb', line 35
def field_names
PipeDrive.field_names[resource_name.to_sym]
end
|
.find_by(type, opts, strict = false) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/pipe_drive/resource_base.rb', line 78
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
47
48
49
50
51
52
|
# File 'lib/pipe_drive/resource_base.rb', line 47
def list(options={}, &block)
path = "/#{resource_name}s"
params = {start_from: 0, limit: DEFAULT_PER_PAGE}
params.merge!(options)
(path, params, &block)
end
|
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/pipe_drive/resource_base.rb', line 54
def (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_class ⇒ Object
39
40
41
|
# File 'lib/pipe_drive/resource_base.rb', line 39
def resource_class
self
end
|
.resource_name ⇒ Object
27
28
29
|
# File 'lib/pipe_drive/resource_base.rb', line 27
def resource_name
name.split('::').last.downcase
end
|
.search(type, opts) ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/pipe_drive/resource_base.rb', line 67
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
|