schematichq 1.5.8 → 1.5.9

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 (47) hide show
  1. checksums.yaml +4 -4
  2. data/.fern/metadata.json +5 -5
  3. data/.fern/replay.lock +7 -1
  4. data/README.md +7 -1
  5. data/lib/schematic/billing/client.rb +228 -0
  6. data/lib/schematic/billing/types/convert_company_trial_response.rb +15 -0
  7. data/lib/schematic/billing/types/create_billing_subscription_request_body.rb +2 -0
  8. data/lib/schematic/billing/types/end_company_trial_response.rb +15 -0
  9. data/lib/schematic/billing/types/get_company_trial_response.rb +15 -0
  10. data/lib/schematic/billing/types/start_company_trial_request_body.rb +21 -0
  11. data/lib/schematic/billing/types/start_company_trial_response.rb +15 -0
  12. data/lib/schematic/billing/types/update_company_trial_request_body.rb +15 -0
  13. data/lib/schematic/billing/types/update_company_trial_response.rb +15 -0
  14. data/lib/schematic/cache.rb +19 -2
  15. data/lib/schematic/checkout/types/checkout_data_request_body.rb +2 -0
  16. data/lib/schematic/client.rb +1 -1
  17. data/lib/schematic/datastream/client.rb +52 -22
  18. data/lib/schematic/datastream/resource_cache.rb +13 -3
  19. data/lib/schematic/internal/http/raw_client.rb +1 -1
  20. data/lib/schematic/internal/iterators/offset_page_iterator.rb +2 -0
  21. data/lib/schematic/redis_cache.rb +10 -1
  22. data/lib/schematic/schematic_client.rb +21 -3
  23. data/lib/schematic/types/billing_customer_subscription.rb +2 -0
  24. data/lib/schematic/types/billing_native_subscription_response_data.rb +4 -0
  25. data/lib/schematic/types/billing_product_for_subscription_response_data.rb +2 -0
  26. data/lib/schematic/types/billing_product_plan_response_data.rb +2 -0
  27. data/lib/schematic/types/company_trial_response_data.rb +35 -0
  28. data/lib/schematic/types/company_trial_status.rb +16 -0
  29. data/lib/schematic/types/credit_ledger_export_metadata.rb +25 -0
  30. data/lib/schematic/types/credit_ledger_export_metadata_event_type.rb +19 -0
  31. data/lib/schematic/types/data_export_metadata.rb +2 -0
  32. data/lib/schematic/types/data_export_type.rb +1 -0
  33. data/lib/schematic/types/manage_plan_request.rb +2 -0
  34. data/lib/schematic/types/manage_plan_response_response_data.rb +2 -0
  35. data/lib/schematic/types/migration_error_code.rb +1 -0
  36. data/lib/schematic/types/plan_change_action.rb +1 -0
  37. data/lib/schematic/types/plan_change_response_data.rb +4 -0
  38. data/lib/schematic/types/publish_plan_version_request_body.rb +4 -0
  39. data/lib/schematic/types/publish_preview_interval_change_response_data.rb +13 -0
  40. data/lib/schematic/types/publish_preview_response_data.rb +2 -0
  41. data/lib/schematic/types/scheduled_downgrade_response_data.rb +4 -0
  42. data/lib/schematic/types/trial_source.rb +15 -0
  43. data/lib/schematic/types/web_scheduled_downgrade_webhook_output.rb +2 -0
  44. data/lib/schematic/version.rb +1 -1
  45. data/lib/schematic.rb +13 -0
  46. data/reference.md +316 -0
  47. metadata +15 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 80f63dc869707208166ab3f8707ac94ee36adc0e3e76c6b034fc1510b77badd0
4
- data.tar.gz: 869c5a1dd5d37de4d48bb20aa7f849c9ee8a90e3a6a8a24e088334b93f5b6593
3
+ metadata.gz: 68047a14bbe3275b501f21b2e1ae979b70ad3ebbc7dfb71429c90c3c01a9f305
4
+ data.tar.gz: 36a872072164d60d8f68c3785d41e4b42bef180bf05a31f97328a3c6fb955410
5
5
  SHA512:
6
- metadata.gz: 89c064c6ff4e83963fe5d7ae8db2d5cbb1dacbb0eadbab49357d712ec22abae313368f1af4440b2f813e13f0f2878bc0cba471afd7d805b5ed48a40e3aa6135b
7
- data.tar.gz: 3ee9e07c16578e59b482abd7241b0ad4b5476adb7f5dd85d7566721efd9328bdf533a0ac91544d1e3572b7dfc5d01caf8ac0f6c2f27841bac9652ab89d9ed1c0
6
+ metadata.gz: 208c33eab26fbcdcc1a0f01e294ac1fc7eb30e4fe97367f886696616dc2a5ff601a6b45b7455093f1c132cfe0f3afc78ed4662946ecb1dff1c730bcd7b9ef36f
7
+ data.tar.gz: ab940bf3ce9bde7dd5adf6a7a940d4854f196bc3c386a46b1d4297328686961007173bdd6bac23fc9c1ab13dbcaa7f993d559b4046150889590e2065f13a4892
data/.fern/metadata.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
- "cliVersion": "5.50.0",
2
+ "cliVersion": "5.215.1",
3
3
  "generatorName": "fernapi/fern-ruby-sdk",
4
- "generatorVersion": "1.41.3",
4
+ "generatorVersion": "1.43.1",
5
5
  "generatorConfig": {
6
6
  "module": "SchematicHQ",
7
7
  "rubocopVariableNumberStyle": "disabled",
@@ -13,10 +13,10 @@
13
13
  "webrick": ">= 1.0"
14
14
  }
15
15
  },
16
- "originGitCommit": "bb8b74d2a37ba58ca0f0906f1154394af6021c6f",
16
+ "originGitCommit": "47e820113906faef63cb69ca180aab7ff64df91b",
17
17
  "originGitCommitIsDirty": false,
18
18
  "invokedBy": "ci",
19
- "requestedVersion": "1.5.8",
19
+ "requestedVersion": "1.5.9",
20
20
  "ciProvider": "github",
21
- "sdkVersion": "1.5.8"
21
+ "sdkVersion": "1.5.9"
22
22
  }
data/.fern/replay.lock CHANGED
@@ -150,5 +150,11 @@ generations:
150
150
  cli_version: unknown
151
151
  generator_versions:
152
152
  fernapi/fern-ruby-sdk: 1.41.3
153
- current_generation: 6203732456bfc5b0f3a341ca9c51f838ac1b0c1c
153
+ - commit_sha: d1026be02eb8c1fbcec2d001e79bed2083c9d589
154
+ tree_hash: 58b172bea0c2808ba561718a7c3239c5024b6e83
155
+ timestamp: 2026-11-16T10:02:08.297Z
156
+ cli_version: unknown
157
+ generator_versions:
158
+ fernapi/fern-ruby-sdk: 1.43.1
159
+ current_generation: d1026be02eb8c1fbcec2d001e79bed2083c9d589
154
160
  patches: []
data/README.md CHANGED
@@ -648,6 +648,8 @@ is_flag_on = client.check_flag(
648
648
  client.close
649
649
  ```
650
650
 
651
+ With DataStream enabled (including Replicator Mode), `track` also adds the event's quantity to the matching metric on the cached company right away, so usage-based limits reflect it before Schematic sends back an updated value. This happens even while the WebSocket is disconnected or the replicator reports not ready. The next company update from Schematic or the replicator replaces the local value, so usage is never counted twice. Companies that aren't in the cache are left alone.
652
+
651
653
  ### Configuration Options
652
654
 
653
655
  You can customize DataStream behavior via the `datastream_options` hash:
@@ -726,7 +728,11 @@ When running in Replicator Mode, the client will:
726
728
  - Skip establishing WebSocket connections
727
729
  - Periodically check if the replicator service is ready
728
730
  - Use cached data populated by the external replicator service
729
- - Fall back to direct API calls if the replicator is not available
731
+ - Fall back to direct API calls until the replicator reports its cache ready
732
+
733
+ The client serves flag checks from the cache only once the replicator reports that its cache is ready. The replicator's health endpoint returns `ready: true` once it has fully loaded the cache for its current `cache_version`, and `ready: false` (HTTP 503) before that. Until the client sees `ready: true`, `check_flag`, `check_flag_with_entitlement` and `check_flags` all skip the cache and call the Schematic API, falling back to your flag defaults if the API call fails. Single and bulk checks follow the same rule. Only an explicit answer changes readiness. If the health endpoint can't be reached, times out, or returns a body without a `ready` field the client can read, the client keeps the readiness and `cache_version` from the last answer it got. Before the first answer, the cache counts as not ready.
734
+
735
+ Once the cache is ready, flag checks evaluate locally from the cache. A flag that isn't in the cache still falls back to the API. The DataStream client's `cache_ready?` reports the same readiness the flag checks use.
730
736
 
731
737
  ## Testing
732
738
 
@@ -1874,6 +1874,234 @@ module Schematic
1874
1874
  end
1875
1875
  end
1876
1876
 
1877
+ # @param request_options [Hash]
1878
+ # @param params [Hash]
1879
+ # @option request_options [String] :base_url
1880
+ # @option request_options [Hash{String => Object}] :additional_headers
1881
+ # @option request_options [Hash{String => Object}] :additional_query_parameters
1882
+ # @option request_options [Hash{String => Object}] :additional_body_parameters
1883
+ # @option request_options [Integer] :timeout_in_seconds
1884
+ # @option params [String] :company_id
1885
+ #
1886
+ # @example
1887
+ # client.billing.get_company_trial(company_id: "company_id")
1888
+ #
1889
+ # @return [Schematic::Billing::Types::GetCompanyTrialResponse]
1890
+ def get_company_trial(request_options: {}, **params)
1891
+ params = Schematic::Internal::Types::Utils.normalize_keys(params)
1892
+ request = Schematic::Internal::JSON::Request.new(
1893
+ base_url: request_options[:base_url],
1894
+ method: "GET",
1895
+ path: "companies/#{URI.encode_uri_component(params[:company_id].to_s)}/trial",
1896
+ request_options: request_options
1897
+ )
1898
+ begin
1899
+ response = @client.send(request)
1900
+ rescue Net::HTTPRequestTimeout
1901
+ raise Schematic::Errors::TimeoutError
1902
+ end
1903
+ code = response.code.to_i
1904
+ if code.between?(200, 299)
1905
+ (response.body.to_s.empty? ? nil : Schematic::Billing::Types::GetCompanyTrialResponse.load(response.body))
1906
+ else
1907
+ error_class = Schematic::Errors::ResponseError.subclass_for_code(code)
1908
+ error_types = {
1909
+ 401 => Schematic::Types::ApiError,
1910
+ 403 => Schematic::Types::ApiError,
1911
+ 404 => Schematic::Types::ApiError,
1912
+ 500 => Schematic::Types::ApiError
1913
+ }
1914
+ error_body = Schematic::Errors::ResponseError.load_error_body(code, response.body, error_types)
1915
+ raise error_class.new(response.body, code: code, body: error_body, headers: response.to_hash)
1916
+ end
1917
+ end
1918
+
1919
+ # @param request_options [Hash]
1920
+ # @param params [Schematic::Billing::Types::StartCompanyTrialRequestBody]
1921
+ # @option request_options [String] :base_url
1922
+ # @option request_options [Hash{String => Object}] :additional_headers
1923
+ # @option request_options [Hash{String => Object}] :additional_query_parameters
1924
+ # @option request_options [Hash{String => Object}] :additional_body_parameters
1925
+ # @option request_options [Integer] :timeout_in_seconds
1926
+ # @option params [String] :company_id
1927
+ #
1928
+ # @example
1929
+ # client.billing.start_company_trial(
1930
+ # company_id: "company_id",
1931
+ # cadence: "day",
1932
+ # plan_id: "plan_id"
1933
+ # )
1934
+ #
1935
+ # @return [Schematic::Billing::Types::StartCompanyTrialResponse]
1936
+ def start_company_trial(request_options: {}, **params)
1937
+ params = Schematic::Internal::Types::Utils.normalize_keys(params)
1938
+ request_data = Schematic::Billing::Types::StartCompanyTrialRequestBody.new(params).to_h
1939
+ non_body_param_names = %w[company_id]
1940
+ body = request_data.except(*non_body_param_names)
1941
+
1942
+ request = Schematic::Internal::JSON::Request.new(
1943
+ base_url: request_options[:base_url],
1944
+ method: "POST",
1945
+ path: "companies/#{URI.encode_uri_component(params[:company_id].to_s)}/trial",
1946
+ body: body,
1947
+ request_options: request_options
1948
+ )
1949
+ begin
1950
+ response = @client.send(request)
1951
+ rescue Net::HTTPRequestTimeout
1952
+ raise Schematic::Errors::TimeoutError
1953
+ end
1954
+ code = response.code.to_i
1955
+ if code.between?(200, 299)
1956
+ (response.body.to_s.empty? ? nil : Schematic::Billing::Types::StartCompanyTrialResponse.load(response.body))
1957
+ else
1958
+ error_class = Schematic::Errors::ResponseError.subclass_for_code(code)
1959
+ error_types = {
1960
+ 400 => Schematic::Types::ApiError,
1961
+ 401 => Schematic::Types::ApiError,
1962
+ 403 => Schematic::Types::ApiError,
1963
+ 404 => Schematic::Types::ApiError,
1964
+ 500 => Schematic::Types::ApiError
1965
+ }
1966
+ error_body = Schematic::Errors::ResponseError.load_error_body(code, response.body, error_types)
1967
+ raise error_class.new(response.body, code: code, body: error_body, headers: response.to_hash)
1968
+ end
1969
+ end
1970
+
1971
+ # @param request_options [Hash]
1972
+ # @param params [Schematic::Billing::Types::UpdateCompanyTrialRequestBody]
1973
+ # @option request_options [String] :base_url
1974
+ # @option request_options [Hash{String => Object}] :additional_headers
1975
+ # @option request_options [Hash{String => Object}] :additional_query_parameters
1976
+ # @option request_options [Hash{String => Object}] :additional_body_parameters
1977
+ # @option request_options [Integer] :timeout_in_seconds
1978
+ # @option params [String] :company_id
1979
+ #
1980
+ # @example
1981
+ # client.billing.update_company_trial(company_id: "company_id")
1982
+ #
1983
+ # @return [Schematic::Billing::Types::UpdateCompanyTrialResponse]
1984
+ def update_company_trial(request_options: {}, **params)
1985
+ params = Schematic::Internal::Types::Utils.normalize_keys(params)
1986
+ request_data = Schematic::Billing::Types::UpdateCompanyTrialRequestBody.new(params).to_h
1987
+ non_body_param_names = %w[company_id]
1988
+ body = request_data.except(*non_body_param_names)
1989
+
1990
+ request = Schematic::Internal::JSON::Request.new(
1991
+ base_url: request_options[:base_url],
1992
+ method: "PUT",
1993
+ path: "companies/#{URI.encode_uri_component(params[:company_id].to_s)}/trial",
1994
+ body: body,
1995
+ request_options: request_options
1996
+ )
1997
+ begin
1998
+ response = @client.send(request)
1999
+ rescue Net::HTTPRequestTimeout
2000
+ raise Schematic::Errors::TimeoutError
2001
+ end
2002
+ code = response.code.to_i
2003
+ if code.between?(200, 299)
2004
+ (response.body.to_s.empty? ? nil : Schematic::Billing::Types::UpdateCompanyTrialResponse.load(response.body))
2005
+ else
2006
+ error_class = Schematic::Errors::ResponseError.subclass_for_code(code)
2007
+ error_types = {
2008
+ 400 => Schematic::Types::ApiError,
2009
+ 401 => Schematic::Types::ApiError,
2010
+ 403 => Schematic::Types::ApiError,
2011
+ 404 => Schematic::Types::ApiError,
2012
+ 500 => Schematic::Types::ApiError
2013
+ }
2014
+ error_body = Schematic::Errors::ResponseError.load_error_body(code, response.body, error_types)
2015
+ raise error_class.new(response.body, code: code, body: error_body, headers: response.to_hash)
2016
+ end
2017
+ end
2018
+
2019
+ # @param request_options [Hash]
2020
+ # @param params [Hash]
2021
+ # @option request_options [String] :base_url
2022
+ # @option request_options [Hash{String => Object}] :additional_headers
2023
+ # @option request_options [Hash{String => Object}] :additional_query_parameters
2024
+ # @option request_options [Hash{String => Object}] :additional_body_parameters
2025
+ # @option request_options [Integer] :timeout_in_seconds
2026
+ # @option params [String] :company_id
2027
+ #
2028
+ # @example
2029
+ # client.billing.convert_company_trial(company_id: "company_id")
2030
+ #
2031
+ # @return [Schematic::Billing::Types::ConvertCompanyTrialResponse]
2032
+ def convert_company_trial(request_options: {}, **params)
2033
+ params = Schematic::Internal::Types::Utils.normalize_keys(params)
2034
+ request = Schematic::Internal::JSON::Request.new(
2035
+ base_url: request_options[:base_url],
2036
+ method: "POST",
2037
+ path: "companies/#{URI.encode_uri_component(params[:company_id].to_s)}/trial/convert",
2038
+ request_options: request_options
2039
+ )
2040
+ begin
2041
+ response = @client.send(request)
2042
+ rescue Net::HTTPRequestTimeout
2043
+ raise Schematic::Errors::TimeoutError
2044
+ end
2045
+ code = response.code.to_i
2046
+ if code.between?(200, 299)
2047
+ (response.body.to_s.empty? ? nil : Schematic::Billing::Types::ConvertCompanyTrialResponse.load(response.body))
2048
+ else
2049
+ error_class = Schematic::Errors::ResponseError.subclass_for_code(code)
2050
+ error_types = {
2051
+ 400 => Schematic::Types::ApiError,
2052
+ 401 => Schematic::Types::ApiError,
2053
+ 403 => Schematic::Types::ApiError,
2054
+ 404 => Schematic::Types::ApiError,
2055
+ 500 => Schematic::Types::ApiError
2056
+ }
2057
+ error_body = Schematic::Errors::ResponseError.load_error_body(code, response.body, error_types)
2058
+ raise error_class.new(response.body, code: code, body: error_body, headers: response.to_hash)
2059
+ end
2060
+ end
2061
+
2062
+ # @param request_options [Hash]
2063
+ # @param params [Hash]
2064
+ # @option request_options [String] :base_url
2065
+ # @option request_options [Hash{String => Object}] :additional_headers
2066
+ # @option request_options [Hash{String => Object}] :additional_query_parameters
2067
+ # @option request_options [Hash{String => Object}] :additional_body_parameters
2068
+ # @option request_options [Integer] :timeout_in_seconds
2069
+ # @option params [String] :company_id
2070
+ #
2071
+ # @example
2072
+ # client.billing.end_company_trial(company_id: "company_id")
2073
+ #
2074
+ # @return [Schematic::Billing::Types::EndCompanyTrialResponse]
2075
+ def end_company_trial(request_options: {}, **params)
2076
+ params = Schematic::Internal::Types::Utils.normalize_keys(params)
2077
+ request = Schematic::Internal::JSON::Request.new(
2078
+ base_url: request_options[:base_url],
2079
+ method: "POST",
2080
+ path: "companies/#{URI.encode_uri_component(params[:company_id].to_s)}/trial/end",
2081
+ request_options: request_options
2082
+ )
2083
+ begin
2084
+ response = @client.send(request)
2085
+ rescue Net::HTTPRequestTimeout
2086
+ raise Schematic::Errors::TimeoutError
2087
+ end
2088
+ code = response.code.to_i
2089
+ if code.between?(200, 299)
2090
+ (response.body.to_s.empty? ? nil : Schematic::Billing::Types::EndCompanyTrialResponse.load(response.body))
2091
+ else
2092
+ error_class = Schematic::Errors::ResponseError.subclass_for_code(code)
2093
+ error_types = {
2094
+ 400 => Schematic::Types::ApiError,
2095
+ 401 => Schematic::Types::ApiError,
2096
+ 403 => Schematic::Types::ApiError,
2097
+ 404 => Schematic::Types::ApiError,
2098
+ 500 => Schematic::Types::ApiError
2099
+ }
2100
+ error_body = Schematic::Errors::ResponseError.load_error_body(code, response.body, error_types)
2101
+ raise error_class.new(response.body, code: code, body: error_body, headers: response.to_hash)
2102
+ end
2103
+ end
2104
+
1877
2105
  # @param request_options [Hash]
1878
2106
  # @param params [Hash]
1879
2107
  # @option request_options [String] :base_url
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../schematic"
4
+
5
+ module Schematic
6
+ module Billing
7
+ module Types
8
+ class ConvertCompanyTrialResponse < Internal::Types::Model
9
+ field :data, -> { Schematic::Types::CompanyTrialResponseData }, optional: false, nullable: false
10
+
11
+ field :params, -> { Internal::Types::Hash[String, Object] }, optional: false, nullable: false
12
+ end
13
+ end
14
+ end
15
+ end
@@ -6,6 +6,8 @@ module Schematic
6
6
  class CreateBillingSubscriptionRequestBody < Internal::Types::Model
7
7
  field :application_id, -> { String }, optional: true, nullable: false
8
8
 
9
+ field :billing_cycle_anchor, -> { String }, optional: true, nullable: false
10
+
9
11
  field :cancel_at, -> { Integer }, optional: true, nullable: false
10
12
 
11
13
  field :cancel_at_period_end, -> { Internal::Types::Boolean }, optional: false, nullable: false
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../schematic"
4
+
5
+ module Schematic
6
+ module Billing
7
+ module Types
8
+ class EndCompanyTrialResponse < Internal::Types::Model
9
+ field :data, -> { Schematic::Types::CompanyTrialResponseData }, optional: false, nullable: false
10
+
11
+ field :params, -> { Internal::Types::Hash[String, Object] }, optional: false, nullable: false
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../schematic"
4
+
5
+ module Schematic
6
+ module Billing
7
+ module Types
8
+ class GetCompanyTrialResponse < Internal::Types::Model
9
+ field :data, -> { Schematic::Types::CompanyTrialResponseData }, optional: false, nullable: false
10
+
11
+ field :params, -> { Internal::Types::Hash[String, Object] }, optional: false, nullable: false
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Schematic
4
+ module Billing
5
+ module Types
6
+ class StartCompanyTrialRequestBody < Internal::Types::Model
7
+ field :company_id, -> { String }, optional: false, nullable: false
8
+
9
+ field :cadence, -> { Schematic::Types::BillingProductPriceInterval }, optional: false, nullable: false
10
+
11
+ field :cadence_interval_count, -> { Integer }, optional: true, nullable: false
12
+
13
+ field :end_behavior, -> { Schematic::Types::BillingSubscriptionTrialEndSetting }, optional: true, nullable: false
14
+
15
+ field :plan_id, -> { String }, optional: false, nullable: false
16
+
17
+ field :trial_end, -> { String }, optional: true, nullable: false
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../schematic"
4
+
5
+ module Schematic
6
+ module Billing
7
+ module Types
8
+ class StartCompanyTrialResponse < Internal::Types::Model
9
+ field :data, -> { Schematic::Types::CompanyTrialResponseData }, optional: false, nullable: false
10
+
11
+ field :params, -> { Internal::Types::Hash[String, Object] }, optional: false, nullable: false
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Schematic
4
+ module Billing
5
+ module Types
6
+ class UpdateCompanyTrialRequestBody < Internal::Types::Model
7
+ field :company_id, -> { String }, optional: false, nullable: false
8
+
9
+ field :end_behavior, -> { Schematic::Types::BillingSubscriptionTrialEndSetting }, optional: true, nullable: false
10
+
11
+ field :trial_end, -> { String }, optional: true, nullable: false
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../schematic"
4
+
5
+ module Schematic
6
+ module Billing
7
+ module Types
8
+ class UpdateCompanyTrialResponse < Internal::Types::Model
9
+ field :data, -> { Schematic::Types::CompanyTrialResponseData }, optional: false, nullable: false
10
+
11
+ field :params, -> { Internal::Types::Hash[String, Object] }, optional: false, nullable: false
12
+ end
13
+ end
14
+ end
15
+ end
@@ -14,6 +14,9 @@ module Schematic
14
14
  raise NotImplementedError
15
15
  end
16
16
 
17
+ # Optional: `set_keep_ttl(key, value)` overwrites a value without changing
18
+ # its expiry. Providers that don't define it are written with `set`.
19
+
17
20
  # Remove all keys matching `scope` that are NOT in `keys_to_keep`.
18
21
  # `scope:` is required so shared caches (e.g. Redis) only prune their own
19
22
  # namespace instead of wiping sibling caches that share a base key_prefix.
@@ -69,6 +72,20 @@ module Schematic
69
72
  end
70
73
  end
71
74
 
75
+ # Overwrites the value but keeps a live entry's expiry. A missing or
76
+ # expired entry gets the default TTL.
77
+ def set_keep_ttl(key, value)
78
+ kept = @mutex.synchronize do
79
+ entry = @cache[key]
80
+ next false unless entry && !entry.expired?
81
+
82
+ @cache.delete(key)
83
+ @cache[key] = CacheEntry.new(value, entry.ttl, created_at: entry.created_at)
84
+ true
85
+ end
86
+ set(key, value) unless kept
87
+ end
88
+
72
89
  def delete(key)
73
90
  @mutex.synchronize do
74
91
  @cache.delete(key)
@@ -122,10 +139,10 @@ module Schematic
122
139
  class CacheEntry
123
140
  attr_reader :value, :ttl, :created_at
124
141
 
125
- def initialize(value, ttl)
142
+ def initialize(value, ttl, created_at: nil)
126
143
  @value = value
127
144
  @ttl = ttl
128
- @created_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
145
+ @created_at = created_at || Process.clock_gettime(Process::CLOCK_MONOTONIC)
129
146
  end
130
147
 
131
148
  def expired?
@@ -9,6 +9,8 @@ module Schematic
9
9
  field :currency, -> { Schematic::Types::Currency }, optional: true, nullable: false
10
10
 
11
11
  field :selected_plan_id, -> { String }, optional: true, nullable: false
12
+
13
+ field :selected_plan_version_id, -> { String }, optional: true, nullable: false
12
14
  end
13
15
  end
14
16
  end
@@ -43,7 +43,7 @@ module Schematic
43
43
  @raw_client = Schematic::Internal::Http::RawClient.new(
44
44
  base_url: base_url || Schematic::Environment::DEFAULT,
45
45
  headers: {
46
- "User-Agent" => "schematichq/1.5.8",
46
+ "User-Agent" => "schematichq/1.5.9",
47
47
  "X-Fern-Language" => "Ruby",
48
48
  "X-Schematic-Api-Key" => api_key.to_s
49
49
  },
@@ -129,10 +129,26 @@ module Schematic
129
129
  end
130
130
  end
131
131
 
132
+ # Whether the DataStream is connected. In websocket mode this is the
133
+ # WebSocket connection state. In replicator mode it reports replicator
134
+ # readiness (the same value as cache_ready?), kept for backward
135
+ # compatibility; use cache_ready? to decide whether the replicator cache
136
+ # can be served.
132
137
  def connected?
133
138
  @replicator_mode ? @replicator_ready : (@ws_client&.connected || false)
134
139
  end
135
140
 
141
+ # Whether the cache is ready to serve flag checks. In replicator mode this
142
+ # is the `ready` field from the replicator's health endpoint, which is true
143
+ # only once the replicator has fully loaded the cache for its current
144
+ # cache_version. It is false until the first answer from the health
145
+ # endpoint, and a failed poll leaves it as the last answer set it.
146
+ # Outside replicator mode it is always true: the SDK
147
+ # fills its own cache over the WebSocket, so there is nothing to wait for.
148
+ def cache_ready?
149
+ @replicator_mode ? @replicator_ready : true
150
+ end
151
+
136
152
  def close
137
153
  @logger.info("Closing DataStream client")
138
154
  @mutex.synchronize { @stopped = true }
@@ -208,7 +224,10 @@ module Schematic
208
224
  end
209
225
 
210
226
  company[:metrics] = metrics
211
- @company_cache.cache_entity(company, ttl: @cache_ttl)
227
+ # The replicator owns these keys and usually writes them with no
228
+ # expiry; applying our TTL here would make them vanish if the
229
+ # replicator doesn't rewrite the company in time.
230
+ @company_cache.cache_entity(company, ttl: @cache_ttl, keep_ttl: @replicator_mode)
212
231
  end
213
232
 
214
233
  private
@@ -499,6 +518,15 @@ module Schematic
499
518
  @health_thread.abort_on_exception = false
500
519
  end
501
520
 
521
+ # Polls the replicator health endpoint. The body is read whatever the
522
+ # HTTP status: a replicator that is still loading its cache answers 503
523
+ # with {"ready": false, "cache_version": "..."}. Only an explicit answer
524
+ # changes state: a body with a boolean `ready` sets readiness from it and
525
+ # records any non-empty cache_version. A failed poll (connection error,
526
+ # timeout, or a body with no boolean `ready`) changes nothing, because
527
+ # it says nothing about whether the cache is complete, and flipping every
528
+ # check to the API on one blip would send the whole fleet to the API at
529
+ # once. Before the first answer the replicator counts as not ready.
502
530
  def check_replicator_health
503
531
  uri = URI.parse(@replicator_health_url)
504
532
  http = Net::HTTP.new(uri.host, uri.port)
@@ -507,29 +535,17 @@ module Schematic
507
535
  http.read_timeout = 5
508
536
  response = http.get(uri.request_uri)
509
537
 
510
- if response.is_a?(Net::HTTPSuccess)
511
- body = begin
512
- JSON.parse(response.body, symbolize_names: true)
513
- rescue StandardError
514
- {}
515
- end
516
- was_ready = @replicator_ready
517
- @replicator_ready = body[:ready] == true
518
-
519
- update_cache_version(body[:cache_version].to_s)
520
-
521
- @logger.info("Replicator is ready") if @replicator_ready && !was_ready
522
- @logger.info("Replicator is no longer ready") if !@replicator_ready && was_ready
523
- else
524
- was_ready = @replicator_ready
525
- @replicator_ready = false
526
- @logger.info("Replicator is no longer ready") if was_ready
527
- @logger.warn("Replicator health check failed: #{response.code}")
538
+ body = parse_health_body(response.body)
539
+ unless body
540
+ @logger.warn("Replicator health check returned no readiness answer (HTTP #{response.code})")
541
+ return
528
542
  end
543
+
544
+ cache_version = body[:cache_version].to_s
545
+ update_cache_version(cache_version) unless cache_version.empty?
546
+ update_replicator_ready(body[:ready])
547
+ @logger.debug("Replicator not ready (HTTP #{response.code})") unless @replicator_ready
529
548
  rescue StandardError => e
530
- was_ready = @replicator_ready
531
- @replicator_ready = false
532
- @logger.info("Replicator is no longer ready") if was_ready
533
549
  @logger.warn("Replicator health check error: #{e.message}")
534
550
  end
535
551
 
@@ -543,6 +559,20 @@ module Schematic
543
559
  # definitions. The credit-lease check path resolves the flag, the company,
544
560
  # and the user itself before evaluating, so it needs all three.
545
561
  public :get_flag, :get_company, :get_user, :get_cached_company
562
+
563
+ def parse_health_body(raw)
564
+ body = JSON.parse(raw.to_s, symbolize_names: true)
565
+ body.is_a?(Hash) && [true, false].include?(body[:ready]) ? body : nil
566
+ rescue JSON::ParserError
567
+ nil
568
+ end
569
+
570
+ def update_replicator_ready(ready)
571
+ was_ready = @replicator_ready
572
+ @replicator_ready = ready
573
+ @logger.info("Replicator is ready (cache_version: #{@cache_version})") if ready && !was_ready
574
+ @logger.info("Replicator is no longer ready") if !ready && was_ready
575
+ end
546
576
  end
547
577
  end
548
578
  end
@@ -14,17 +14,19 @@ module Schematic
14
14
 
15
15
  attr_writer :cache_version
16
16
 
17
- def cache_entity(entity, ttl: nil)
17
+ # With keep_ttl, each key keeps its current expiry instead of taking
18
+ # `ttl`, on providers that support it.
19
+ def cache_entity(entity, ttl: nil, keep_ttl: false)
18
20
  id = @get_id.call(entity)
19
21
  return unless id
20
22
 
21
23
  id_key = build_id_key(id)
22
- @primary_cache.set(id_key, entity, ttl: ttl)
24
+ write(@primary_cache, id_key, entity, ttl, keep_ttl)
23
25
 
24
26
  keys = @get_keys.call(entity)
25
27
  keys&.each do |k, v|
26
28
  lookup_key = build_lookup_key(k, v)
27
- @lookup_cache.set(lookup_key, id, ttl: ttl)
29
+ write(@lookup_cache, lookup_key, id, ttl, keep_ttl)
28
30
  end
29
31
  end
30
32
 
@@ -62,6 +64,14 @@ module Schematic
62
64
 
63
65
  private
64
66
 
67
+ def write(cache, key, value, ttl, keep_ttl)
68
+ if keep_ttl && cache.respond_to?(:set_keep_ttl)
69
+ cache.set_keep_ttl(key, value)
70
+ else
71
+ cache.set(key, value, ttl: ttl)
72
+ end
73
+ end
74
+
65
75
  def build_id_key(id)
66
76
  "#{@key_prefix}:#{@cache_version}:#{id}"
67
77
  end
@@ -237,7 +237,7 @@ module Schematic
237
237
  )
238
238
 
239
239
  request_headers = @default_headers.merge(auth_headers).merge(headers)
240
- request_headers.each { |name, value| request[name] = value }
240
+ request_headers.each { |name, value| request[name] = value == false ? "false" : value }
241
241
  request.body = body if body
242
242
 
243
243
  # Net::HTTP disables its transparent gzip/deflate decoding as soon as an