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
|
# File 'lib/pipe_drive/resource_base.rb', line 4
def initialize(attrs)
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
super(attrs)
end
|
Class Method Details
.field_class ⇒ Object
41
42
43
|
# File 'lib/pipe_drive/resource_base.rb', line 41
def field_class
Object.const_get("#{name}Field")
end
|
.field_keys ⇒ Object
29
30
31
|
# File 'lib/pipe_drive/resource_base.rb', line 29
def field_keys
PipeDrive.field_keys[resource_name.to_sym]
end
|
.field_names ⇒ Object
33
34
35
|
# File 'lib/pipe_drive/resource_base.rb', line 33
def field_names
PipeDrive.field_names[resource_name.to_sym]
end
|
.find_by(type, opts, strict = false) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/pipe_drive/resource_base.rb', line 76
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
45
46
47
48
49
50
|
# File 'lib/pipe_drive/resource_base.rb', line 45
def list(options={}, &block)
path = "/#{resource_name}s"
params = {start_from: 0, limit: DEFAULT_PER_PAGE}
params.merge!(options)
(path, params, &block)
end
|
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/pipe_drive/resource_base.rb', line 52
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
37
38
39
|
# File 'lib/pipe_drive/resource_base.rb', line 37
def resource_class
self
end
|
.resource_name ⇒ Object
25
26
27
|
# File 'lib/pipe_drive/resource_base.rb', line 25
def resource_name
name.split('::').last.downcase
end
|
.search(type, opts) ⇒ Object
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/pipe_drive/resource_base.rb', line 65
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
|