Class: Pipedrive::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/pipedrive/base.rb
Direct Known Subclasses
Activity, ActivityType, Deal, DealField, File, Filter, Goal, Note, Organization, OrganizationField, Person, PersonField, Pipeline, Product, ProductField, Role, Stage, User
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(api_token = ::Pipedrive.api_token) ⇒ Base
Returns a new instance of Base.
5
6
7
8
9
|
# File 'lib/pipedrive/base.rb', line 5
def initialize(api_token = ::Pipedrive.api_token)
raise 'api_token should be set' unless api_token.present?
@api_token = api_token
end
|
Class Method Details
.connection ⇒ Object
This method smells of :reek:TooManyStatements :nodoc
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/pipedrive/base.rb', line 83
def connection
@connection ||= Faraday.new(faraday_options) do |conn|
conn.request :url_encoded
conn.response :mashify
conn.response :json, content_type: /\bjson$/
conn.use FaradayMiddleware::ParseJson
conn.response :logger, ::Pipedrive.logger if ::Pipedrive.debug
conn.adapter Faraday.default_adapter
end
end
|
.faraday_options ⇒ Object
71
72
73
74
75
76
77
78
79
|
# File 'lib/pipedrive/base.rb', line 71
def faraday_options
{
url: 'https://api.pipedrive.com',
headers: {
accept: 'application/json',
user_agent: ::Pipedrive.user_agent
}
}
end
|
Instance Method Details
#build_url(args, fields_to_select = nil) ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/pipedrive/base.rb', line 32
def build_url(args, fields_to_select = nil)
url = +"/v1/#{entity_name}"
url << "/#{args[1]}" if args[1]
url << ":(#{fields_to_select.join(',')})" if fields_to_select.is_a?(::Array) && fields_to_select.size.positive?
url << "?api_token=#{@api_token}"
url
end
|
#connection ⇒ Object
11
12
13
|
# File 'lib/pipedrive/base.rb', line 11
def connection
self.class.connection.dup
end
|
#entity_name ⇒ Object
64
65
66
67
68
|
# File 'lib/pipedrive/base.rb', line 64
def entity_name
class_name = self.class.name.split('::')[-1].downcase.pluralize
class_names = { 'people' => 'persons' }
class_names[class_name] || class_name
end
|
#failed_response(res) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/pipedrive/base.rb', line 52
def failed_response(res)
failed_res = res.body.merge(success: false, not_authorized: false,
failed: false)
case res.status
when 401
failed_res[:not_authorized] = true
when 420
failed_res[:failed] = true
end
failed_res
end
|
#make_api_call(*args) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/pipedrive/base.rb', line 15
def make_api_call(*args)
params = args.
method = args[0]
raise 'method param missing' unless method.present?
url = build_url(args, params.delete(:fields_to_select))
begin
res = connection.__send__(method.to_sym, url, params)
rescue Errno::ETIMEDOUT
retry
rescue Faraday::ParsingError
sleep 5
retry
end
process_response(res)
end
|
#process_response(res) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/pipedrive/base.rb', line 40
def process_response(res)
if res.success?
data = if res.body.is_a?(::Hashie::Mash)
res.body.merge(success: true)
else
::Hashie::Mash.new(success: true)
end
return data
end
failed_response(res)
end
|