Class: PipedriveAPI::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/pipedrive_api/base.rb

Class Method Summary collapse

Class Method Details

.all(**params) ⇒ Object



19
20
21
22
23
# File 'lib/pipedrive_api/base.rb', line 19

def all(**params)
  # TODO pagination
  response = get "#{resource_path}", query: params
  handle response
end

.auth(api_token:, account_name: "api") ⇒ Object



14
15
16
17
# File 'lib/pipedrive_api/base.rb', line 14

def auth(api_token:, account_name: "api")
  default_params api_token: api_token
  base_uri "https://#{}.pipedrive.com/#{API_VERSION}/"
end

.create(**params) ⇒ Object



35
36
37
38
# File 'lib/pipedrive_api/base.rb', line 35

def create(**params)
  response = post resource_path, body: params.to_json
  handle response
end

.find(id) ⇒ Object



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

def find(id)
  response = get "#{resource_path}/#{id}"
  handle response
end

.find_by_name(name, **params) ⇒ Object



30
31
32
33
# File 'lib/pipedrive_api/base.rb', line 30

def find_by_name(name, **params)
  response = get "#{resource_path}/find", query: { term: name }.merge(params)
  handle response
end

.handle(response) ⇒ Object



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

def handle response
  case response.code
  when 200..299
    response['data']
  when 403..404
    raise HTTParty::Error, response.parsed_response['error']
  when 500..600
    raise HTTParty::Error, response.parsed_response['error']
  else
    raise StandardError, 'Unknown error'
  end
end

.remove(id) ⇒ Object



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

def remove(id)
  response = delete "#{resource_path}/#{id}"
  handle response
end

.resource_pathObject



50
51
52
53
54
55
56
# File 'lib/pipedrive_api/base.rb', line 50

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

.update(id, **params) ⇒ Object



40
41
42
43
# File 'lib/pipedrive_api/base.rb', line 40

def update(id, **params)
  response = put "#{resource_path}/#{id}", body: params.to_json
  handle response
end