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

Direct Known Subclasses

Deal, Organization, Person, Pipeline, Product, Stage

Defined Under Namespace

Modules: Deals

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new CloudApp::Base object.

Only used internally

Parameters:

  • attributes (Hash)


33
34
35
36
37
38
39
# File 'lib/pipedrive/base.rb', line 33

def initialize(attrs = {})
  if attrs['data']
    super( attrs['data'] )
  else
    super(attrs)
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

Class Method Details

.all(response = nil) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/pipedrive/base.rb', line 74

def all(response = nil)
  res = response || get(resource_path)
  if res.ok?
    res['data'].nil? ? [] : res['data'].map{|obj| new(obj)}
  else
    bad_response(res)
  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



56
57
58
# File 'lib/pipedrive/base.rb', line 56

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

.bad_response(response) ⇒ Object

Examines a bad response and raises an appropriate exception

Parameters:

  • response (HTTParty::Response)

Raises:

  • (StandardError)


63
64
65
66
67
68
# File 'lib/pipedrive/base.rb', line 63

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

.create(opts = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/pipedrive/base.rb', line 83

def create( opts = {} )
  res = post resource_path, :body => opts
  if res.success?
    res['data'] = opts.merge res['data']
    new(res)
  else
    bad_response(res)
  end
end

.find(id) ⇒ Object



93
94
95
96
# File 'lib/pipedrive/base.rb', line 93

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

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



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

def find_by_name(name, opts={})
  res = get "#{resource_path}/find", :query => { :term => name }.merge(opts)
  res.ok? ? new_list(res) : bad_response(res)
end

.new_list(attrs) ⇒ Object



70
71
72
# File 'lib/pipedrive/base.rb', line 70

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

.resource_pathObject



103
104
105
# File 'lib/pipedrive/base.rb', line 103

def resource_path
  "/#{name.split('::').last.downcase}s"
end

Instance Method Details

#update(opts = {}) ⇒ Boolean

Updates the object.

Parameters:

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

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/pipedrive/base.rb', line 45

def update(opts = {})
  res = put "#{resource_path}/#{id}", :body => opts
  !!(res.success? && @table.merge!(res['data'].symbolize_keys))
end