hcp 1.3.0 → 1.4.0

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
2
  SHA256:
3
- metadata.gz: 6efa69ce3e07caf79d11e344137caf623856a6c4ea62bdda98730d852df80e5e
4
- data.tar.gz: 10296089173df4f01acca8945fe39cdff3fdaa28f789730fe402b3322624a338
3
+ metadata.gz: 7eda96bf4ffa8ef77fdb696d95c2f47bed39b593ef075051ae40c5fe5f2e64c8
4
+ data.tar.gz: 52b938c9001e14231015447cc203d7ea72899a4ef982d58aa2f41ce88cf99c66
5
5
  SHA512:
6
- metadata.gz: 82f25858ff585025ee7f1efbd8d099114b18a07f2cac3fc8f2f20d4967b8fe46d68f61d5b922d7ebb80146645fb6d47aa4788df96feae706c8310e99c8992098
7
- data.tar.gz: 0333e25614ba5be270978f6b5022ca27e2e4b1ead840e4804aa73b1287f57b5cbc8ee25bf8aa065061abb5e194c069ec62751e9ce281c8c665d9857bcc2578c4
6
+ metadata.gz: 68b998e5fcaaf1656ea9fef6d11a8dff91c4a7a46a4f5b87c3d681dac2f99b4a10b4eae8cb2b115c2f272ce100667cfed87c83700fdd778380c2370ea877f8e9
7
+ data.tar.gz: 694e677888b6b317779b07f11f9f8644ac1b8e68fd8643b2a3bd300057cb43c1a696c05d844ed25f4310a0eba465824c33ea459ba73b341f3eeb1378ee3c349c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.4.0] - 2026-08-28
4
+
5
+ - [Feature] Read as a key for one block, on one thread: `Hcp.with_key(key, company_id:)`,
6
+ which hands the block an `Hcp::Access` answering `account`. A process serving several
7
+ accounts held them all on one `Hcp.key`, so two threads could read each other's; a key
8
+ handed to a block is the thread's alone, and the one set before it comes back after.
9
+
10
+ - [Feature] Read the account a key belongs to: `Hcp::Company.current`, taking `company_id:`.
11
+ There is no list of companies and no ID to find one by, so it is `current` rather than
12
+ `find` or `where`. `locations` is the account itself and then every location under it at
13
+ any depth, flat, so it is never empty; each ID is what `company_id:` takes everywhere else.
14
+
15
+ - [Fix] `Hcp::Address#latitude` and `#longitude` answer a `Float` whichever endpoint they were
16
+ read from. Housecall Pro stamps them as numbers under a customer and as strings under the
17
+ company, and the gem documented `Float` while handing back whatever arrived.
18
+
19
+ - [Feature] Read when the account is free to be booked into: `Hcp::BookingWindow.all`, taking
20
+ `starts_at:`, `days:`, `minutes:`, `service_id:`, `price_form_id:` and `employee_ids:`.
21
+ Housecall Pro answers this list whole rather than a page at a time, so it comes back as an
22
+ `Array` rather than as a relation.
23
+
3
24
  ## [1.3.0] - 2026-08-25
4
25
 
5
26
  - [Feature] Read customers, estimates, jobs and job appointments as an Active Record relation:
@@ -12,6 +33,8 @@
12
33
  - [Feature] Raise `Hcp::NotFound` where Housecall Pro has no such record, and
13
34
  `Hcp::TooManyRequests`, carrying `reset_at`, where it refuses one for rate. Both descend
14
35
  from `Hcp::Error`, so an existing rescue still catches them.
36
+ - [Fix] `require 'hcp'` defines `Hcp::VERSION`, which until now was only set as a side effect
37
+ of Bundler evaluating the gemspec.
15
38
  - [Breaking change] `Hcp::Lead#lead_for`, `#customer_for` and `#uri` are now private, and
16
39
  neither `Hcp::Lead` nor `Hcp::Lead::Pipeline` inherits from `Hcp::Resource`, which is now a
17
40
  record rather than a holder of credentials. Nothing documented ever called them.
data/CLAUDE.md ADDED
@@ -0,0 +1,43 @@
1
+ # hcp
2
+
3
+ A Ruby client for the Housecall Pro API.
4
+
5
+ ## Follow the coding guidelines
6
+
7
+ https://raw.githubusercontent.com/HouseAccountEng/guidelines/refs/heads/main/STYLE.md
8
+
9
+ Read them before writing, not after review. They are stricter than this code alone suggests: one
10
+ line of comment above every public declaration and none above a private one, no metaprogramming
11
+ except on an explicit instruction, no method that nothing calls, no rescue for an error that has
12
+ never happened, and a cap of 100 lines to a file and 50 Ruby files to a folder — both enforced by
13
+ `bundle exec rake`, which is the gate for everything here.
14
+
15
+ Two rules are easy to miss and expensive to undo: a commit message carries no trailer naming its
16
+ author, and a CHANGELOG entry says which of fix, feature or breaking change it is, because that is
17
+ what picks the version.
18
+
19
+ ## Probe the API rather than trust its spec
20
+
21
+ The authoritative OpenAPI spec is at
22
+
23
+ https://stoplight.io/api/v1/projects/housecallpro/housecall-public-api/nodes/reference/housecall.v1.yaml
24
+
25
+ docs.housecallpro.com renders it client-side, so fetching that HTML gets nothing worth reading.
26
+
27
+ **The spec disagrees with the live API.** Send a real request before writing code against a
28
+ documented shape. Six disagreements found so far, each of which would have shipped a bug:
29
+
30
+ - `page_size` is capped at 200. The spec publishes no maximum.
31
+ - `GET /jobs/{id}/line_items` answers `{"object":"list","data":[…]}`, not the documented
32
+ `{url, data}`.
33
+ - A refusal comes back three ways — `{"error":{"message":…}}`, `{"error":"…"}` and `{"message":…}`.
34
+ The spec describes only the first.
35
+ - An unknown filter is **ignored, not refused**, so a typo answers the whole account rather than a
36
+ page of it. This is why the gem checks condition names itself.
37
+ - `GET /company` answers `default_arrival_window` as an integer. The spec publishes it as a string.
38
+ - `GET /company` stamps an address's `latitude` and `longitude` as **strings**, where
39
+ `GET /customers/{id}/addresses` answers the same fields as numbers.
40
+
41
+ A company-scoped key refuses `X-Company-Id` with a 401 on every endpoint, so the header can only
42
+ be exercised with an application key. `GET /company` answers `locations` only to the latter, and
43
+ nests: a location holds locations of its own, several levels deep.
data/README.md CHANGED
@@ -15,7 +15,7 @@ gem install hcp
15
15
  Or, in a Gemfile, pinned to the current major:
16
16
 
17
17
  ```ruby
18
- gem 'hcp', '~> 1.3'
18
+ gem 'hcp', '~> 1.4'
19
19
  ```
20
20
 
21
21
  `~> major.minor` means `bundle update` never crosses a breaking change.
@@ -28,6 +28,18 @@ Set the key once. Left unset, the gem reads `HCP_KEY` from the environment:
28
28
  Hcp.key = ENV['HCP_KEY']
29
29
  ```
30
30
 
31
+ A process serving several accounts hands each thread its own key for as long as a block runs,
32
+ and is handed back the account that key opens:
33
+
34
+ ```ruby
35
+ Hcp.with_key(key, company_id: location_id) do |access|
36
+ access.account.name # the location, as `Hcp::Company.current` would read it
37
+ Hcp::Job.limit 10 # every read inside the block is this key's
38
+ end
39
+ ```
40
+
41
+ Neither thread sees the other's key, and the one set before the block comes back after it.
42
+
31
43
  An account with more than one location passes `company_id:` per call, since one process may
32
44
  serve several:
33
45
 
@@ -36,6 +48,8 @@ Hcp::Job.find id, company_id: company_id
36
48
  Hcp::Job.all(company_id: company_id).where(work_status: :scheduled)
37
49
  ```
38
50
 
51
+ `Hcp::Company.current.locations` is where those IDs come from.
52
+
39
53
  ## Reading
40
54
 
41
55
  A list is walked lazily, a page read only once the one before it runs out, so asking for three
@@ -96,6 +110,60 @@ estimate.estimate_number, estimate.options.map(&:total_amount)
96
110
 
97
111
  Housecall Pro counts money in cents; this gem reads it in dollars, as a `BigDecimal`.
98
112
 
113
+ ## The company
114
+
115
+ The account the key belongs to. There is no list of companies to narrow and no ID to look one
116
+ up by — a key reads its own account and nothing else — so it is read with `current` rather than
117
+ with `find` or `where`:
118
+
119
+ ```ruby
120
+ company = Hcp::Company.current
121
+ company.name, company.phone, company.support_email, company.website, company.logo_url
122
+ company.time_zone, company.arrival_window, company.address.city, company.zip_codes
123
+ ```
124
+
125
+ `arrival_window` is how many minutes wide a customer's window is by default, and `zip_codes`
126
+ are the ones the account will travel to.
127
+
128
+ `locations` is never empty: it is the account itself, then every location under it at any
129
+ depth, in the order Housecall Pro lists them. A single company is its own one location, and a
130
+ franchise reads flat however deep Housecall Pro nests it:
131
+
132
+ ```ruby
133
+ Hcp::Company.current.locations.map(&:name)
134
+ # => ['Acme Handyman HQ', 'Acme Handyman East', 'Acme Handyman of Springfield', …]
135
+ ```
136
+
137
+ Each location's `id` is what `company_id:` takes, here and everywhere else:
138
+
139
+ ```ruby
140
+ Hcp::Company.current company_id: location.id
141
+ ```
142
+
143
+ ## Booking windows
144
+
145
+ When the account is free to be booked into, as its Online Booking settings answer it. Housecall
146
+ Pro hands this list back whole rather than a page at a time, so it comes back as an `Array`
147
+ rather than as a relation — there is nothing left to narrow, order or cut afterwards.
148
+
149
+ ```ruby
150
+ Hcp::BookingWindow.all
151
+ Hcp::BookingWindow.all starts_at: Date.tomorrow, days: 14
152
+ Hcp::BookingWindow.all employee_ids: [ employee.id ]
153
+ Hcp::BookingWindow.all service_id: id, minutes: 90
154
+ ```
155
+
156
+ Each window says when it opens, when it closes, and whether it is free:
157
+
158
+ ```ruby
159
+ free = Hcp::BookingWindow.all(days: 3).select(&:available?)
160
+ free.map { |window| [ window.starts_at, window.ends_at ] }
161
+ ```
162
+
163
+ Left out, `starts_at` is the next day holding a free window and `days` is seven. A window is
164
+ cut to the service's own duration where `service_id` names one, to `minutes` where that is
165
+ given, and to thirty minutes otherwise.
166
+
99
167
  ## Errors
100
168
 
101
169
  Everything descends from `Hcp::Error`, so one rescue still catches the lot.
data/lib/hcp/access.rb ADDED
@@ -0,0 +1,12 @@
1
+ module Hcp
2
+ # What a key opens: the account it belongs to, read as one of its locations.
3
+ class Access
4
+ # @param company_id [String, nil] the location to read as, where the account has several.
5
+ def initialize(company_id: nil)
6
+ @company_id = company_id
7
+ end
8
+
9
+ # @return [Company] the account the key belongs to, as the location it is read as.
10
+ def account = Company.current company_id: @company_id
11
+ end
12
+ end
data/lib/hcp/key.rb CHANGED
@@ -1,9 +1,24 @@
1
1
  module Hcp
2
2
  class << self
3
- # The Housecall Pro API key every request is read with.
3
+ # The Housecall Pro API key every request is read with, where a thread holds none of its own.
4
4
  attr_writer :key
5
5
 
6
- # @return [String, nil] the key this module was given, or the one the environment carries.
7
- def key = @key || ENV['HCP_KEY']
6
+ # @return [String, nil] the key this thread was handed, the one this module was given, or
7
+ # the one the environment carries.
8
+ def key = ActiveSupport::IsolatedExecutionState[:hcp_key] || @key || ENV['HCP_KEY']
9
+
10
+ # Reads as this key for the block, on this thread alone, so a process serving several
11
+ # accounts can hold a key on every thread without any of them seeing another's.
12
+ # @param key [String] the key to read with.
13
+ # @param company_id [String, nil] the location to read as, where the account has several.
14
+ # @yieldparam access [Access] the account the key opens.
15
+ # @return [Object] what the block answered.
16
+ def with_key(key, company_id: nil)
17
+ previous = ActiveSupport::IsolatedExecutionState[:hcp_key]
18
+ ActiveSupport::IsolatedExecutionState[:hcp_key] = key
19
+ yield Access.new(company_id: company_id)
20
+ ensure
21
+ ActiveSupport::IsolatedExecutionState[:hcp_key] = previous
22
+ end
8
23
  end
9
24
  end
@@ -11,11 +11,12 @@ module Hcp
11
11
  attribute :zip
12
12
  attribute :country
13
13
 
14
+ # Housecall Pro answers these as numbers under a customer and as strings under the company.
14
15
  # @return [Float, nil] how far north the address is, where Housecall Pro placed it.
15
- attribute :latitude
16
+ def latitude = @node['latitude']&.to_f
16
17
 
17
18
  # @return [Float, nil] how far east the address is, where Housecall Pro placed it.
18
- attribute :longitude
19
+ def longitude = @node['longitude']&.to_f
19
20
 
20
21
  # @return [Symbol, nil] :billing or :service.
21
22
  def type = @node['type']&.to_sym
@@ -0,0 +1,52 @@
1
+ module Hcp
2
+ # A slot the account's online booking settings leave open for work to be booked into.
3
+ class BookingWindow < Resource
4
+ # How Housecall Pro writes the moment a range is looked at from.
5
+ STAMP = '%Y-%m-%dT%H:%M:%S'
6
+
7
+ class << self
8
+ # Where Housecall Pro keeps them.
9
+ def path = 'company/schedule_availability/booking_windows'
10
+
11
+ # What Housecall Pro calls a list of them.
12
+ def key = 'booking_windows'
13
+
14
+ # Housecall Pro answers these whole rather than a page at a time, so they are read in one
15
+ # request rather than walked, and there is no list left to narrow, order or cut.
16
+ # @param starts_at [Date, Time, nil] where to look from; the next day holding a free
17
+ # window where left out.
18
+ # @param days [Integer, nil] how many days of the schedule to look at; seven where left out.
19
+ # @param minutes [Integer, nil] how wide to cut each window; the service's own duration,
20
+ # or thirty minutes, where left out.
21
+ # @param service_id [String, nil] the service whose assigned pros to look at.
22
+ # @param price_form_id [String, nil] the price form whose assigned pros to look at.
23
+ # @param employee_ids [Array<String>, nil] the pros to look at, rather than all of them.
24
+ # @param company_id [String, nil] the location to read as, where the account has several.
25
+ # @return [Array<BookingWindow>] every window in the range, free and taken alike.
26
+ def all(starts_at: nil, days: nil, minutes: nil, service_id: nil, price_form_id: nil,
27
+ employee_ids: nil, company_id: nil)
28
+ params = { start_date: stamp(starts_at), show_for_days: days, service_id: service_id,
29
+ service_duration: minutes, price_form_id: price_form_id, employee_ids: employee_ids, }
30
+ read(params, company_id).fetch(key).
31
+ map { |node| new node: node, company_id: company_id }
32
+ end
33
+
34
+ private
35
+
36
+ def stamp(value) = value&.strftime(STAMP)
37
+
38
+ def read(params, company_id)
39
+ Request.new(path: path, params: params, company_id: company_id).body
40
+ end
41
+ end
42
+
43
+ # @return [Time, nil] when the window opens.
44
+ timestamp :starts_at, :start_time
45
+
46
+ # @return [Time, nil] when the window closes.
47
+ timestamp :ends_at, :end_time
48
+
49
+ # @return [Boolean] whether the account is free to take work in the window.
50
+ def available? = @node['available']
51
+ end
52
+ end
@@ -0,0 +1,42 @@
1
+ module Hcp
2
+ # The account a key belongs to, and the settings every job it holds is booked under.
3
+ class Company < Resource
4
+ # Where Housecall Pro keeps it.
5
+ def self.path = 'company'
6
+
7
+ # The only company a key can read is its own, so there is no list and no ID to find one by.
8
+ # @param company_id [String, nil] the location to read as, where the account has several.
9
+ # @return [Company] the account, as the location it was read as.
10
+ def self.current(company_id: nil)
11
+ node = Request.new(path: path, company_id: company_id).body
12
+ new node: node, company_id: company_id
13
+ end
14
+
15
+ attribute :name
16
+ attribute :website
17
+ attribute :time_zone
18
+
19
+ # @return [String, nil] where the account's logo is served from.
20
+ attribute :logo_url
21
+
22
+ # @return [String, nil] the number the account is reached on.
23
+ attribute :phone, :phone_number
24
+
25
+ # @return [String, nil] the address a customer's reply goes to.
26
+ attribute :support_email
27
+
28
+ # @return [Integer, nil] how many minutes wide the window a customer is given is, by default.
29
+ attribute :arrival_window, :default_arrival_window
30
+
31
+ # @return [Address, nil] where the account is run from.
32
+ def address = record Address, 'address'
33
+
34
+ # @return [Array<String>] the ZIP codes the account will travel to.
35
+ def zip_codes = Array(@node.dig('service_areas_data', 'zip_codes'))
36
+
37
+ # Itself first, then every location under it at any depth, in the order Housecall Pro
38
+ # lists them: a single company is its own one location, and a franchise reads flat.
39
+ # @return [Array<Company>] the locations whose IDs `company_id:` takes, this one included.
40
+ def locations = [ self, *records(Company, 'locations').flat_map(&:locations) ]
41
+ end
42
+ end
data/lib/hcp/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # Ruby client for the Housecall Pro API.
2
2
  module Hcp
3
3
  # Current version of the gem.
4
- VERSION = '1.3.0'
4
+ VERSION = '1.4.0'
5
5
  end
data/lib/hcp.rb CHANGED
@@ -5,11 +5,12 @@ require 'net/http'
5
5
  require 'time'
6
6
 
7
7
  # Only the Active Support files whose methods are used, rather than the whole of it: a name
8
- # Housecall Pro holds nothing for arrives as readily empty as null, and its queries carry
9
- # arrays in the bracketed form `to_query` writes.
8
+ # Housecall Pro holds nothing for arrives as readily empty as null, its queries carry arrays
9
+ # in the bracketed form `to_query` writes, and a key handed to one thread stays on it.
10
10
  require 'active_support/core_ext/enumerable'
11
11
  require 'active_support/core_ext/object/blank'
12
12
  require 'active_support/core_ext/object/to_query'
13
+ require 'active_support/isolated_execution_state'
13
14
 
14
15
  require 'hcp/version'
15
16
  require 'hcp/error'
@@ -34,6 +35,8 @@ require 'hcp/resources/schedule'
34
35
  require 'hcp/concerns/scheduled'
35
36
 
36
37
  require 'hcp/resources/address'
38
+ require 'hcp/resources/booking_window'
39
+ require 'hcp/resources/company'
37
40
  require 'hcp/resources/employee'
38
41
  require 'hcp/resources/line_item'
39
42
  require 'hcp/resources/note'
@@ -47,6 +50,9 @@ require 'hcp/resources/job/invoice'
47
50
  require 'hcp/resources/estimate'
48
51
  require 'hcp/resources/estimate/option'
49
52
 
53
+ # After Company, which is what a key opens.
54
+ require 'hcp/access'
55
+
50
56
  require 'hcp/concerns/keyed'
51
57
  require 'hcp/lead'
52
58
  require 'hcp/lead/pipeline'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hcp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claudio Baccigalupo
@@ -116,9 +116,11 @@ extra_rdoc_files: []
116
116
  files:
117
117
  - ".yardopts"
118
118
  - CHANGELOG.md
119
+ - CLAUDE.md
119
120
  - LICENSE.txt
120
121
  - README.md
121
122
  - lib/hcp.rb
123
+ - lib/hcp/access.rb
122
124
  - lib/hcp/answer.rb
123
125
  - lib/hcp/concerns/chainable.rb
124
126
  - lib/hcp/concerns/keyed.rb
@@ -139,6 +141,8 @@ files:
139
141
  - lib/hcp/request.rb
140
142
  - lib/hcp/resource.rb
141
143
  - lib/hcp/resources/address.rb
144
+ - lib/hcp/resources/booking_window.rb
145
+ - lib/hcp/resources/company.rb
142
146
  - lib/hcp/resources/customer.rb
143
147
  - lib/hcp/resources/employee.rb
144
148
  - lib/hcp/resources/estimate.rb