ruby_hubspot_api 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 62b690c7e426369cd5979c4f6b5b0cec8b5a7620
4
- data.tar.gz: 26047bb4aa34d3a91bf6c1aff80b6e3775220f4f
2
+ SHA256:
3
+ metadata.gz: 85164b2e72737294737730eea14e12955d3f143f5fadc6a586523b002b1d82fa
4
+ data.tar.gz: 4240f83a2b75521845a0db28a6f7eac72b12542cd0c8c0bd1504f4a5c36c3c00
5
5
  SHA512:
6
- metadata.gz: a6512174b13fa2ef07740690864c79a6c3593c908c1fed37170ef35bf08ca7ae5ceb9a3e53fe12d53079eb4e4bb47a58801430103b96c9a7b78dfd67eaaabaa5
7
- data.tar.gz: 6ea719cb62b217fdbbdb2009d64215369608ba8d3423b59245472fb64fcd957174c5cc28a0cbcbd1f5eaf904260d19e1989da528df913292c139b5f4d893110e
6
+ metadata.gz: d74e992af58345457232d6617c6133e1874a85f98925606a954c86c0a766c198bd5e7d0643e5417d8a21ea195f1aba9d823147123c9cf8d631ac9514a4ed268c
7
+ data.tar.gz: 32be447b0f03a5d86c340f00d1639d61a839c040fbe96789bdaeb7869403fadaf04843167a497e99b6223b0cca4c7e00c62d04ac8554d6ec618db6861cc969f8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby_hubspot_api (0.3.1)
4
+ ruby_hubspot_api (0.3.2)
5
5
  httparty (>= 0.1, < 1.0)
6
6
 
7
7
  GEM
@@ -17,15 +17,17 @@ GEM
17
17
  crack (1.0.0)
18
18
  bigdecimal
19
19
  rexml
20
+ csv (3.3.0)
20
21
  diff-lcs (1.5.1)
21
22
  docile (1.3.5)
22
23
  dotenv (2.8.1)
23
24
  hashdiff (1.1.1)
24
- httparty (0.21.0)
25
+ httparty (0.22.0)
26
+ csv
25
27
  mini_mime (>= 1.0.0)
26
28
  multi_xml (>= 0.5.2)
27
29
  method_source (1.1.0)
28
- mini_mime (1.1.2)
30
+ mini_mime (1.1.5)
29
31
  multi_xml (0.6.0)
30
32
  pry (0.14.2)
31
33
  coderay (~> 1.1)
@@ -80,4 +82,4 @@ DEPENDENCIES
80
82
  webmock (>= 3.0)
81
83
 
82
84
  BUNDLED WITH
83
- 2.2.34
85
+ 2.3.27
data/README.md CHANGED
@@ -310,6 +310,21 @@ companies.each do |company|
310
310
  end
311
311
  ```
312
312
 
313
+ #### Getting the total number of records
314
+
315
+ Having created a search collection you can retrieve the total number of matching records from Hubspot by calling .total on the collection. This will make a single request to the api to retrieve the number of matching records
316
+
317
+ ```ruby
318
+ contacts_collection = Hubspot::Contact.search(query: { lead_status: 'cold' } )
319
+
320
+ if contacts_collection.total > 200
321
+ puts "Contacts will be processed overnight"
322
+ # schedule_processing_job
323
+ else
324
+ puts "Found #{contacts_collection.total} contacts. Processing...."
325
+ process_contacts(contacts_collection)
326
+ ```
327
+
313
328
  #### Specifying Properties in Search
314
329
 
315
330
  When performing a search, you can also specify which properties to return.
@@ -7,7 +7,9 @@ module Hubspot
7
7
  attr_accessor :response
8
8
 
9
9
  def initialize(response, message = nil)
10
- message = response.parsed_response['message'] if !message && response.respond_to?(:parsed_response)
10
+ if !message && response.respond_to?(:parsed_response)
11
+ message = response.parsed_response['message']
12
+ end
11
13
  message += "\n" if message
12
14
  me = super("#{message}Response body: #{response.body}",)
13
15
  me.response = response
@@ -20,6 +22,7 @@ module Hubspot
20
22
  class NotConfiguredError < StandardError; end
21
23
  class ArgumentError < StandardError; end
22
24
  class NothingToDoError < StandardError; end
25
+ class NotImplementedError < StandardError; end
23
26
 
24
27
  class << self
25
28
  def error_from_response(response)
@@ -11,17 +11,24 @@ module Hubspot
11
11
 
12
12
  MAX_LIMIT = 100 # HubSpot max items per page
13
13
 
14
+ # rubocop:disable Lint/MissingSuper
14
15
  def initialize(url:, params: {}, resource_class: nil, method: :get)
15
16
  @url = url
16
17
  @params = params
17
18
  @resource_class = resource_class
18
19
  @method = method.to_sym
19
20
  end
21
+ # rubocop:enable Lint/MissingSuper
22
+
23
+ def total
24
+ @total ||= determine_total
25
+ end
20
26
 
21
27
  def each_page
22
28
  offset = nil
23
29
  loop do
24
30
  response = fetch_page(offset)
31
+ @total = response['total'] if response['total']
25
32
  mapped_results = process_results(response)
26
33
  yield mapped_results unless mapped_results.empty?
27
34
  offset = response.dig('paging', 'next', 'after')
@@ -38,10 +45,12 @@ module Hubspot
38
45
  end
39
46
 
40
47
  # Override Enumerable's first method so as not to have to call each (via all)
48
+ # rubocop:disable Metrics/MethodLength
41
49
  def first(limit = 1)
42
50
  resources = []
43
51
  remaining = limit
44
52
 
53
+ original_limit = @params.delete(:limit)
45
54
  # Modify @params directly to set the limit
46
55
  @params[:limit] = [remaining, MAX_LIMIT].min
47
56
 
@@ -52,8 +61,10 @@ module Hubspot
52
61
  break if remaining <= 0
53
62
  end
54
63
 
64
+ @params[:limit] = original_limit
55
65
  limit == 1 ? resources.first : resources.first(limit)
56
66
  end
67
+ # rubocop:enable Metrics/MethodLength
57
68
 
58
69
  def each(&block)
59
70
  each_page do |page|
@@ -63,6 +74,33 @@ module Hubspot
63
74
 
64
75
  private
65
76
 
77
+ def determine_total
78
+ # We only get a response['total'] for the search endpoint
79
+ raise NotImplementedError, 'Total only available for search requests' unless search_request?
80
+
81
+ # if we don't already know the total we will make a single request and minimise the response
82
+ # size by asking for just one property and one record.
83
+
84
+ # store the current properties
85
+ original_properties = @params.delete(:properties)
86
+
87
+ # just request hs_object_id
88
+ @params[:properties] = ['hs_object_id']
89
+
90
+ # dummy request. @total will be set during the each_page evaluation
91
+ _first_page = first
92
+
93
+ # restore the original properties
94
+ @params[:properties] = original_properties
95
+
96
+ # return the now set total
97
+ @total
98
+ end
99
+
100
+ def search_request?
101
+ @url.include?('/search')
102
+ end
103
+
66
104
  def fetch_page(offset)
67
105
  params_with_offset = @params.dup
68
106
  params_with_offset.merge!(after: offset) if offset
@@ -314,13 +314,8 @@ module Hubspot
314
314
  method: :post
315
315
  )
316
316
  end
317
-
318
317
  # rubocop:enable Metrics/MethodLength
319
318
 
320
- # The root of the api call. Mostly this will be "crm"
321
- # but you can override this to account for a different
322
- # object hierarchy
323
-
324
319
  # Define the resource name based on the class
325
320
  def resource_name
326
321
  name = self.name.split('::').last.downcase
@@ -339,6 +334,9 @@ module Hubspot
339
334
 
340
335
  private
341
336
 
337
+ # The root of the api call. Mostly this will be "crm"
338
+ # but you can override this to account for a different
339
+ # object hierarchy
342
340
  def api_root
343
341
  '/crm/v3/objects'
344
342
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hubspot
4
- VERSION = '0.3.1'
4
+ VERSION = '0.3.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_hubspot_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Brook
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-16 00:00:00.000000000 Z
11
+ date: 2024-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -239,7 +239,7 @@ licenses:
239
239
  metadata:
240
240
  homepage_uri: https://github.com/sensadrome/ruby_hubspot_api
241
241
  changelog_uri: https://github.com/sensadrome/ruby_hubspot_api/blob/main/CHANGELOG.md
242
- post_install_message:
242
+ post_install_message:
243
243
  rdoc_options: []
244
244
  require_paths:
245
245
  - lib
@@ -254,9 +254,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
254
254
  - !ruby/object:Gem::Version
255
255
  version: '0'
256
256
  requirements: []
257
- rubyforge_project:
258
- rubygems_version: 2.6.14
259
- signing_key:
257
+ rubygems_version: 3.1.6
258
+ signing_key:
260
259
  specification_version: 4
261
260
  summary: ruby_hubspot_api is an ORM-like wrapper for the Hubspot API
262
261
  test_files: []