class PipeDrive::ResourceBase

Public Class Methods

new(attrs) click to toggle source
Calls superclass method PipeDrive::Base::new
# 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

Protected Class Methods

field_class() click to toggle source
# File lib/pipe_drive/resource_base.rb, line 48
def field_class
  Object.const_get("#{name}Field")
end
field_keys() click to toggle source
# File lib/pipe_drive/resource_base.rb, line 36
def field_keys
  PipeDrive.field_keys[resource_name.to_sym]
end
field_names() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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(path, params) { |item| ... } click to toggle source
# File lib/pipe_drive/resource_base.rb, line 97
def pagination(path, params, &block)
  resources = []
  loop do
    next_start_from = requester.http_get(path, params) do |result|
      return resources if result[:data].nil?
      items = list_objects(result)
      resources += items
      items.each do |item|
        yield item
      end if block_given?
      result.dig(:additional_data, :pagination, :next_start)
    end
    break if next_start_from.nil?
    params[:start] = next_start_from
  end
  resources
end
pagination_list(start_from=0, per_page=DEFAULT_PER_PAGE, options={}) { |resource| ... } click to toggle source
# 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_class() click to toggle source
# File lib/pipe_drive/resource_base.rb, line 44
def resource_class
  self
end
resource_name() click to toggle source
# File lib/pipe_drive/resource_base.rb, line 32
def resource_name
  name.split('::').last.downcase
end

Protected Instance Methods

pagination(path, params, &block) click to toggle source
# File lib/pipe_drive/resource_base.rb, line 26
def pagination(path, params, &block)
  self.class.send(:pagination, path, params, &block)
end