Class: Pipedrive::Resource
Direct Known Subclasses
Activity, ActivityType, CallLog, Currency, Deal, File, Filter, GlobalMessage, Goal, Lead, LeadLabel, LeadSource, Note, Organization, OrganizationField, Participant, Person, Pipeline, Product, Stage, Subscription, Team, User, Webhook
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
create
#delete
#update
included
Constructor Details
#initialize(data = {}) ⇒ Resource
Returns a new instance of Resource.
113
114
115
116
|
# File 'lib/pipedrive/resource.rb', line 113
def initialize(data = {})
@data = @unsaved_data = {}
initialize_from_data(data)
end
|
Class Attribute Details
.resources_url ⇒ Object
Returns the value of attribute resources_url.
11
12
13
|
# File 'lib/pipedrive/resource.rb', line 11
def resources_url
@resources_url
end
|
Class Method Details
.all(params = {}) ⇒ Object
55
56
57
58
|
# File 'lib/pipedrive/resource.rb', line 55
def all(params = {})
response = request(:get, resource_url, params)
response.dig(:data)&.map { |d| new(d) }
end
|
.class_name ⇒ Object
17
18
19
|
# File 'lib/pipedrive/resource.rb', line 17
def class_name
name.split("::")[-1]
end
|
.fields_dicc ⇒ Object
30
31
32
|
# File 'lib/pipedrive/resource.rb', line 30
def fields_dicc
@fields_dicc ||= fields[0] if respond_to? :fields
end
|
.has_many(resource_name, args = {}) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/pipedrive/resource.rb', line 69
def has_many(resource_name, args = {})
class_name = args[:class_name]
unless resource_name && class_name
raise "You must specify the resource name and its class name " \
"For example has_many :deals, class_name: 'Deal'"
end
class_name_lower_case = class_name.downcase
singular = args[:singular] || class_name_lower_case
options = { "include_#{class_name_lower_case}_data": 1 }
class_name = "::Pipedrive::#{class_name}" unless class_name.include?("Pipedrive")
define_method(resource_name) do |params = {}|
response = request(:get,
"#{resource_url}/#{resource_name}",
params.merge(options))
response
.dig(:data)
&.map do |data|
class_name_as_sym = class_name_lower_case.to_sym
data[:metadata] = data
if data.key?(class_name_as_sym)
data = data.merge(data.delete(class_name_as_sym))
end
Object.const_get(class_name).new(data)
end
end
define_method("add_#{singular}") do |params|
response = request(
:post,
"#{resource_url}/#{resource_name}",
params.merge(id: id)
)
Object.const_get(class_name).new(response.dig(:data))
end
define_method("delete_#{singular}") do |resource_id|
response = request(:delete, "#{resource_url}/#{resource_name}/#{resource_id}")
response[:success]
end
end
|
.inverted_fields_dicc ⇒ Object
34
35
36
|
# File 'lib/pipedrive/resource.rb', line 34
def inverted_fields_dicc
@inverted_fields_dicc ||= fields_dicc&.invert
end
|
.resource_url ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/pipedrive/resource.rb', line 21
def resource_url
if self == Resource
raise NotImplementedError,
"Pipedrive::Resource is an abstract class. You should perform actions " \
"on its subclasses (Organization, Person, Deal, etc)"
end
resources_url || "#{class_name.downcase}s"
end
|
.retrieve(id) ⇒ Object
50
51
52
53
|
# File 'lib/pipedrive/resource.rb', line 50
def retrieve(id)
response = request(:get, "#{resource_url}/#{id}")
new(response.dig(:data))
end
|
.search(term, params = {}) ⇒ Object
60
61
62
63
64
65
66
67
|
# File 'lib/pipedrive/resource.rb', line 60
def search(term, params = {})
response = request(
:get,
"#{resource_url}/search",
{ term: term }.merge(params)
)
response.dig(:data, :items).map { |d| new(d.dig(:item)) }
end
|
.search_for_fields(values) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/pipedrive/resource.rb', line 38
def search_for_fields(values)
return values unless values.is_a?(Hash) && fields_dicc&.any?
values.reduce({}) do |new_hash, (k, v)|
if inverted_fields_dicc[k]
new_hash.merge(inverted_fields_dicc[k] => v)
else
new_hash.merge(k => v)
end
end
end
|
.update_method(method_override = nil) ⇒ Object
13
14
15
|
# File 'lib/pipedrive/resource.rb', line 13
def update_method(method_override = nil)
@update_method ||= method_override
end
|
Instance Method Details
#initialize_from_data(data) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/pipedrive/resource.rb', line 136
def initialize_from_data(data)
klass = self.class
@data = data
data&.each_key do |k|
m, is_custom_field = klass.fields_dicc&.dig(k) &&
[klass.fields_dicc&.dig(k), true] ||
[k, false]
if m == :method
klass.define_method(m) do |*args|
args.empty? ? fetch_value(m, is_custom_field) : super(*args)
end
else
klass.define_method(m) { fetch_value(m, is_custom_field) }
end
klass.define_method(:"#{m}=") do |value|
@data[m] = @unsaved_data[m] = value
end
next unless [FalseClass, TrueClass].include?(
fetch_value(m, is_custom_field).class
)
klass.define_method(:"#{m}?") do
fetch_value(m, is_custom_field)
end
end
self
end
|
#no_content? ⇒ Boolean
Also known as:
empty?
182
183
184
|
# File 'lib/pipedrive/resource.rb', line 182
def no_content?
@data.nil? || @data.empty?
end
|
#refresh ⇒ Object
131
132
133
134
|
# File 'lib/pipedrive/resource.rb', line 131
def refresh
response = request(:get, resource_url)
initialize_from_data(response.dig(:data))
end
|
#resource_url ⇒ Object
118
119
120
|
# File 'lib/pipedrive/resource.rb', line 118
def resource_url
"#{self.class.resource_url}/#{id}"
end
|
#search_for_fields(values) ⇒ Object
122
123
124
|
# File 'lib/pipedrive/resource.rb', line 122
def search_for_fields(values)
self.class.search_for_fields(values)
end
|
#update_attributes(new_attrs) ⇒ Object
126
127
128
129
|
# File 'lib/pipedrive/resource.rb', line 126
def update_attributes(new_attrs)
new_attrs.delete("id")
@data.merge!(new_attrs)
end
|
#update_method ⇒ Object
174
175
176
|
# File 'lib/pipedrive/resource.rb', line 174
def update_method
self.class.update_method
end
|