Class: Pipedrive::Base

Inherits:
OpenStruct
  • Object
show all
Extended by:
Forwardable
Includes:
HTTParty
Defined in:
lib/pipedrive/base.rb

Overview

Base class for setting HTTParty configurations globally

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Pipedrive::Base

Create a new Pipedrive::Base object.

Only used internally

Parameters:

  • attributes (Hash)

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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
    if attrs['related_objects']
      struct_attrs.merge!(initialize_related_objects(attrs['related_objects']))
    end
  else
    struct_attrs = attrs
  end

  super(struct_attrs)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data


26
27
28
# File 'lib/pipedrive/base.rb', line 26

def data
  @data
end

Class Method Details

.all(response = nil, options = {}, get_absolutely_all = false) ⇒ Object


119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/pipedrive/base.rb', line 119

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 && res['additional_data']['pagination'] && res['additional_data']['pagination'] && res['additional_data']['pagination']['more_items_in_collection']
      options[:query] = options[:query].merge({:start => res['additional_data']['pagination']['next_start']})
      data += self.all(nil,options,true)
    end
    data
  else
    bad_response(res,attrs)
  end
end

.authenticate(token) ⇒ Hash

Sets the authentication credentials in a class variable.

Parameters:

  • email (String)

    cl.ly email

  • password (String)

    cl.ly password

Returns:

  • (Hash)

    authentication credentials


100
101
102
# File 'lib/pipedrive/base.rb', line 100

def authenticate(token)
  default_params :api_token => token
end

.bad_response(response, params = {}) ⇒ Object

Examines a bad response and raises an appropriate exception

Parameters:

  • response (HTTParty::Response)

Raises:

  • (StandardError)

107
108
109
110
111
112
113
# File 'lib/pipedrive/base.rb', line 107

def bad_response(response, params={})
  puts params.inspect
  if response.class == HTTParty::Response
    raise HTTParty::ResponseError, response
  end
  raise StandardError, 'Unknown error'
end

.create(opts = {}) ⇒ Object


133
134
135
136
137
138
139
140
141
# File 'lib/pipedrive/base.rb', line 133

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

.destroy(id) ⇒ Object


158
159
160
161
# File 'lib/pipedrive/base.rb', line 158

def destroy(id)
   res = delete "#{resource_path}/#{id}"
   res.ok? ? res : bad_response(res, id)
end

.find(id) ⇒ Object


148
149
150
151
# File 'lib/pipedrive/base.rb', line 148

def find(id)
  res = get "#{resource_path}/#{id}"
  res.ok? ? new(res) : bad_response(res,id)
end

.find_by_name(name, opts = {}) ⇒ Object


153
154
155
156
# File 'lib/pipedrive/base.rb', line 153

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

.new_list(attrs) ⇒ Object


115
116
117
# File 'lib/pipedrive/base.rb', line 115

def new_list( attrs )
  attrs['data'].is_a?(Array) ? attrs['data'].map {|data| self.new( 'data' => data ) } : []
end

.resource_pathObject


163
164
165
166
167
168
169
# File 'lib/pipedrive/base.rb', line 163

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

.search(opts) ⇒ Object


143
144
145
146
# File 'lib/pipedrive/base.rb', line 143

def search opts
  res = get resource_path, query: opts
  res.ok? ? new_list(res) : bad_response(res, opts)
end

Instance Method Details

#destroyHTTParty::Response

Destroys the object

Returns:

  • (HTTParty::Response)

    response


89
90
91
92
# File 'lib/pipedrive/base.rb', line 89

def destroy
  res = delete "#{resource_path}/#{id}"
  res.ok? ? res : bad_response(res, id)
end

Create related objects from hash

Only used internally

Parameters:

  • related_object_hash (Hash)

Returns:

  • (Hash)

57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pipedrive/base.rb', line 57

def initialize_related_objects related_object_hash
  related_objects = Hash.new
  # Create related objects if given
  related_object_hash.each do |key, value|
    # Check if the given class is defined for the related object
    class_name = "Pipedrive::" + key.capitalize
    if Object.const_defined?(class_name)
      related_object = Object::const_get(class_name).new(value.values.shift)
      related_objects[key] = related_object
    end
  end
  
  related_objects
end

#update(opts = {}) ⇒ Boolean

Updates the object.

Parameters:

  • opts (Hash) (defaults to: {})

Returns:

  • (Boolean)

76
77
78
79
80
81
82
83
84
# File 'lib/pipedrive/base.rb', line 76

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