Class: Pipedrive::Resource

Inherits:
Object
  • Object
show all
Extended by:
APIOperations::Create
Includes:
APIOperations::Delete, APIOperations::Request, APIOperations::Update
Defined in:
lib/pipedrive/resource.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from APIOperations::Create

create

Methods included from APIOperations::Delete

#delete

Methods included from APIOperations::Update

#update

Methods included from APIOperations::Request

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_urlObject

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_nameObject



17
18
19
# File 'lib/pipedrive/resource.rb', line 17

def class_name
  name.split("::")[-1]
end

.fields_diccObject



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
  # always include all the data of the resource
  options = { "include_#{class_name_lower_case}_data": 1 }
  # add namespace to class_name
  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_diccObject



34
35
36
# File 'lib/pipedrive/resource.rb', line 34

def inverted_fields_dicc
  @inverted_fields_dicc ||= fields_dicc&.invert
end

.resource_urlObject



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
  # init data
  @data = data
  # generate the methods
  data&.each_key do |k|
    # it could be a custom field diccionary
    m, is_custom_field = klass.fields_dicc&.dig(k) &&
                         [klass.fields_dicc&.dig(k), true] ||
                         [k, false]

    if m == :method
      # Object#method is a built-in Ruby method that accepts a symbol
      # and returns the corresponding Method object. Because the API may
      # also use `method` as a field name, we check the arity of *args
      # to decide whether to act as a getter or call the parent 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?

Returns:

  • (Boolean)


182
183
184
# File 'lib/pipedrive/resource.rb', line 182

def no_content?
  @data.nil? || @data.empty?
end

#refreshObject



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_urlObject



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_methodObject



174
175
176
# File 'lib/pipedrive/resource.rb', line 174

def update_method
  self.class.update_method
end