apiverve_readingtime 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 +7 -0
- data/LICENSE +21 -0
- data/README.md +100 -0
- data/lib/apiverve/client.rb +184 -0
- data/lib/apiverve_readingtime/client.rb +187 -0
- data/lib/apiverve_readingtime.rb +9 -0
- metadata +99 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: dc06ce8f696bff92931d5d132be933d90d3fdc7343361a4716f13faad173ba29
|
|
4
|
+
data.tar.gz: 0f8628311d36d57ea34e874d637ed80dff51d52c5e66312271ffeaa946ee6aad
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f7f65b69fc5475ebd16d5a8a044c8eb5fb5595e18c4045a5f95427188a2b546d3fd6e5f6401b84bc598d4de20fad5e58f20b41130095f032baaa0bb1f074d2f4
|
|
7
|
+
data.tar.gz: a1c68e81469e460db3372e7aad1535745301ea23ecd9d69132ee66f310dc876c52607f33daed068a94ba26fc583072ca4f5e21965afcdaf88e8c3e020a9d4132
|
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,100 @@
|
|
|
1
|
+
# Reading Time Calculator API - Ruby Gem
|
|
2
|
+
|
|
3
|
+
Reading Time Calculator estimates how long it will take to read a given text. It calculates reading time based on word count and customizable reading speed (WPM).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'apiverve_readingtime'
|
|
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_readingtime
|
|
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_readingtime'
|
|
33
|
+
|
|
34
|
+
# Initialize the client
|
|
35
|
+
client = APIVerve::Readingtime::Client.new(api_key: "YOUR_API_KEY")
|
|
36
|
+
|
|
37
|
+
# Make a request
|
|
38
|
+
response = client.execute({
|
|
39
|
+
text: "This is a sample article with multiple paragraphs. Reading time will be calculated based on word count. The average reading speed is 250 words per minute, which can be customized. This helps content creators provide accurate time estimates to their readers.",
|
|
40
|
+
wpm: 250
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
# Print the response
|
|
44
|
+
puts response
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
### Error Handling
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
begin
|
|
52
|
+
response = client.execute({ text: "This is a sample article with multiple paragraphs. Reading time will be calculated based on word count. The average reading speed is 250 words per minute, which can be customized. This helps content creators provide accurate time estimates to their readers.", wpm: 250 })
|
|
53
|
+
puts response["data"]
|
|
54
|
+
rescue APIVerve::Readingtime::ValidationError => e
|
|
55
|
+
puts "Validation error: #{e.errors.join(', ')}"
|
|
56
|
+
rescue APIVerve::Readingtime::APIError => e
|
|
57
|
+
puts "API error: #{e.message}"
|
|
58
|
+
puts "Status code: #{e.status_code}"
|
|
59
|
+
end
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Debug Mode
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
# Enable debug logging
|
|
66
|
+
client = APIVerve::Readingtime::Client.new(
|
|
67
|
+
api_key: "YOUR_API_KEY",
|
|
68
|
+
debug: true
|
|
69
|
+
)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Example Response
|
|
73
|
+
|
|
74
|
+
```json
|
|
75
|
+
{
|
|
76
|
+
"status": "ok",
|
|
77
|
+
"error": null,
|
|
78
|
+
"data": {
|
|
79
|
+
"text_length": 257,
|
|
80
|
+
"word_count": 41,
|
|
81
|
+
"reading_time_minutes": 1,
|
|
82
|
+
"reading_time_seconds": 10,
|
|
83
|
+
"reading_time_text": "1 min read",
|
|
84
|
+
"words_per_minute": 250
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Documentation
|
|
90
|
+
|
|
91
|
+
For more information, visit the [API Documentation](https://docs.apiverve.com/ref/readingtime?utm_source=rubygems&utm_medium=readme).
|
|
92
|
+
|
|
93
|
+
## Support
|
|
94
|
+
|
|
95
|
+
- Website: [https://apiverve.com/marketplace/readingtime?utm_source=ruby&utm_medium=readme](https://apiverve.com/marketplace/readingtime?utm_source=ruby&utm_medium=readme)
|
|
96
|
+
- Email: hello@apiverve.com
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
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 Readingtime
|
|
9
|
+
# Client for the Reading Time Calculator API
|
|
10
|
+
#
|
|
11
|
+
# @example Basic usage
|
|
12
|
+
# client = APIVerve::Readingtime::Client.new(api_key: "your_api_key")
|
|
13
|
+
# response = client.execute({ text: "This is a sample article with multiple paragraphs. Reading time will be calculated based on word count. The average reading speed is 250 words per minute, which can be customized. This helps content creators provide accurate time estimates to their readers.", wpm: 250 })
|
|
14
|
+
# puts response
|
|
15
|
+
#
|
|
16
|
+
# @see https://apiverve.com/marketplace/readingtime?utm_source=ruby&utm_medium=readme
|
|
17
|
+
class Client
|
|
18
|
+
BASE_URL = "https://api.apiverve.com/v1/readingtime"
|
|
19
|
+
DEFAULT_TIMEOUT = 30
|
|
20
|
+
|
|
21
|
+
# Validation rules for parameters
|
|
22
|
+
VALIDATION_RULES = { 'text' => { type: 'string', required: true, max_length: 500000 }, 'wpm' => { type: 'integer', required: false, min: 50, max: 1000 } }
|
|
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::Readingtime] #{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,187 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "faraday"
|
|
4
|
+
require "faraday/multipart"
|
|
5
|
+
require "json"
|
|
6
|
+
|
|
7
|
+
module APIVerve
|
|
8
|
+
module Readingtime
|
|
9
|
+
# Client for the Reading Time Calculator API
|
|
10
|
+
#
|
|
11
|
+
# @example Basic usage
|
|
12
|
+
# client = APIVerve::Readingtime::Client.new(api_key: "your_api_key")
|
|
13
|
+
# response = client.execute({ text: "This is a sample article with multiple paragraphs. Reading time will be calculated based on word count. The average reading speed is 250 words per minute, which can be customized. This helps content creators provide accurate time estimates to their readers.", wpm: 250 })
|
|
14
|
+
# puts response
|
|
15
|
+
#
|
|
16
|
+
# @see https://apiverve.com/marketplace/readingtime?utm_source=ruby&utm_medium=readme
|
|
17
|
+
class Client
|
|
18
|
+
BASE_URL = "https://api.apiverve.com/v1/readingtime"
|
|
19
|
+
DEFAULT_TIMEOUT = 30
|
|
20
|
+
|
|
21
|
+
# Validation rules for parameters
|
|
22
|
+
VALIDATION_RULES = {
|
|
23
|
+
'text' => { type: 'string', required: true, max_length: 500000 },
|
|
24
|
+
'wpm' => { type: 'integer', required: false, min: 50, max: 1000 }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
# Format validation patterns
|
|
28
|
+
FORMAT_PATTERNS = {
|
|
29
|
+
'email' => /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
30
|
+
'url' => /^https?:\/\/.+/,
|
|
31
|
+
'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}$/,
|
|
32
|
+
'date' => /^\d{4}-\d{2}-\d{2}$/,
|
|
33
|
+
'hexColor' => /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
|
|
34
|
+
}.freeze
|
|
35
|
+
|
|
36
|
+
# Initialize the client
|
|
37
|
+
#
|
|
38
|
+
# @param api_key [String] Your APIVerve API key
|
|
39
|
+
# @param timeout [Integer] Request timeout in seconds (default: 30)
|
|
40
|
+
# @param debug [Boolean] Enable debug logging (default: false)
|
|
41
|
+
# @raise [ArgumentError] If API key is invalid
|
|
42
|
+
def initialize(api_key:, timeout: DEFAULT_TIMEOUT, debug: false)
|
|
43
|
+
validate_api_key!(api_key)
|
|
44
|
+
|
|
45
|
+
@api_key = api_key
|
|
46
|
+
@timeout = timeout
|
|
47
|
+
@debug = debug
|
|
48
|
+
|
|
49
|
+
@connection = Faraday.new(url: BASE_URL) do |conn|
|
|
50
|
+
conn.request :multipart
|
|
51
|
+
conn.request :url_encoded
|
|
52
|
+
conn.adapter Faraday.default_adapter
|
|
53
|
+
conn.options.timeout = @timeout
|
|
54
|
+
conn.headers["x-api-key"] = @api_key
|
|
55
|
+
conn.headers["auth-mode"] = "rubygems-package"
|
|
56
|
+
conn.headers["Content-Type"] = "application/json"
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Execute the API request
|
|
61
|
+
#
|
|
62
|
+
# @param params [Hash] Query parameters or request body
|
|
63
|
+
# @return [Hash] API response
|
|
64
|
+
# @raise [APIError] If the request fails
|
|
65
|
+
# @raise [ValidationError] If parameter validation fails
|
|
66
|
+
def execute(params = {})
|
|
67
|
+
validate_params!(params)
|
|
68
|
+
|
|
69
|
+
log("Making POST request to #{BASE_URL}")
|
|
70
|
+
log("Parameters: #{params.inspect}") if params.any?
|
|
71
|
+
|
|
72
|
+
response = @connection.post do |req|
|
|
73
|
+
req.body = params.to_json
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
handle_response(response)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def validate_api_key!(api_key)
|
|
82
|
+
raise ArgumentError, "API key is required. Get your API key at: https://apiverve.com" if api_key.nil? || api_key.strip.empty?
|
|
83
|
+
|
|
84
|
+
unless api_key.match?(/^[a-zA-Z0-9_-]+$/)
|
|
85
|
+
raise ArgumentError, "Invalid API key format. API key should only contain letters, numbers, hyphens, and underscores."
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def validate_params!(params)
|
|
90
|
+
return if VALIDATION_RULES.empty?
|
|
91
|
+
|
|
92
|
+
errors = []
|
|
93
|
+
|
|
94
|
+
VALIDATION_RULES.each do |param_name, rules|
|
|
95
|
+
value = params[param_name.to_sym] || params[param_name]
|
|
96
|
+
|
|
97
|
+
# Check required
|
|
98
|
+
if rules[:required] && (value.nil? || value.to_s.empty?)
|
|
99
|
+
errors << "Required parameter [#{param_name}] is missing."
|
|
100
|
+
next
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
next if value.nil?
|
|
104
|
+
|
|
105
|
+
case rules[:type]
|
|
106
|
+
when "integer", "number"
|
|
107
|
+
begin
|
|
108
|
+
num_value = rules[:type] == "number" ? Float(value) : Integer(value)
|
|
109
|
+
errors << "Parameter [#{param_name}] must be at least #{rules[:min]}." if rules[:min] && num_value < rules[:min]
|
|
110
|
+
errors << "Parameter [#{param_name}] must be at most #{rules[:max]}." if rules[:max] && num_value > rules[:max]
|
|
111
|
+
rescue ArgumentError, TypeError
|
|
112
|
+
errors << "Parameter [#{param_name}] must be a valid #{rules[:type]}."
|
|
113
|
+
end
|
|
114
|
+
when "string"
|
|
115
|
+
unless value.is_a?(String)
|
|
116
|
+
errors << "Parameter [#{param_name}] must be a string."
|
|
117
|
+
next
|
|
118
|
+
end
|
|
119
|
+
errors << "Parameter [#{param_name}] must be at least #{rules[:min_length]} characters." if rules[:min_length] && value.length < rules[:min_length]
|
|
120
|
+
errors << "Parameter [#{param_name}] must be at most #{rules[:max_length]} characters." if rules[:max_length] && value.length > rules[:max_length]
|
|
121
|
+
|
|
122
|
+
if rules[:format] && FORMAT_PATTERNS[rules[:format]]
|
|
123
|
+
unless value.match?(FORMAT_PATTERNS[rules[:format]])
|
|
124
|
+
errors << "Parameter [#{param_name}] must be a valid #{rules[:format]}."
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
when "boolean"
|
|
128
|
+
unless [true, false, "true", "false"].include?(value)
|
|
129
|
+
errors << "Parameter [#{param_name}] must be a boolean."
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Enum validation
|
|
134
|
+
if rules[:enum] && !rules[:enum].include?(value)
|
|
135
|
+
errors << "Parameter [#{param_name}] must be one of: #{rules[:enum].join(', ')}."
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
raise ValidationError, errors unless errors.empty?
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def handle_response(response)
|
|
143
|
+
log("Response status: #{response.status}")
|
|
144
|
+
|
|
145
|
+
data = JSON.parse(response.body)
|
|
146
|
+
|
|
147
|
+
if data["status"] == "error"
|
|
148
|
+
raise APIError.new(data["error"] || "Unknown API error", response.status, data)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
unless response.success?
|
|
152
|
+
raise APIError.new(data["error"] || "HTTP #{response.status} error", response.status, data)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
log("Request successful")
|
|
156
|
+
data
|
|
157
|
+
rescue JSON::ParserError => e
|
|
158
|
+
raise APIError.new("Invalid JSON response: #{e.message}", response.status)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def log(message)
|
|
162
|
+
puts "[APIVerve::Readingtime] #{message}" if @debug
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Custom error class for API errors
|
|
167
|
+
class APIError < StandardError
|
|
168
|
+
attr_reader :status_code, :response
|
|
169
|
+
|
|
170
|
+
def initialize(message, status_code = nil, response = nil)
|
|
171
|
+
@status_code = status_code
|
|
172
|
+
@response = response
|
|
173
|
+
super(message)
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Custom error class for validation errors
|
|
178
|
+
class ValidationError < StandardError
|
|
179
|
+
attr_reader :errors
|
|
180
|
+
|
|
181
|
+
def initialize(errors)
|
|
182
|
+
@errors = errors
|
|
183
|
+
super("Validation failed: #{errors.join(' ')}")
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: apiverve_readingtime
|
|
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: Reading Time Calculator estimates how long it will take to read a given
|
|
61
|
+
text. It calculates reading time based on word count and customizable reading speed
|
|
62
|
+
(WPM).
|
|
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_readingtime.rb
|
|
73
|
+
- lib/apiverve_readingtime/client.rb
|
|
74
|
+
homepage: https://apiverve.com/marketplace/readingtime?utm_source=ruby&utm_medium=homepage
|
|
75
|
+
licenses:
|
|
76
|
+
- MIT
|
|
77
|
+
metadata:
|
|
78
|
+
homepage_uri: https://apiverve.com/marketplace/readingtime?utm_source=ruby&utm_medium=homepage
|
|
79
|
+
source_code_uri: https://github.com/apiverve/readingtime-API/tree/main/ruby
|
|
80
|
+
changelog_uri: https://apiverve.com/changelog
|
|
81
|
+
documentation_uri: https://docs.apiverve.com/ref/readingtime
|
|
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: Reading Time Calculator API - Ruby Client
|
|
99
|
+
test_files: []
|