class Pipedrive::Client
Constants
- PIPELINE_HOST
Public Class Methods
new(path, parameters = {})
click to toggle source
# File lib/pipedrive_client.rb, line 10 def initialize(path, parameters = {}) @api_path = path @parameters = parameters @parameters[:query] ||= {} @parameters[:query][:limit] ||= 500 @parameters[:query][:start] ||= 0 end
Public Instance Methods
get()
click to toggle source
# File lib/pipedrive_client.rb, line 18 def get fetch_all end
Private Instance Methods
api_key()
click to toggle source
# File lib/pipedrive_client.rb, line 83 def api_key return @api_key if @api_key return @api_key = ENV['pipedrive_api_key'] if ENV['pipedrive_api_key'] @api_key = YAML.load( File.read( File.expand_path('pipedrive_key.yml', 'config') ) )[:api_key] end
check_limit(headers)
click to toggle source
# File lib/pipedrive_client.rb, line 72 def check_limit(headers) headers =~ /X-RateLimit-Remaining: (\d+)/ return unless Regexp.last_match(1) == '0' headers =~ /X-RateLimit-Reset: (\d+)/ sleep(Regexp.last_match(1).to_i) end
create_api_url()
click to toggle source
# File lib/pipedrive_client.rb, line 24 def create_api_url url = "#{PIPELINE_HOST}#{@api_path}" url += "/#{@parameters[:id]}" if @parameters[:id] url += "/#{@parameters[:method]}" if @parameters[:method] token_query = { api_token: api_key } if @parameters[:query] @parameters[:query].merge!(token_query) else @parameters[:query] = token_query end "#{url}?" \ "#{@parameters[:query].map { |key, value| "#{key}=#{value}" }.join('&')}" end
data_ok?(data)
click to toggle source
# File lib/pipedrive_client.rb, line 79 def data_ok?(data) data.is_a?(Hash) && data['success'] && data['data'] end
fetch()
click to toggle source
# File lib/pipedrive_client.rb, line 38 def fetch url = create_api_url tries = 3 response = nil loop do break if tries.zero? tries -= 1 response = Typhoeus.get(url) response.response_headers =~ /X-RateLimit-Remaining: (\d+)/ check_limit(response.response_headers) next if response.response_code != 200 response = JSON.parse(response.body) break if response['success'] end response end
fetch_all()
click to toggle source
# File lib/pipedrive_client.rb, line 55 def fetch_all all_data = [] loop do response = fetch return nil unless data_ok?(response) data = response['data'] all_data += data.is_a?(Array) ? data : [data] unless response['additional_data'].present? && response['additional_data']['pagination'].present? && response['additional_data']['pagination']['more_items_in_collection'] break end @parameters[:query][:start] += @parameters[:query][:limit] end all_data end