Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: amoniacou/pipedrive.rb
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: didww/pipedrive.rb
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 6 commits
  • 6 files changed
  • 2 contributors

Commits on Oct 12, 2023

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    45cc4de View commit details
  2. Merge pull request #1 from didww/add-webhook-resource-and-retry-when-…

    …token-stale
    
    Added webhook resource and refreshing access token
    vvitto authored Oct 12, 2023
    Copy the full SHA
    f4df2c6 View commit details

Commits on Dec 12, 2023

  1. Copy the full SHA
    4a12360 View commit details
  2. Merge pull request #2 from didww/add-client-options-to-contsructor-ar…

    …guments
    
    Add client options to constructor arguments
    vvitto authored Dec 12, 2023
    Copy the full SHA
    de1d7fa View commit details

Commits on Feb 19, 2024

  1. Add Call Log resource

    Bohdan Paratskyi committed Feb 19, 2024
    Copy the full SHA
    c2f83b6 View commit details

Commits on Feb 20, 2024

  1. Merge pull request #3 from paratskyi/add-call-log-resource

    Add Call Log resource
    vvitto authored Feb 20, 2024
    Copy the full SHA
    8f5d93c View commit details
Showing with 103 additions and 31 deletions.
  1. +7 −0 lib/pipedrive.rb
  2. +60 −30 lib/pipedrive/base.rb
  3. +11 −0 lib/pipedrive/call_log.rb
  4. +1 −1 lib/pipedrive/version.rb
  5. +11 −0 lib/pipedrive/webhook.rb
  6. +13 −0 spec/lib/pipedrive/call_log_spec.rb
7 changes: 7 additions & 0 deletions lib/pipedrive.rb
Original file line number Diff line number Diff line change
@@ -98,3 +98,10 @@ def logger

# Pipelines
require 'pipedrive/pipeline'

# Webhooks
require 'pipedrive/webhook'

# Call Logs
require 'pipedrive/call_log'

90 changes: 60 additions & 30 deletions lib/pipedrive/base.rb
Original file line number Diff line number Diff line change
@@ -2,14 +2,17 @@

module Pipedrive
class Base
def initialize(api_token = ::Pipedrive.api_token)
raise 'api_token should be set' unless api_token.present?
attr_reader :faraday_options, :client_options

@api_token = api_token
end

def connection
self.class.connection.dup
def initialize(*args)
options = args.extract_options!
@client_id = options[:client_id]
@client_secret = options[:client_secret]
@access_token = options[:access_token]
@refresh_token = options[:refresh_token]
@domain_url = options[:domain_url]
@authentication_callback = options[:authentication_callback]
@client_options = options[:client_options] || {}
end

def make_api_call(*args)
@@ -19,22 +22,48 @@ def make_api_call(*args)

url = build_url(args, params.delete(:fields_to_select))
params = params.to_json unless method.to_sym == :get
params = nil if method.to_sym == :delete
can_refresh = true
begin
res = connection.__send__(method.to_sym, url, params)
rescue Errno::ETIMEDOUT
retry
rescue Faraday::ParsingError
sleep 5
retry
rescue Faraday::UnauthorizedError
refresh_access_token if can_refresh
can_refresh = false
retry
end

process_response(res)
end

def refresh_access_token
res = connection.post 'https://oauth.pipedrive.com/oauth/token' do |req|
req.body = URI.encode_www_form(
grant_type: 'refresh_token',
refresh_token: @refresh_token
)
req.headers = {
'Authorization': "Basic #{Base64.strict_encode64("#{@client_id}:#{@client_secret}").strip}",
'Content-Type': 'application/x-www-form-urlencoded'
}
end

if res.success?
@access_token = res.body.access_token
@refresh_token = res.body.refresh_token
@connection = nil # clear connection in cache to use new tokens
@authentication_callback.call(res.body) if @authentication_callback.present?
end
end

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

@@ -64,33 +93,34 @@ def failed_response(res)

def entity_name
class_name = self.class.name.split('::')[-1].downcase.pluralize
class_names = { 'people' => 'persons' }
class_names = { 'people' => 'persons', 'calllogs' => 'callLogs' }
class_names[class_name] || class_name
end

class << self
def faraday_options
{
url: 'https://api.pipedrive.com',
headers: {
accept: 'application/json',
content_type: 'application/json',
user_agent: ::Pipedrive.user_agent
}
def faraday_options
{
url: @domain_url,
headers: {
authorization: "Bearer #{@access_token}",
accept: 'application/json',
content_type: 'application/json',
user_agent: ::Pipedrive.user_agent
}
end
}.merge(client_options)
end

# This method smells of :reek:TooManyStatements
# :nodoc
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

# This method smells of :reek:TooManyStatements
# :nodoc
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.response :raise_error
conn.adapter Faraday.default_adapter
end
end
end
11 changes: 11 additions & 0 deletions lib/pipedrive/call_log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Pipedrive
class CallLog < Base
include ::Pipedrive::Operations::Read
include ::Pipedrive::Operations::Create
include ::Pipedrive::Operations::Update
include ::Pipedrive::Operations::Delete
include ::Pipedrive::Utils
end
end
2 changes: 1 addition & 1 deletion lib/pipedrive/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Pipedrive
VERSION = '0.3.0'
VERSION = '0.3.3'
end
11 changes: 11 additions & 0 deletions lib/pipedrive/webhook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Pipedrive
class Webhook < Base
include ::Pipedrive::Operations::Read
include ::Pipedrive::Operations::Create
include ::Pipedrive::Operations::Update
include ::Pipedrive::Operations::Delete
include ::Pipedrive::Utils
end
end
13 changes: 13 additions & 0 deletions spec/lib/pipedrive/call_log_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ::Pipedrive::CallLog do
subject { described_class.new('token') }

describe '#entity_name' do
subject { super().entity_name }

it { is_expected.to eq('call_logs') }
end
end