apiverve_regextester 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a3d3ef0b5b4f4ef32e9ac977a47fc6fa2007cd385cf58aabfedc1ed89d8f30fb
4
+ data.tar.gz: f21350b662c5e706c5dc9a01cffac940027dd1f1236f093a50114cf91048a544
5
+ SHA512:
6
+ metadata.gz: 80795e764a4dff0881b48c751fe3d9861d1117abfc77d7ab5d889f306687433c9356203d64cf41697a233cf627240d9722a4aac53c454772bae72836e96a2c2b
7
+ data.tar.gz: 9bf574b8d3c71194487d3255e2936242eb8048d0b10a58c2c54c8c71d1ffd30e921bee2e750aadcb3d1796d2d4d3a32e06bb18bbdb065e6e864f87f49b1c84e5
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) {{year}} APIVerve
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,374 @@
1
+ # Regex Tester API - Ruby Gem
2
+
3
+ Regex Tester is a comprehensive tool for testing and validating regular expressions. It supports multiple operations (test, match, search, replace, split) with detailed performance analysis and pattern suggestions.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'apiverve_regextester'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install apiverve_regextester
23
+ ```
24
+
25
+ ## Getting Started
26
+
27
+ Get your API key at [APIVerve](https://apiverve.com)
28
+
29
+ ### Basic Usage
30
+
31
+ ```ruby
32
+ require 'apiverve_regextester'
33
+
34
+ # Initialize the client
35
+ client = APIVerve::Regextester::Client.new(api_key: "YOUR_API_KEY")
36
+
37
+ # Make a request
38
+ response = client.execute({
39
+ pattern: "\d{3}-\d{2}-\d{4}",
40
+ text: "My SSN is 123-45-6789 and my friend's is 987-65-4321",
41
+ flags: "g"
42
+ })
43
+
44
+ # Print the response
45
+ puts response
46
+ ```
47
+
48
+
49
+ ### Error Handling
50
+
51
+ ```ruby
52
+ begin
53
+ response = client.execute({ pattern: "\d{3}-\d{2}-\d{4}", text: "My SSN is 123-45-6789 and my friend's is 987-65-4321", flags: "g" })
54
+ puts response["data"]
55
+ rescue APIVerve::Regextester::ValidationError => e
56
+ puts "Validation error: #{e.errors.join(', ')}"
57
+ rescue APIVerve::Regextester::APIError => e
58
+ puts "API error: #{e.message}"
59
+ puts "Status code: #{e.status_code}"
60
+ end
61
+ ```
62
+
63
+ ### Debug Mode
64
+
65
+ ```ruby
66
+ # Enable debug logging
67
+ client = APIVerve::Regextester::Client.new(
68
+ api_key: "YOUR_API_KEY",
69
+ debug: true
70
+ )
71
+ ```
72
+
73
+ ## Example Response
74
+
75
+ ```json
76
+ {
77
+ "status": "ok",
78
+ "error": null,
79
+ "data": {
80
+ "pattern": "\\d{3}-\\d{2}-\\d{4}",
81
+ "text": "My SSN is 123-45-6789 and my friend's is 987-65-4321",
82
+ "flags": "g",
83
+ "test_type": "test",
84
+ "replacement": null,
85
+ "is_valid_regex": true,
86
+ "regex_info": {
87
+ "pattern": "\\d{3}-\\d{2}-\\d{4}",
88
+ "flags": {
89
+ "global": true,
90
+ "ignore_case": false,
91
+ "multiline": false,
92
+ "sticky": false,
93
+ "unicode": false,
94
+ "dot_all": false
95
+ },
96
+ "source": "\\d{3}-\\d{2}-\\d{4}",
97
+ "last_index": 21,
98
+ "pattern_length": 17,
99
+ "complexity": "Medium"
100
+ },
101
+ "test_results": {
102
+ "operation": "test",
103
+ "result": true,
104
+ "execution_time_ms": 0,
105
+ "description": "Returns true if pattern matches anywhere in text, false otherwise"
106
+ },
107
+ "performance": {
108
+ "iterations": 192,
109
+ "total_time_ms": 0,
110
+ "average_time_ms": 0,
111
+ "performance_rating": "Excellent"
112
+ },
113
+ "pattern_analysis": {
114
+ "contains_anchors": {
115
+ "start_anchor": false,
116
+ "end_anchor": false,
117
+ "word_boundary": false
118
+ },
119
+ "contains_quantifiers": {
120
+ "zero_or_more": false,
121
+ "one_or_more": false,
122
+ "zero_or_one": false,
123
+ "specific_count": true,
124
+ "range_count": false
125
+ },
126
+ "contains_groups": {
127
+ "capturing_groups": 0,
128
+ "non_capturing_groups": 0,
129
+ "named_groups": 0
130
+ },
131
+ "contains_character_classes": {
132
+ "predefined_classes": true,
133
+ "custom_classes": false,
134
+ "negated_classes": false
135
+ },
136
+ "contains_special_chars": {
137
+ "wildcard": false,
138
+ "pipe": false,
139
+ "escape_sequences": 3
140
+ }
141
+ },
142
+ "suggestions": [
143
+ "Consider anchoring with ^ or $ if you need exact matches"
144
+ ],
145
+ "common_patterns": [
146
+ {
147
+ "name": "Email Address",
148
+ "pattern": "^[\\w\\.-]+@[\\w\\.-]+\\.[a-zA-Z]{2,}$",
149
+ "description": "Matches valid email addresses",
150
+ "example": "user@example.com"
151
+ },
152
+ {
153
+ "name": "Phone Number (US)",
154
+ "pattern": "^\\(?(\\d{3})\\)?[-.\\s]?(\\d{3})[-.\\s]?(\\d{4})$",
155
+ "description": "Matches US phone numbers in various formats",
156
+ "example": "(123) 456-7890"
157
+ },
158
+ {
159
+ "name": "URL",
160
+ "pattern": "^https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)$",
161
+ "description": "Matches HTTP and HTTPS URLs",
162
+ "example": "https://www.example.com"
163
+ },
164
+ {
165
+ "name": "IP Address (IPv4)",
166
+ "pattern": "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$",
167
+ "description": "Matches valid IPv4 addresses",
168
+ "example": "192.168.1.1"
169
+ },
170
+ {
171
+ "name": "Credit Card Number",
172
+ "pattern": "^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3[0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})$",
173
+ "description": "Matches major credit card formats",
174
+ "example": "4532123456789012"
175
+ },
176
+ {
177
+ "name": "Social Security Number",
178
+ "pattern": "^\\d{3}-?\\d{2}-?\\d{4}$",
179
+ "description": "Matches SSN with or without dashes",
180
+ "example": "123-45-6789"
181
+ },
182
+ {
183
+ "name": "Date (MM/DD/YYYY)",
184
+ "pattern": "^(0[1-9]|1[0-2])\\/(0[1-9]|[12][0-9]|3[01])\\/(19|20)\\d{2}$",
185
+ "description": "Matches MM/DD/YYYY date format",
186
+ "example": "12/31/2023"
187
+ },
188
+ {
189
+ "name": "Time (24-hour)",
190
+ "pattern": "^([01]?[0-9]|2[0-3]):[0-5][0-9]$",
191
+ "description": "Matches 24-hour time format",
192
+ "example": "14:30"
193
+ },
194
+ {
195
+ "name": "Hexadecimal Color",
196
+ "pattern": "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$",
197
+ "description": "Matches hex color codes",
198
+ "example": "#FF5733"
199
+ },
200
+ {
201
+ "name": "Strong Password",
202
+ "pattern": "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$",
203
+ "description": "At least 8 chars with uppercase, lowercase, digit, and special char",
204
+ "example": "MyP@ssw0rd"
205
+ }
206
+ ],
207
+ "regex_guide": {
208
+ "basic_syntax": [
209
+ {
210
+ "symbol": ".",
211
+ "description": "Matches any single character except newline"
212
+ },
213
+ {
214
+ "symbol": "*",
215
+ "description": "Matches 0 or more of the preceding character"
216
+ },
217
+ {
218
+ "symbol": "+",
219
+ "description": "Matches 1 or more of the preceding character"
220
+ },
221
+ {
222
+ "symbol": "?",
223
+ "description": "Matches 0 or 1 of the preceding character"
224
+ },
225
+ {
226
+ "symbol": "^",
227
+ "description": "Matches start of string"
228
+ },
229
+ {
230
+ "symbol": "$",
231
+ "description": "Matches end of string"
232
+ },
233
+ {
234
+ "symbol": "|",
235
+ "description": "OR operator"
236
+ },
237
+ {
238
+ "symbol": "\\",
239
+ "description": "Escape character"
240
+ }
241
+ ],
242
+ "character_classes": [
243
+ {
244
+ "symbol": "[abc]",
245
+ "description": "Matches any character in the set"
246
+ },
247
+ {
248
+ "symbol": "[^abc]",
249
+ "description": "Matches any character NOT in the set"
250
+ },
251
+ {
252
+ "symbol": "[a-z]",
253
+ "description": "Matches any lowercase letter"
254
+ },
255
+ {
256
+ "symbol": "[A-Z]",
257
+ "description": "Matches any uppercase letter"
258
+ },
259
+ {
260
+ "symbol": "[0-9]",
261
+ "description": "Matches any digit"
262
+ },
263
+ {
264
+ "symbol": "\\d",
265
+ "description": "Matches any digit (equivalent to [0-9])"
266
+ },
267
+ {
268
+ "symbol": "\\w",
269
+ "description": "Matches any word character [a-zA-Z0-9_]"
270
+ },
271
+ {
272
+ "symbol": "\\s",
273
+ "description": "Matches any whitespace character"
274
+ }
275
+ ],
276
+ "quantifiers": [
277
+ {
278
+ "symbol": "{n}",
279
+ "description": "Matches exactly n times"
280
+ },
281
+ {
282
+ "symbol": "{n,}",
283
+ "description": "Matches n or more times"
284
+ },
285
+ {
286
+ "symbol": "{n,m}",
287
+ "description": "Matches between n and m times"
288
+ },
289
+ {
290
+ "symbol": "*?",
291
+ "description": "Non-greedy: matches 0 or more (lazy)"
292
+ },
293
+ {
294
+ "symbol": "+?",
295
+ "description": "Non-greedy: matches 1 or more (lazy)"
296
+ },
297
+ {
298
+ "symbol": "??",
299
+ "description": "Non-greedy: matches 0 or 1 (lazy)"
300
+ }
301
+ ],
302
+ "groups": [
303
+ {
304
+ "symbol": "(abc)",
305
+ "description": "Capturing group"
306
+ },
307
+ {
308
+ "symbol": "(?:abc)",
309
+ "description": "Non-capturing group"
310
+ },
311
+ {
312
+ "symbol": "(?<name>abc)",
313
+ "description": "Named capturing group"
314
+ },
315
+ {
316
+ "symbol": "(?=abc)",
317
+ "description": "Positive lookahead"
318
+ },
319
+ {
320
+ "symbol": "(?!abc)",
321
+ "description": "Negative lookahead"
322
+ },
323
+ {
324
+ "symbol": "(?<=abc)",
325
+ "description": "Positive lookbehind"
326
+ },
327
+ {
328
+ "symbol": "(?<!abc)",
329
+ "description": "Negative lookbehind"
330
+ }
331
+ ],
332
+ "flags": [
333
+ {
334
+ "flag": "g",
335
+ "description": "Global - find all matches"
336
+ },
337
+ {
338
+ "flag": "i",
339
+ "description": "Case insensitive"
340
+ },
341
+ {
342
+ "flag": "m",
343
+ "description": "Multiline - ^ and $ match line breaks"
344
+ },
345
+ {
346
+ "flag": "s",
347
+ "description": "Dot matches newline characters"
348
+ },
349
+ {
350
+ "flag": "u",
351
+ "description": "Unicode mode"
352
+ },
353
+ {
354
+ "flag": "y",
355
+ "description": "Sticky - matches from lastIndex position"
356
+ }
357
+ ]
358
+ }
359
+ }
360
+ }
361
+ ```
362
+
363
+ ## Documentation
364
+
365
+ For more information, visit the [API Documentation](https://docs.apiverve.com/ref/regextester?utm_source=rubygems&utm_medium=readme).
366
+
367
+ ## Support
368
+
369
+ - Website: [https://apiverve.com/marketplace/regextester?utm_source=ruby&utm_medium=readme](https://apiverve.com/marketplace/regextester?utm_source=ruby&utm_medium=readme)
370
+ - Email: hello@apiverve.com
371
+
372
+ ## License
373
+
374
+ This gem is available under the [MIT License](LICENSE).
@@ -0,0 +1,184 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "faraday/multipart"
5
+ require "json"
6
+
7
+ module APIVerve
8
+ module Regextester
9
+ # Client for the Regex Tester API
10
+ #
11
+ # @example Basic usage
12
+ # client = APIVerve::Regextester::Client.new(api_key: "your_api_key")
13
+ # response = client.execute({ pattern: "\d{3}-\d{2}-\d{4}", text: "My SSN is 123-45-6789 and my friend's is 987-65-4321", flags: "g" })
14
+ # puts response
15
+ #
16
+ # @see https://apiverve.com/marketplace/regextester?utm_source=ruby&utm_medium=readme
17
+ class Client
18
+ BASE_URL = "https://api.apiverve.com/v1/regextester"
19
+ DEFAULT_TIMEOUT = 30
20
+
21
+ # Validation rules for parameters
22
+ VALIDATION_RULES = { 'pattern' => { type: 'string', required: true }, 'text' => { type: 'string', required: true }, 'flags' => { type: 'string', required: false }, 'test_type' => { type: 'string', required: false }, 'replacement' => { type: 'string', required: false } }
23
+
24
+ # Format validation patterns
25
+ FORMAT_PATTERNS = {
26
+ 'email' => /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
27
+ 'url' => /^https?:\/\/.+/,
28
+ 'ip' => /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/,
29
+ 'date' => /^\d{4}-\d{2}-\d{2}$/,
30
+ 'hexColor' => /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
31
+ }.freeze
32
+
33
+ # Initialize the client
34
+ #
35
+ # @param api_key [String] Your APIVerve API key
36
+ # @param timeout [Integer] Request timeout in seconds (default: 30)
37
+ # @param debug [Boolean] Enable debug logging (default: false)
38
+ # @raise [ArgumentError] If API key is invalid
39
+ def initialize(api_key:, timeout: DEFAULT_TIMEOUT, debug: false)
40
+ validate_api_key!(api_key)
41
+
42
+ @api_key = api_key
43
+ @timeout = timeout
44
+ @debug = debug
45
+
46
+ @connection = Faraday.new(url: BASE_URL) do |conn|
47
+ conn.request :multipart
48
+ conn.request :url_encoded
49
+ conn.adapter Faraday.default_adapter
50
+ conn.options.timeout = @timeout
51
+ conn.headers["x-api-key"] = @api_key
52
+ conn.headers["auth-mode"] = "rubygems-package"
53
+ conn.headers["Content-Type"] = "application/json"
54
+ end
55
+ end
56
+
57
+ # Execute the API request
58
+ #
59
+ # @param params [Hash] Query parameters or request body
60
+ # @return [Hash] API response
61
+ # @raise [APIError] If the request fails
62
+ # @raise [ValidationError] If parameter validation fails
63
+ def execute(params = {})
64
+ validate_params!(params)
65
+
66
+ log("Making POST request to #{BASE_URL}")
67
+ log("Parameters: #{params.inspect}") if params.any?
68
+
69
+ response = @connection.post do |req|
70
+ req.body = params.to_json
71
+ end
72
+
73
+ handle_response(response)
74
+ end
75
+
76
+ private
77
+
78
+ def validate_api_key!(api_key)
79
+ raise ArgumentError, "API key is required. Get your API key at: https://apiverve.com" if api_key.nil? || api_key.strip.empty?
80
+
81
+ unless api_key.match?(/^[a-zA-Z0-9_-]+$/)
82
+ raise ArgumentError, "Invalid API key format. API key should only contain letters, numbers, hyphens, and underscores."
83
+ end
84
+ end
85
+
86
+ def validate_params!(params)
87
+ return if VALIDATION_RULES.empty?
88
+
89
+ errors = []
90
+
91
+ VALIDATION_RULES.each do |param_name, rules|
92
+ value = params[param_name.to_sym] || params[param_name]
93
+
94
+ # Check required
95
+ if rules[:required] && (value.nil? || value.to_s.empty?)
96
+ errors << "Required parameter [#{param_name}] is missing."
97
+ next
98
+ end
99
+
100
+ next if value.nil?
101
+
102
+ case rules[:type]
103
+ when "integer", "number"
104
+ begin
105
+ num_value = rules[:type] == "number" ? Float(value) : Integer(value)
106
+ errors << "Parameter [#{param_name}] must be at least #{rules[:min]}." if rules[:min] && num_value < rules[:min]
107
+ errors << "Parameter [#{param_name}] must be at most #{rules[:max]}." if rules[:max] && num_value > rules[:max]
108
+ rescue ArgumentError, TypeError
109
+ errors << "Parameter [#{param_name}] must be a valid #{rules[:type]}."
110
+ end
111
+ when "string"
112
+ unless value.is_a?(String)
113
+ errors << "Parameter [#{param_name}] must be a string."
114
+ next
115
+ end
116
+ errors << "Parameter [#{param_name}] must be at least #{rules[:min_length]} characters." if rules[:min_length] && value.length < rules[:min_length]
117
+ errors << "Parameter [#{param_name}] must be at most #{rules[:max_length]} characters." if rules[:max_length] && value.length > rules[:max_length]
118
+
119
+ if rules[:format] && FORMAT_PATTERNS[rules[:format]]
120
+ unless value.match?(FORMAT_PATTERNS[rules[:format]])
121
+ errors << "Parameter [#{param_name}] must be a valid #{rules[:format]}."
122
+ end
123
+ end
124
+ when "boolean"
125
+ unless [true, false, "true", "false"].include?(value)
126
+ errors << "Parameter [#{param_name}] must be a boolean."
127
+ end
128
+ end
129
+
130
+ # Enum validation
131
+ if rules[:enum] && !rules[:enum].include?(value)
132
+ errors << "Parameter [#{param_name}] must be one of: #{rules[:enum].join(', ')}."
133
+ end
134
+ end
135
+
136
+ raise ValidationError, errors unless errors.empty?
137
+ end
138
+
139
+ def handle_response(response)
140
+ log("Response status: #{response.status}")
141
+
142
+ data = JSON.parse(response.body)
143
+
144
+ if data["status"] == "error"
145
+ raise APIError.new(data["error"] || "Unknown API error", response.status, data)
146
+ end
147
+
148
+ unless response.success?
149
+ raise APIError.new(data["error"] || "HTTP #{response.status} error", response.status, data)
150
+ end
151
+
152
+ log("Request successful")
153
+ data
154
+ rescue JSON::ParserError => e
155
+ raise APIError.new("Invalid JSON response: #{e.message}", response.status)
156
+ end
157
+
158
+ def log(message)
159
+ puts "[APIVerve::Regextester] #{message}" if @debug
160
+ end
161
+ end
162
+
163
+ # Custom error class for API errors
164
+ class APIError < StandardError
165
+ attr_reader :status_code, :response
166
+
167
+ def initialize(message, status_code = nil, response = nil)
168
+ @status_code = status_code
169
+ @response = response
170
+ super(message)
171
+ end
172
+ end
173
+
174
+ # Custom error class for validation errors
175
+ class ValidationError < StandardError
176
+ attr_reader :errors
177
+
178
+ def initialize(errors)
179
+ @errors = errors
180
+ super("Validation failed: #{errors.join(' ')}")
181
+ end
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "faraday/multipart"
5
+ require "json"
6
+
7
+ module APIVerve
8
+ module Regextester
9
+ # Client for the Regex Tester API
10
+ #
11
+ # @example Basic usage
12
+ # client = APIVerve::Regextester::Client.new(api_key: "your_api_key")
13
+ # response = client.execute({ pattern: "\d{3}-\d{2}-\d{4}", text: "My SSN is 123-45-6789 and my friend's is 987-65-4321", flags: "g" })
14
+ # puts response
15
+ #
16
+ # @see https://apiverve.com/marketplace/regextester?utm_source=ruby&utm_medium=readme
17
+ class Client
18
+ BASE_URL = "https://api.apiverve.com/v1/regextester"
19
+ DEFAULT_TIMEOUT = 30
20
+
21
+ # Validation rules for parameters
22
+ VALIDATION_RULES = {
23
+ 'pattern' => { type: 'string', required: true },
24
+ 'text' => { type: 'string', required: true },
25
+ 'flags' => { type: 'string', required: false },
26
+ 'test_type' => { type: 'string', required: false },
27
+ 'replacement' => { type: 'string', required: false }
28
+ }
29
+
30
+ # Format validation patterns
31
+ FORMAT_PATTERNS = {
32
+ 'email' => /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
33
+ 'url' => /^https?:\/\/.+/,
34
+ 'ip' => /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/,
35
+ 'date' => /^\d{4}-\d{2}-\d{2}$/,
36
+ 'hexColor' => /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
37
+ }.freeze
38
+
39
+ # Initialize the client
40
+ #
41
+ # @param api_key [String] Your APIVerve API key
42
+ # @param timeout [Integer] Request timeout in seconds (default: 30)
43
+ # @param debug [Boolean] Enable debug logging (default: false)
44
+ # @raise [ArgumentError] If API key is invalid
45
+ def initialize(api_key:, timeout: DEFAULT_TIMEOUT, debug: false)
46
+ validate_api_key!(api_key)
47
+
48
+ @api_key = api_key
49
+ @timeout = timeout
50
+ @debug = debug
51
+
52
+ @connection = Faraday.new(url: BASE_URL) do |conn|
53
+ conn.request :multipart
54
+ conn.request :url_encoded
55
+ conn.adapter Faraday.default_adapter
56
+ conn.options.timeout = @timeout
57
+ conn.headers["x-api-key"] = @api_key
58
+ conn.headers["auth-mode"] = "rubygems-package"
59
+ conn.headers["Content-Type"] = "application/json"
60
+ end
61
+ end
62
+
63
+ # Execute the API request
64
+ #
65
+ # @param params [Hash] Query parameters or request body
66
+ # @return [Hash] API response
67
+ # @raise [APIError] If the request fails
68
+ # @raise [ValidationError] If parameter validation fails
69
+ def execute(params = {})
70
+ validate_params!(params)
71
+
72
+ log("Making POST request to #{BASE_URL}")
73
+ log("Parameters: #{params.inspect}") if params.any?
74
+
75
+ response = @connection.post do |req|
76
+ req.body = params.to_json
77
+ end
78
+
79
+ handle_response(response)
80
+ end
81
+
82
+ private
83
+
84
+ def validate_api_key!(api_key)
85
+ raise ArgumentError, "API key is required. Get your API key at: https://apiverve.com" if api_key.nil? || api_key.strip.empty?
86
+
87
+ unless api_key.match?(/^[a-zA-Z0-9_-]+$/)
88
+ raise ArgumentError, "Invalid API key format. API key should only contain letters, numbers, hyphens, and underscores."
89
+ end
90
+ end
91
+
92
+ def validate_params!(params)
93
+ return if VALIDATION_RULES.empty?
94
+
95
+ errors = []
96
+
97
+ VALIDATION_RULES.each do |param_name, rules|
98
+ value = params[param_name.to_sym] || params[param_name]
99
+
100
+ # Check required
101
+ if rules[:required] && (value.nil? || value.to_s.empty?)
102
+ errors << "Required parameter [#{param_name}] is missing."
103
+ next
104
+ end
105
+
106
+ next if value.nil?
107
+
108
+ case rules[:type]
109
+ when "integer", "number"
110
+ begin
111
+ num_value = rules[:type] == "number" ? Float(value) : Integer(value)
112
+ errors << "Parameter [#{param_name}] must be at least #{rules[:min]}." if rules[:min] && num_value < rules[:min]
113
+ errors << "Parameter [#{param_name}] must be at most #{rules[:max]}." if rules[:max] && num_value > rules[:max]
114
+ rescue ArgumentError, TypeError
115
+ errors << "Parameter [#{param_name}] must be a valid #{rules[:type]}."
116
+ end
117
+ when "string"
118
+ unless value.is_a?(String)
119
+ errors << "Parameter [#{param_name}] must be a string."
120
+ next
121
+ end
122
+ errors << "Parameter [#{param_name}] must be at least #{rules[:min_length]} characters." if rules[:min_length] && value.length < rules[:min_length]
123
+ errors << "Parameter [#{param_name}] must be at most #{rules[:max_length]} characters." if rules[:max_length] && value.length > rules[:max_length]
124
+
125
+ if rules[:format] && FORMAT_PATTERNS[rules[:format]]
126
+ unless value.match?(FORMAT_PATTERNS[rules[:format]])
127
+ errors << "Parameter [#{param_name}] must be a valid #{rules[:format]}."
128
+ end
129
+ end
130
+ when "boolean"
131
+ unless [true, false, "true", "false"].include?(value)
132
+ errors << "Parameter [#{param_name}] must be a boolean."
133
+ end
134
+ end
135
+
136
+ # Enum validation
137
+ if rules[:enum] && !rules[:enum].include?(value)
138
+ errors << "Parameter [#{param_name}] must be one of: #{rules[:enum].join(', ')}."
139
+ end
140
+ end
141
+
142
+ raise ValidationError, errors unless errors.empty?
143
+ end
144
+
145
+ def handle_response(response)
146
+ log("Response status: #{response.status}")
147
+
148
+ data = JSON.parse(response.body)
149
+
150
+ if data["status"] == "error"
151
+ raise APIError.new(data["error"] || "Unknown API error", response.status, data)
152
+ end
153
+
154
+ unless response.success?
155
+ raise APIError.new(data["error"] || "HTTP #{response.status} error", response.status, data)
156
+ end
157
+
158
+ log("Request successful")
159
+ data
160
+ rescue JSON::ParserError => e
161
+ raise APIError.new("Invalid JSON response: #{e.message}", response.status)
162
+ end
163
+
164
+ def log(message)
165
+ puts "[APIVerve::Regextester] #{message}" if @debug
166
+ end
167
+ end
168
+
169
+ # Custom error class for API errors
170
+ class APIError < StandardError
171
+ attr_reader :status_code, :response
172
+
173
+ def initialize(message, status_code = nil, response = nil)
174
+ @status_code = status_code
175
+ @response = response
176
+ super(message)
177
+ end
178
+ end
179
+
180
+ # Custom error class for validation errors
181
+ class ValidationError < StandardError
182
+ attr_reader :errors
183
+
184
+ def initialize(errors)
185
+ @errors = errors
186
+ super("Validation failed: #{errors.join(' ')}")
187
+ end
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "apiverve_regextester/client"
4
+
5
+ module APIVerve
6
+ module Regextester
7
+ VERSION = "1.2.0"
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apiverve_regextester
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - APIVerve
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: faraday
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.0'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '1.0'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '3.0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: faraday-multipart
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: '1.0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - "~>"
44
+ - !ruby/object:Gem::Version
45
+ version: '1.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '2.0'
53
+ type: :runtime
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '2.0'
60
+ description: Regex Tester is a comprehensive tool for testing and validating regular
61
+ expressions. It supports multiple operations (test, match, search, replace, split)
62
+ with detailed performance analysis and pattern suggestions.
63
+ email:
64
+ - hello@apiverve.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - LICENSE
70
+ - README.md
71
+ - lib/apiverve/client.rb
72
+ - lib/apiverve_regextester.rb
73
+ - lib/apiverve_regextester/client.rb
74
+ homepage: https://apiverve.com/marketplace/regextester?utm_source=ruby&utm_medium=homepage
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://apiverve.com/marketplace/regextester?utm_source=ruby&utm_medium=homepage
79
+ source_code_uri: https://github.com/apiverve/regextester-API/tree/main/ruby
80
+ changelog_uri: https://apiverve.com/changelog
81
+ documentation_uri: https://docs.apiverve.com/ref/regextester
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.7.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.6.9
97
+ specification_version: 4
98
+ summary: Regex Tester API - Ruby Client
99
+ test_files: []