module Pipekit::Repository

Attributes

request[R]

Public Class Methods

new(request = nil) click to toggle source
# File lib/pipekit/repository.rb, line 4
def initialize(request = nil)
  @request = request || Request.new(resource)
end

Public Instance Methods

all(query = {}) click to toggle source
# File lib/pipekit/repository.rb, line 8
def all(query = {})
  request.get("", query)
end
create(fields) click to toggle source

Public: Create a record on Pipedrive.

fields - fields for the record.

Examples

create({name: "John Doe", deal_id: 123})

Returns nothing.

# File lib/pipekit/repository.rb, line 55
def create(fields)
  request.post(fields)
end
find_by(options) click to toggle source

Public: Get the first record by one field from Pipedrive.

options - A Hash with one key-value pair. Key is a field name and values is a field value.

Examples

find_by(name: "John Doe")
find_by(github_username: "pipedriver")
find_by(id: 123)

Returns a Hash or nil if none found.

# File lib/pipekit/repository.rb, line 41
def find_by(options)
  warn "Using `Repository#find_by` with an email may return inexact matches" if email_key?(options)
  where(options, true).first
end
update(id, fields) click to toggle source

Public: Updates a record on Pipedrive.

fields - fields for the record.

Examples

update(123, {name: "Jane Doe"})

Returns nothing.

# File lib/pipekit/repository.rb, line 68
def update(id, fields)
  request.put(id, fields)
end
where(options, raise_error = false) click to toggle source

Public: Get all records from Pipedrive by one of the record's fields.

options - A Hash with one key-value pair. Key is a field name and values is a field value.

Examples

where(name: "John Doe")
where(github_username: "pipedriver")
where(id: 123)

Returns array of Hashes.

# File lib/pipekit/repository.rb, line 23
def where(options, raise_error = false)
  send("get_by_#{options.keys.first}", options.values.first)
rescue ResourceNotFoundError => error
  raise error if raise_error
  []
end

Private Instance Methods

email_key?(options) click to toggle source
# File lib/pipekit/repository.rb, line 102
def email_key?(options)
  options.keys.first && options.keys.first.to_s == "email"
end
get(id = nil) click to toggle source
# File lib/pipekit/repository.rb, line 83
def get(id = nil)
  request.get(id)
end
get_by_field(field:, value:) click to toggle source
# File lib/pipekit/repository.rb, line 91
def get_by_field(field:, value:)
  result = request.search_by_field(field: field, value: value)
  result.map { |item| get_by_id(item["id"]) }.flatten
end
get_by_id(id) click to toggle source
# File lib/pipekit/repository.rb, line 87
def get_by_id(id)
  [get(id)]
end
method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/pipekit/repository.rb, line 76
def method_missing(method_name, *args)
  super unless method_name =~ /^get_by/

  field = method_name.to_s.gsub("get_by_", "")
  get_by_field(field: field, value: args[0])
end
resource() click to toggle source
# File lib/pipekit/repository.rb, line 96
def resource
  singular_resource = self.class::SINGULAR_CLASSNAME
  pluralized_resource = self.class::PLURALIZED_CLASSNAME
  ResourceLabel.new(singular_label: singular_resource, pluralized_label: pluralized_resource)
end