metrifox-sdk 1.0.1 → 1.0.3

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: f814153e75795c0d87de3d15f3164f73fc6a561be992a209900880d2b8461406
4
- data.tar.gz: 5329b40387f7555df0895e7889f0e5f63b52b854571d34816627d159340bccd5
3
+ metadata.gz: 61978214945170e21af55446d8aa5606a14710fa5515cdd327d11227b5f95a22
4
+ data.tar.gz: cc96ae1ea46452d1d1221b936048bb9ad8931bd93317c727352272f08ad796a6
5
5
  SHA512:
6
- metadata.gz: 0efde0b9967198c7f0631b7bd35343f00fa94c56d8cfde2696eb7b6f4405ca6995adb82776f5ea516a6f5e40d132ff1023690e4c0526560ad429879a1dfc605a
7
- data.tar.gz: 7c14771a87ad8032448a53d38b20e3ed94fb423c4b674897c9263408cc4693fcfd7033931c1ac66e53c0c962dda0f92f29841aa8b9a35065a1512fd496aa11b1
6
+ metadata.gz: b31ffd58d394c50d65fd22b339f939dcbfa8ae31bb716cce31c759cd1ad9ab584746f1476e1e376541e6ac27539e4abed0b3aca665f6607136a594226881f2bf
7
+ data.tar.gz: b7e0c2167befb2befc63e50b4cef0b940c0a4f81890933f2100937fe458a3c3a626451d7b4fc103f1f649cc7b6cac83ff79ed74c5fee21b023a5692479256d59
data/README.md CHANGED
@@ -7,7 +7,7 @@ A Ruby SDK for interacting with the Metrifox platform API.
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'metrifox_sdk'
10
+ gem 'metrifox-sdk'
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -19,7 +19,7 @@ $ bundle install
19
19
  Or install it yourself as:
20
20
 
21
21
  ```bash
22
- $ gem install metrifox_sdk
22
+ $ gem install metrifox-sdk
23
23
  ```
24
24
 
25
25
  ## Usage
@@ -27,25 +27,21 @@ $ gem install metrifox_sdk
27
27
  ### Configuration
28
28
 
29
29
  ```ruby
30
- require 'metrifox_sdk'
30
+ require 'metrifox-sdk'
31
31
 
32
32
  # Initialize with configuration
33
- MetrifoxSDK.init({
34
- api_key: "your-api-key",
35
- base_url: "https://metrifox-api.staging.useyala.com/api/v1/",
36
- web_app_base_url: "https://frontend-v3.staging.useyala.com"
37
- })
33
+ METRIFOX_SDK = MetrifoxSDK.init({ api_key: "your-api-key"})
38
34
 
39
35
  # Or set environment variable
40
36
  ENV["METRIFOX_API_KEY"] = "your-api-key"
41
- MetrifoxSDK.init
37
+ METRIFOX_SDK = MetrifoxSDK.init
42
38
  ```
43
39
 
44
40
  ### Access Control
45
41
 
46
42
  ```ruby
47
43
  # Check feature access
48
- response = MetrifoxSDK.usages.check_access({
44
+ response = METRIFOX_SDK.usages.check_access({
49
45
  feature_key: "premium_feature",
50
46
  customer_key: "customer_123"
51
47
  })
@@ -56,64 +52,146 @@ puts response["can_access"] # true/false
56
52
  ### Usage Tracking
57
53
 
58
54
  ```ruby
59
- # Record usage event
60
- response = MetrifoxSDK.usages.record_usage({
55
+ # Basic usage recording
56
+ response = METRIFOX_SDK.usages.record_usage({
61
57
  customer_key: "customer_123",
62
58
  event_name: "api_call",
63
59
  amount: 1
64
60
  })
61
+
62
+ # Advanced usage recording with additional fields
63
+ response = METRIFOX_SDK.usages.record_usage({
64
+ customer_key: "customer_123",
65
+ event_name: "api_call",
66
+ amount: 1,
67
+ credit_used: 5,
68
+ event_id: "event_uuid_123",
69
+ timestamp: 1640995200,
70
+ metadata: {
71
+ source: "web_app",
72
+ feature: "premium_search"
73
+ }
74
+ })
75
+
76
+ # Using structured request object
77
+ usage_request = MetrifoxSDK::Types::UsageEventRequest.new(
78
+ customer_key: "customer_123",
79
+ event_name: "api_call",
80
+ amount: 1,
81
+ credit_used: 5,
82
+ event_id: "event_uuid_123",
83
+ timestamp: Time.now.to_i,
84
+ metadata: { source: "mobile_app" }
85
+ )
86
+
87
+ response = METRIFOX_SDK.usages.record_usage(usage_request)
65
88
  ```
66
89
 
67
90
  ### Customer Management
68
91
 
69
92
  ```ruby
70
- # Create customer
93
+ # Create customer (customer_key is REQUIRED)
71
94
  customer_data = {
72
- customer_key: "customer_123",
95
+ customer_key: "customer_123", # Required - unique identifier for the customer
73
96
  customer_type: "BUSINESS",
74
97
  primary_email: "customer@example.com",
75
98
  legal_name: "Acme Corp",
76
99
  display_name: "ACME"
77
100
  }
78
101
 
79
- response = MetrifoxSDK.customers.create(customer_data)
102
+ response = METRIFOX_SDK.customers.create(customer_data)
80
103
 
81
- # Update customer
104
+ # Update customer (customer_key cannot be changed and is passed as a parameter)
82
105
  update_data = {
83
106
  display_name: "ACME Corporation",
84
107
  website_url: "https://acme.com"
108
+ # Note: customer_key is NOT included here - it's immutable
85
109
  }
86
110
 
87
- response = MetrifoxSDK.customers.update("customer_123", update_data)
111
+ response = METRIFOX_SDK.customers.update("customer_123", update_data)
88
112
 
89
113
  # Get customer
90
- response = MetrifoxSDK.customers.get_customer({ customer_key: "customer_123" })
114
+ response = METRIFOX_SDK.customers.get_customer({ customer_key: "customer_123" })
91
115
 
92
116
  # Get customer details
93
- response = MetrifoxSDK.customers.get_details({ customer_key: "customer_123" })
117
+ response = METRIFOX_SDK.customers.get_details({ customer_key: "customer_123" })
118
+
119
+ # Check for an active subscription
120
+ has_active_subscription = MetrifoxSDK.customers.has_active_subscription?(customer_key: "customer_123")
121
+ puts has_active_subscription # true or false
122
+
123
+ # List customers
124
+ response = METRIFOX_SDK.customers.list
125
+
126
+ # List customers with pagination
127
+ response = METRIFOX_SDK.customers.list({ page: 2, per_page: 10 })
128
+
129
+ # List customers with filters
130
+ response = METRIFOX_SDK.customers.list({
131
+ search_term: "TechStart",
132
+ customer_type: "BUSINESS",
133
+ date_created: "2025-09-01"
134
+ })
135
+
136
+ # List customers with combined pagination and filters
137
+ response = METRIFOX_SDK.customers.list({
138
+ page: 1,
139
+ per_page: 5,
140
+ search_term: "John",
141
+ customer_type: "INDIVIDUAL"
142
+ })
94
143
 
95
144
  # Delete customer
96
- response = MetrifoxSDK.delete_customer({ customer_key: "customer_123" })
145
+ response = METRIFOX_SDK.customers.delete_customer({ customer_key: "customer_123" })
146
+
97
147
  ```
98
148
 
99
149
  ### CSV Upload
100
150
 
101
151
  ```ruby
102
152
  # Upload customers via CSV
103
- response = MetrifoxSDK.customers.upload_csv("/path/to/customers.csv")
153
+ response = METRIFOX_SDK.customers.upload_csv("/path/to/customers.csv")
104
154
 
105
155
  puts response["data"]["total_customers"]
106
156
  puts response["data"]["successful_upload_count"]
107
157
  ```
108
158
 
109
- ### Using Client Instance
159
+ ### Checkout URL Generation
110
160
 
111
161
  ```ruby
112
- client = MetrifoxSDK::Client.new({
113
- api_key: "your-api-key"
162
+ # Basic checkout URL generation
163
+ checkout_url = METRIFOX_SDK.checkout.url({
164
+ offering_key: "your_offering_key"
165
+ })
166
+
167
+ # With optional billing interval
168
+ checkout_url = METRIFOX_SDK.checkout.url({
169
+ offering_key: "your_offering_key",
170
+ billing_interval: "monthly"
171
+ })
172
+
173
+ # With customer key for pre-filled checkout
174
+ checkout_url = METRIFOX_SDK.checkout.url({
175
+ offering_key: "your_offering_key",
176
+ billing_interval: "monthly",
177
+ customer_key: "customer_123"
114
178
  })
115
179
 
116
- response = client.usages.check_access({
180
+ # Using structured config object
181
+ checkout_config = MetrifoxSDK::Types::CheckoutConfig.new(
182
+ offering_key: "your_offering_key",
183
+ billing_interval: "monthly",
184
+ customer_key: "customer_123"
185
+ )
186
+
187
+ checkout_url = METRIFOX_SDK.checkout.url(checkout_config)
188
+ ```
189
+
190
+ ### Using Client Instance
191
+
192
+ ```ruby
193
+
194
+ response = METRIFOX_SDK.usages.check_access({
117
195
  feature_key: "premium_feature",
118
196
  customer_key: "customer_123"
119
197
  })
@@ -130,24 +208,33 @@ access_request = MetrifoxSDK::Types::AccessCheckRequest.new(
130
208
  customer_key: "customer_123"
131
209
  )
132
210
 
133
- response = MetrifoxSDK.usages.check_access(access_request)
211
+ response = METRIFOX_SDK.usages.check_access(access_request)
134
212
 
135
- # Customer creation with structured data
213
+ # Customer creation with structured data (customer_key is REQUIRED)
136
214
  customer_request = MetrifoxSDK::Types::CustomerCreateRequest.new(
137
- customer_key: "customer_123",
215
+ customer_key: "customer_123", # Required
138
216
  customer_type: MetrifoxSDK::Types::CustomerType::BUSINESS,
139
217
  primary_email: "customer@example.com",
140
218
  legal_name: "Acme Corp"
141
219
  )
142
220
 
143
- response = MetrifoxSDK.customers.create(customer_request)
221
+ response = METRIFOX_SDK.customers.create(customer_request)
222
+
223
+ # Customer update with structured data (customer_key is immutable)
224
+ customer_update = MetrifoxSDK::Types::CustomerUpdateRequest.new(
225
+ display_name: "Acme Corporation",
226
+ website_url: "https://acme.com"
227
+ # Note: customer_key is NOT a field in CustomerUpdateRequest
228
+ )
229
+
230
+ response = METRIFOX_SDK.customers.update("customer_123", customer_update)
144
231
  ```
145
232
 
146
233
  ## Error Handling
147
234
 
148
235
  ```ruby
149
236
  begin
150
- response = MetrifoxSDK.usages.check_access({
237
+ response = METRIFOX_SDK.usages.check_access({
151
238
  feature_key: "premium_feature",
152
239
  customer_key: "customer_123"
153
240
  })
@@ -168,4 +255,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/yourus
168
255
 
169
256
  ## License
170
257
 
171
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
258
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/lib/metrifox-sdk.rb CHANGED
@@ -4,8 +4,11 @@ require_relative "metrifox_sdk/types"
4
4
  require_relative "metrifox_sdk/util_methods"
5
5
  require_relative "metrifox_sdk/base_api"
6
6
  require_relative "metrifox_sdk/base_module"
7
+ require_relative "metrifox_sdk/customers/api"
7
8
  require_relative "metrifox_sdk/customers/module"
8
9
  require_relative "metrifox_sdk/usages/module"
10
+ require_relative "metrifox_sdk/checkout/api"
11
+ require_relative "metrifox_sdk/checkout/module"
9
12
 
10
13
  module MetrifoxSDK
11
14
  class << self
@@ -0,0 +1,23 @@
1
+ require "net/http"
2
+ require "uri"
3
+ require "json"
4
+ require_relative "../base_api"
5
+
6
+ module MetrifoxSDK::Checkout
7
+ class API < MetrifoxSDK::BaseApi
8
+ def fetch_checkout_key(base_url, api_key)
9
+ uri = URI.join(base_url, "auth/checkout-username")
10
+ response = make_request(uri, "GET", api_key)
11
+ data = parse_response(response, "Failed to get tenant checkout settings")
12
+ data.dig("data", "checkout_username")
13
+ end
14
+
15
+ def generate_checkout_url(base_url, api_key, query_params)
16
+ uri = URI.join(base_url, "products/offerings/generate-checkout-url")
17
+ uri.query = URI.encode_www_form(query_params)
18
+ response = make_request(uri, "GET", api_key)
19
+ data = parse_response(response, "Failed to generate checkout URL")
20
+ data.dig("data", "checkout_url")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,55 @@
1
+ require_relative "api"
2
+ require_relative "../base_module"
3
+
4
+ module MetrifoxSDK
5
+ module Checkout
6
+ class Module < BaseModule
7
+ def url(config)
8
+ validate_api_key!
9
+
10
+ # Handle both hash and struct access patterns
11
+ offering_key = get_value(config, :offering_key)
12
+ billing_interval = get_value(config, :billing_interval)
13
+ customer_key = get_value(config, :customer_key)
14
+
15
+ raise ArgumentError, "offering_key is required" if offering_key.nil? || offering_key.empty?
16
+
17
+ # Build query parameters
18
+ query_params = { offering_key: offering_key }
19
+ query_params[:billing_interval] = billing_interval if billing_interval && !billing_interval.empty?
20
+ query_params[:customer_key] = customer_key if customer_key && !customer_key.empty?
21
+
22
+ # Call API to generate checkout URL
23
+ checkout_url = api.generate_checkout_url(base_url, api_key, query_params)
24
+ raise StandardError, "Checkout URL could not be generated" if checkout_url.nil? || checkout_url.empty?
25
+
26
+ checkout_url
27
+ end
28
+
29
+ private
30
+
31
+ def get_checkout_key
32
+ api.fetch_checkout_key(base_url, api_key)
33
+ end
34
+
35
+ def web_app_base_url
36
+ @client.web_app_base_url
37
+ end
38
+
39
+ def api
40
+ @api ||= API.new
41
+ end
42
+
43
+ # Helper method to get value from either hash or struct
44
+ def get_value(object, key)
45
+ if object.respond_to?(key)
46
+ object.public_send(key)
47
+ elsif object.respond_to?(:[])
48
+ object[key] || object[key.to_s]
49
+ else
50
+ nil
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -21,6 +21,10 @@ module MetrifoxSDK
21
21
  @usages ||= Usages::Module.new(self)
22
22
  end
23
23
 
24
+ def checkout
25
+ @checkout ||= Checkout::Module.new(self)
26
+ end
27
+
24
28
  private
25
29
 
26
30
  def get_api_key_from_environment
@@ -41,6 +41,25 @@ module MetrifoxSDK::Customers
41
41
  parse_response(response, "Failed to Fetch Customer Details")
42
42
  end
43
43
 
44
+ def customer_active_subscription_request(base_url, api_key, customer_key)
45
+ uri = URI.join(base_url, "customers/#{customer_key}/check-active-subscription")
46
+ response = make_request(uri, "GET", api_key)
47
+ parse_response(response, "Failed to Check Active Subscription")
48
+ end
49
+
50
+ def customer_list_request(base_url, api_key, request_payload = {})
51
+ uri = URI.join(base_url, "customers")
52
+
53
+ # Build query parameters from the request payload
54
+ query_params = build_query_params(request_payload)
55
+ if query_params && !query_params.empty?
56
+ uri.query = URI.encode_www_form(query_params)
57
+ end
58
+
59
+ response = make_request(uri, "GET", api_key)
60
+ parse_response(response, "Failed to Fetch Customers")
61
+ end
62
+
44
63
  def upload_customers_csv(base_url, api_key, file_path)
45
64
  uri = URI.join(base_url, "customers/csv-upload")
46
65
 
@@ -68,6 +87,24 @@ module MetrifoxSDK::Customers
68
87
  end
69
88
  end
70
89
 
90
+ # Helper method to build query parameters for list requests
91
+ def build_query_params(request_payload)
92
+ return {} unless request_payload
93
+
94
+ params = {}
95
+
96
+ # Handle pagination parameters
97
+ params[:page] = get_value(request_payload, :page) if get_value(request_payload, :page)
98
+ params[:per_page] = get_value(request_payload, :per_page) if get_value(request_payload, :per_page)
99
+
100
+ # Handle filter parameters
101
+ params[:search_term] = get_value(request_payload, :search_term) if get_value(request_payload, :search_term)
102
+ params[:customer_type] = get_value(request_payload, :customer_type) if get_value(request_payload, :customer_type)
103
+ params[:date_created] = get_value(request_payload, :date_created) if get_value(request_payload, :date_created)
104
+
105
+ params.compact
106
+ end
107
+
71
108
  # Helper method to get value from either hash or struct
72
109
  def get_value(object, key)
73
110
  if object.respond_to?(key)
@@ -79,4 +116,4 @@ module MetrifoxSDK::Customers
79
116
  end
80
117
  end
81
118
  end
82
- end
119
+ end
@@ -19,16 +19,36 @@ module MetrifoxSDK
19
19
  api.customer_get_request(base_url, api_key, request_payload)
20
20
  end
21
21
 
22
+ def get(request_payload)
23
+ get_customer(request_payload)
24
+ end
25
+
22
26
  def get_details(request_payload)
23
27
  validate_api_key!
24
28
  api.customer_details_get_request(base_url, api_key, request_payload)
25
29
  end
26
30
 
31
+ def has_active_subscription?(customer_key:)
32
+ validate_api_key!
33
+ result = api.customer_active_subscription_request(base_url, api_key, customer_key)
34
+ data = result["data"] || {}
35
+ data["has_active_subscription"]
36
+ end
37
+
27
38
  def delete_customer(request_payload)
28
39
  validate_api_key!
29
40
  api.customer_delete_request(base_url, api_key, request_payload)
30
41
  end
31
42
 
43
+ def delete(request_payload)
44
+ delete_customer(request_payload)
45
+ end
46
+
47
+ def list(request_payload = {})
48
+ validate_api_key!
49
+ api.customer_list_request(base_url, api_key, request_payload)
50
+ end
51
+
32
52
  def upload_csv(file_path)
33
53
  validate_api_key!
34
54
  api.upload_customers_csv(base_url, api_key, file_path)
@@ -41,4 +61,4 @@ module MetrifoxSDK
41
61
  end
42
62
  end
43
63
  end
44
- end
64
+ end
@@ -44,8 +44,11 @@ module MetrifoxSDK
44
44
  :carryover_quantity, :balance, keyword_init: true
45
45
  )
46
46
 
47
- UsageEventRequest = Struct.new(:customer_key, :event_name, :amount, keyword_init: true) do
48
- def initialize(customer_key:, event_name:, amount: 1)
47
+ UsageEventRequest = Struct.new(
48
+ :customer_key, :event_name, :amount, :credit_used, :event_id, :timestamp, :metadata,
49
+ keyword_init: true
50
+ ) do
51
+ def initialize(customer_key:, event_name:, amount: 1, credit_used: nil, event_id: nil, timestamp: nil, metadata: {})
49
52
  super
50
53
  end
51
54
  end
@@ -53,7 +56,7 @@ module MetrifoxSDK
53
56
  UsageEventResponse = Struct.new(:message, :event_name, :customer_key, keyword_init: true)
54
57
 
55
58
  CustomerCreateRequest = Struct.new(
56
- # Core fields
59
+ # Core fields (customer_key is REQUIRED)
57
60
  :customer_key, :customer_type, :primary_email, :primary_phone,
58
61
  # Business fields
59
62
  :legal_name, :display_name, :legal_number, :tax_identification_number,
@@ -73,8 +76,8 @@ module MetrifoxSDK
73
76
  )
74
77
 
75
78
  CustomerUpdateRequest = Struct.new(
76
- # Core fields
77
- :customer_key, :customer_type, :primary_email, :primary_phone, :billing_email,
79
+ # Core fields (customer_key is NOT included - it's immutable and passed as a parameter)
80
+ :customer_type, :primary_email, :primary_phone, :billing_email,
78
81
  # Business fields
79
82
  :legal_name, :display_name, :legal_number, :tax_identification_number,
80
83
  :logo_url, :website_url, :account_manager,
@@ -107,5 +110,6 @@ module MetrifoxSDK
107
110
  APIResponse = Struct.new(:status_code, :message, :data, :errors, :meta, keyword_init: true)
108
111
 
109
112
  EmbedConfig = Struct.new(:container, :product_key, keyword_init: true)
113
+ CheckoutConfig = Struct.new(:offering_key, :billing_interval, :customer_key, keyword_init: true)
110
114
  end
111
115
  end
@@ -29,6 +29,10 @@ module MetrifoxSDK::Usages
29
29
  customer_key = get_value(request_payload, :customer_key)
30
30
  event_name = get_value(request_payload, :event_name)
31
31
  amount = get_value(request_payload, :amount) || 1
32
+ credit_used = get_value(request_payload, :credit_used)
33
+ event_id = get_value(request_payload, :event_id)
34
+ timestamp = get_value(request_payload, :timestamp)
35
+ metadata = get_value(request_payload, :metadata) || {}
32
36
 
33
37
  body = {
34
38
  customer_key: customer_key,
@@ -36,6 +40,12 @@ module MetrifoxSDK::Usages
36
40
  amount: amount
37
41
  }
38
42
 
43
+ # Add optional fields if present
44
+ body[:credit_used] = credit_used if credit_used
45
+ body[:event_id] = event_id if event_id && !event_id.empty?
46
+ body[:timestamp] = timestamp if timestamp
47
+ body[:metadata] = metadata if metadata
48
+
39
49
  response = make_request(uri, "POST", api_key, body)
40
50
  parse_response(response, "Failed to record usage")
41
51
  end
@@ -1,3 +1,3 @@
1
1
  module MetrifoxSDK
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metrifox-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Metrifox
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2025-09-01 00:00:00.000000000 Z
10
+ date: 2025-11-26 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: json
@@ -121,6 +120,8 @@ files:
121
120
  - lib/metrifox-sdk.rb
122
121
  - lib/metrifox_sdk/base_api.rb
123
122
  - lib/metrifox_sdk/base_module.rb
123
+ - lib/metrifox_sdk/checkout/api.rb
124
+ - lib/metrifox_sdk/checkout/module.rb
124
125
  - lib/metrifox_sdk/client.rb
125
126
  - lib/metrifox_sdk/customers/api.rb
126
127
  - lib/metrifox_sdk/customers/module.rb
@@ -136,7 +137,6 @@ metadata:
136
137
  homepage_uri: https://github.com/metrifox/metrifox-ruby
137
138
  source_code_uri: https://github.com/metrifox/metrifox-ruby
138
139
  changelog_uri: https://github.com/metrifox/metrifox-ruby/tree/main/CHANGELOG.md
139
- post_install_message:
140
140
  rdoc_options: []
141
141
  require_paths:
142
142
  - lib
@@ -151,8 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
153
  requirements: []
154
- rubygems_version: 3.5.22
155
- signing_key:
154
+ rubygems_version: 3.6.2
156
155
  specification_version: 4
157
156
  summary: Ruby SDK for Metrifox API
158
157
  test_files: []