class Pipedrive::Base

Base class for setting HTTParty configurations globally

Attributes

data[R]

Public Class Methods

all(response = nil, options = {}, get_absolutely_all = false) click to toggle source
# File lib/pipedrive/base.rb, line 86
def all(response = nil, options = {}, get_absolutely_all = false)
  res = response || get(resource_path, options)
  if res.ok?
    data = res['data'].nil? ? [] : res['data'].map{|obj| new(obj)}
    if get_absolutely_all && has_pagination?(res)
      options[:query] = options[:query].merge({:start => res['additional_data']['pagination']['next_start']})
      data += self.all(nil,options,true)
    end
    data
  else
    bad_response(res, options)
  end
end
authenticate(token) click to toggle source

Sets the authentication credentials in a class variable.

@param [String] email cl.ly email @param [String] password cl.ly password @return [Hash] authentication credentials

# File lib/pipedrive/base.rb, line 68
def authenticate(token)
  default_params :api_token => token
end
bad_response(response, params={}) click to toggle source

Examines a bad response and raises an appropriate exception

@param [HTTParty::Response] response

# File lib/pipedrive/base.rb, line 75
def bad_response(response, params={})
  if response.class == HTTParty::Response
    raise HTTParty::ResponseError, response
  end
  raise StandardError, 'Unknown error'
end
create( opts = {} ) click to toggle source
# File lib/pipedrive/base.rb, line 104
def create( opts = {} )
  res = post resource_path, :body => opts
  if res.success?
    res['data'] = opts.merge res['data']
    new(res)
  else
    bad_response(res,opts)
  end
end
find(id) click to toggle source
# File lib/pipedrive/base.rb, line 114
def find(id)
  res = get "#{resource_path}/#{id}"
  res.ok? ? new(res) : bad_response(res,id)
end
find_by_name(name, opts={}) click to toggle source
# File lib/pipedrive/base.rb, line 119
def find_by_name(name, opts={})
  res = get "#{resource_path}/find", :query => { :term => name }.merge(opts)
  res.ok? ? new_list(res) : bad_response(res,{:name => name}.merge(opts))
end
has_pagination?(res) click to toggle source
# File lib/pipedrive/base.rb, line 100
def has_pagination?(res)
  res['additional_data'] && res['additional_data']['pagination'] && res['additional_data']['pagination']['more_items_in_collection']
end
new(attrs = {}) click to toggle source

Create a new Pipedrive::Base object.

Only used internally

@param [Hash] attributes @return [Pipedrive::Base]

Calls superclass method
# File lib/pipedrive/base.rb, line 34
def initialize(attrs = {})
  if attrs['data']
    struct_attrs = attrs['data']

    if attrs['additional_data']
      struct_attrs.merge!(attrs['additional_data'])
    end
  else
    struct_attrs = attrs
  end

  super(struct_attrs)
end
new_list( attrs ) click to toggle source
# File lib/pipedrive/base.rb, line 82
def new_list( attrs )
  attrs['data'].is_a?(Array) ? attrs['data'].map {|data| self.new( 'data' => data ) } : []
end
resource_path() click to toggle source
# File lib/pipedrive/base.rb, line 124
def resource_path
  # The resource path should match the camelCased class name with the
  # first letter downcased.  Pipedrive API is sensitive to capitalisation
  klass = name.split('::').last
  klass[0] = klass[0].chr.downcase
  klass.end_with?('y') ? "/#{klass.chop}ies" : "/#{klass}s"
end

Public Instance Methods

update(opts = {}) click to toggle source

Updates the object.

@param [Hash] opts @return [Boolean]

# File lib/pipedrive/base.rb, line 52
def update(opts = {})
  res = put "#{resource_path}/#{id}", :body => opts
  if res.success?
    res['data'] = Hash[res['data'].map {|k, v| [k.to_sym, v] }]
    @table.merge!(res['data'])
  else
    false
  end
end