pipedrive_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e69fe37113f0a0ff5d41fa6773dcddb86096038a
4
+ data.tar.gz: cbac3a610d896d2f94c021d7b35c1df9b55cab53
5
+ SHA512:
6
+ metadata.gz: 89bfd48433760dae22b18e7f750f90732e40464fc8a0c307674f87977fd3de49e8d37d96592bca22b8ea5ce1b727525db327e6afea7b97f2972972f5755f16ea
7
+ data.tar.gz: aa44cfd0111b5903ce3bdac5f83fc86cf51e24ab6583fdc5df5b9a319780402357e407dd9da04f06bf83eaef680f9fb2658c5200c148f724a8b0f10cfa924189
@@ -0,0 +1,11 @@
1
+ module PipedriveHelpers
2
+ refine Object do
3
+ def blank?
4
+ respond_to?(:empty?) ? !!empty? : !self
5
+ end
6
+
7
+ def present?
8
+ !blank?
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,94 @@
1
+ require 'typhoeus'
2
+ require 'json'
3
+ require 'yaml'
4
+ require 'helpers/pipedrive_helpers'
5
+
6
+ module Pipedrive
7
+ class Client
8
+ using PipedriveHelpers
9
+
10
+ PIPELINE_HOST = 'https://api.pipedrive.com/v1/'
11
+
12
+ def initialize(path, parameters = {})
13
+ @api_path = path
14
+ @parameters = parameters
15
+ @parameters[:query] ||= {}
16
+ @parameters[:query][:limit] ||= 500
17
+ @parameters[:query][:start] ||= 0
18
+ end
19
+
20
+ def get
21
+ fetch_all
22
+ end
23
+
24
+ private
25
+
26
+ def create_api_url
27
+ url = "#{PIPELINE_HOST}#{@api_path}"
28
+ url += "/#{@parameters[:id]}" if @parameters[:id]
29
+ url += "/#{@parameters[:method]}" if @parameters[:method]
30
+ token_query = { api_token: api_key }
31
+ if @parameters[:query]
32
+ @parameters[:query].merge!(token_query)
33
+ else
34
+ @parameters[:query] = token_query
35
+ end
36
+ "#{url}?" \
37
+ "#{@parameters[:query].map { |key, value| "#{key}=#{value}" }.join('&')}"
38
+ end
39
+
40
+ def fetch
41
+ url = create_api_url
42
+ tries = 3
43
+ response = nil
44
+ loop do
45
+ break if tries.zero?
46
+ tries -= 1
47
+ response = Typhoeus.get(url)
48
+ response.response_headers =~ /X-RateLimit-Remaining: (\d+)/
49
+ check_limit(response.response_headers)
50
+ next if response.response_code != 200
51
+ response = JSON.parse(response.body)
52
+ break if response['success']
53
+ end
54
+ response
55
+ end
56
+
57
+ def fetch_all
58
+ all_data = []
59
+ loop do
60
+ response = fetch
61
+ return nil unless data_ok?(response)
62
+ data = response['data']
63
+ all_data += data.is_a?(Array) ? data : [data]
64
+ unless response['additional_data'].present? &&
65
+ response['additional_data']['pagination'].present? &&
66
+ response['additional_data']['pagination']['more_items_in_collection']
67
+ break
68
+ end
69
+ @parameters[:query][:start] += @parameters[:query][:limit]
70
+ end
71
+ all_data
72
+ end
73
+
74
+ def check_limit(headers)
75
+ headers =~ /X-RateLimit-Remaining: (\d+)/
76
+ return unless Regexp.last_match(1) == '0'
77
+ headers =~ /X-RateLimit-Reset: (\d+)/
78
+ sleep(Regexp.last_match(1).to_i)
79
+ end
80
+
81
+ def data_ok?(data)
82
+ data.is_a?(Hash) && data['success'] && data['data']
83
+ end
84
+
85
+ def api_key
86
+ return @api_key if @api_key
87
+ @api_key = YAML.load(
88
+ File.read(
89
+ File.expand_path('pipedrive_key.yml', 'config')
90
+ )
91
+ )[:api_key]
92
+ end
93
+ end
94
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pipedrive_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jan Mosat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.15'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.15'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '12.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '12.0'
61
+ description: Gem for communicating with pipedrive API
62
+ email: mosat@weps.cz
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - lib/helpers/pipedrive_helpers.rb
68
+ - lib/pipedrive_client.rb
69
+ homepage: https://github.com/Lajcisvk/pipedrive_client
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.2.2
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Pipedrive client
93
+ test_files: []
94
+ has_rdoc: