lcp-sdk 1.1.1 → 1.2.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lcp_sdk.rb +46 -3
  3. data/lib/schema-bundle.json +1575 -464
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 703d40a739d0c90ec564532625d80d28c118177c6ffabea4435e00d276a299b1
4
- data.tar.gz: 3b6861a3b2c9203f7daa6948f8bbe94c34849c5f1dd6c3303c7bc019c151cc7d
3
+ metadata.gz: 2359ed7c34ec4f71be5b057ffb66d982c3aecf2d48e23170d6b3fc19681d39f8
4
+ data.tar.gz: 9d726b8476bd74498b545c1bceba8193dca12d16ce4e1a4a62853e4c03aa417f
5
5
  SHA512:
6
- metadata.gz: 162396e876b349241cf03a867480fb9bfd4240a0ce454db072540f173ed70332e57ed982aac35e088c6cb4be823ef487b970773f3941535cba0d6760184cf948
7
- data.tar.gz: 294d1fd238144a806ef492ee55baa2702b5d730cf8728966f3661a0b1a4ad6734c0cb65cedf4b3b30ac75eb3100515f8d43d12bdb2b0abecede91fcad5ea1abd
6
+ metadata.gz: 2b14f1a591894806113ab8610d4135d7f9b260ba9ac448dccaa89888d43036eb89ebcfbc3fdda0d725f33e333f14198ca9464eb08c2359931b5b1bf411b1f084
7
+ data.tar.gz: ef0422c2e69559f3c7a964a9fdf2e14389f2841f6a19c0e475f441ae5c86d34c5a9105c33257aa49ff04c6f86022af7dba6aecde8d04e85aa2b8173c112cc74e
data/lib/lcp_sdk.rb CHANGED
@@ -142,10 +142,10 @@ module LcpSdk
142
142
  @endpoint = endpoint.delete_suffix("/"); @sender_id = sender_id; @api_key = api_key; @hmac_secret = hmac_secret; @max_retries = max_retries
143
143
  end
144
144
 
145
- def request(method, path, payload: nil, test: false)
145
+ def request(method, path, payload: nil, test: false, max_retries: @max_retries)
146
146
  body = payload.nil? ? "" : JSON.generate(payload)
147
147
  key = payload&.dig("lcp", "message", "idempotency_key")
148
- (0..@max_retries).each do |attempt|
148
+ (0..max_retries).each do |attempt|
149
149
  uri = URI.join("#{@endpoint}/", path.delete_prefix("/")); request = Net::HTTP.const_get(method.capitalize).new(uri)
150
150
  request["Content-Type"] = "application/json"; request["Accept"] = "application/json"; request["X-LCP-Sender-Id"] = @sender_id if @sender_id; request["Authorization"] = "Bearer #{@api_key}" if @api_key; request["X-LCP-Idempotency-Key"] = key if key; request["X-LCP-Test"] = "true" if test
151
151
  if @hmac_secret; timestamp = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"); request["X-LCP-Timestamp"] = timestamp; request["X-LCP-Signature"] = Signing.sign(@hmac_secret, timestamp, key, body); end
@@ -153,7 +153,7 @@ module LcpSdk
153
153
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") { |http| http.request(request) }
154
154
  data = response.body.nil? || response.body.empty? ? {} : JSON.parse(response.body)
155
155
  return data if response.is_a?(Net::HTTPSuccess)
156
- raise "LCP HTTP #{response.code}: #{data}" unless [429, 500, 502, 503, 504].include?(response.code.to_i) && attempt < @max_retries
156
+ raise "LCP HTTP #{response.code}: #{data}" unless [429, 500, 502, 503, 504].include?(response.code.to_i) && attempt < max_retries
157
157
  sleep(2**attempt)
158
158
  end
159
159
  raise "LCP request failed"
@@ -162,8 +162,51 @@ module LcpSdk
162
162
  def submit_lead(envelope, test: false); LcpSdk.validate_envelope(envelope); request("POST", "/v1/lcp/leads", payload: envelope, test: test); end
163
163
  def submit_call(envelope, test: false); LcpSdk.validate_envelope(envelope); request("POST", "/v1/lcp/calls", payload: envelope, test: test); end
164
164
  def submit_bid(envelope, test: false); LcpSdk.validate_envelope(envelope); request("POST", "/v1/lcp/bids", payload: envelope, test: test); end
165
+
166
+ # Submit up to MAX_BATCH_ITEMS lead items in one request (LEP-0001 W2-01)
167
+ # and return the per-item receipts in input order. HTTP 207 partial success
168
+ # is a successful response; per-item error objects are preserved. The batch
169
+ # is never retried as a whole — retrying only rejected/error receipts is the
170
+ # caller's job.
171
+ MAX_BATCH_ITEMS = 100
172
+
173
+ def submit_leads_batch(items, request_id: nil)
174
+ unless (1..MAX_BATCH_ITEMS).cover?(items.length)
175
+ raise ArgumentError, "batch supports 1..#{MAX_BATCH_ITEMS} items, got #{items.length}"
176
+ end
177
+ response = request("POST", "/v1/lcp/leads/batch", payload: { "request_id" => request_id || SecureRandom.uuid, "items" => items }, max_retries: 0)
178
+ results = response["results"]
179
+ raise "batch response missing results" unless results.is_a?(Array)
180
+ order = items.each_with_index.to_h { |item, index| [item["item_id"], index] }
181
+ results.each_with_index.sort_by { |result, index| [order.fetch(result["item_id"], items.length), index] }.map(&:first)
182
+ end
165
183
  def query_lead_status(id); request("GET", "/v1/lcp/leads/#{URI.encode_www_form_component(id)}"); end
166
184
  def get_capabilities; request("GET", "/v1/lcp/capabilities"); end
167
185
  def list_offers(vertical = nil); request("GET", "/v1/lcp/offers#{vertical ? "?vertical=#{URI.encode_www_form_component(vertical)}" : ""}"); end
186
+
187
+ # LEP-0005 history read (GET /v1/lcp/history/requests, history:read). Only
188
+ # supplied, non-empty filters reach the query string — a blank filter would
189
+ # widen the page instead of narrowing it. Rows are scoped server-side to the
190
+ # credential's tenant, so there is no tenant argument. Returns the decoded
191
+ # body unchanged.
192
+ HISTORY_PARAMS = %w[limit cursor request_id status vertical caller_phone_hash from to].freeze
193
+
194
+ def request_history(**params)
195
+ query = HISTORY_PARAMS.filter_map do |name|
196
+ value = params[name.to_sym]
197
+ next if value.nil? || value.to_s.empty?
198
+ "#{name}=#{URI.encode_www_form_component(value.to_s)}"
199
+ end
200
+ request("GET", "/v1/lcp/history/requests#{query.empty? ? "" : "?#{query.join("&")}"}")
201
+ end
202
+
203
+ # Fetch one protocol schema by name (GET /v1/lcp/schemas/{name}) and return
204
+ # the decoded body unchanged. Slash namespaces such as "verticals/mortgage"
205
+ # are legitimate, so each segment is encoded separately and the separators
206
+ # are kept.
207
+ def get_schema(name)
208
+ encoded = name.to_s.split("/", -1).map { |segment| URI.encode_www_form_component(segment) }.join("/")
209
+ request("GET", "/v1/lcp/schemas/#{encoded}")
210
+ end
168
211
  end
169
212
  end