apiverve_exercises 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 +174 -0
- data/lib/apiverve/client.rb +184 -0
- data/lib/apiverve_exercises/client.rb +188 -0
- data/lib/apiverve_exercises.rb +9 -0
- metadata +98 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6a2efba81d82aafbc276ef333ba94abea7d04e0fce24e65bd5a2f65ec4b2ec5c
|
|
4
|
+
data.tar.gz: 2cf6478114b690be136a1653c2f33c9e5c3b3a22478f609bb92b00a45c9ba089
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3720618d760c425b5563eff6a060361ae49cc2ce239e2425952834098a75470bd4d64f43e9b095712d4d97775b9d164d28d9695b30470884180e5ca9fa56790a
|
|
7
|
+
data.tar.gz: 61cdc240e6d7e2ba1e613b4d05fa1249f14fffb7ceb67c64c1a7a56ee660ddd2bdceae24e252857a8535fd9d046ff6e59a981f51d09da3b37342df7b5052c296
|
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,174 @@
|
|
|
1
|
+
# Exercises API - Ruby Gem
|
|
2
|
+
|
|
3
|
+
Exercises is a simple tool for getting exercise information. It returns information on various exercises.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'apiverve_exercises'
|
|
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_exercises
|
|
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_exercises'
|
|
33
|
+
|
|
34
|
+
# Initialize the client
|
|
35
|
+
client = APIVerve::Exercises::Client.new(api_key: "YOUR_API_KEY")
|
|
36
|
+
|
|
37
|
+
# Make a request
|
|
38
|
+
response = client.execute({
|
|
39
|
+
muscle: "chest",
|
|
40
|
+
name: "barbell",
|
|
41
|
+
equipment: "barbell"
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
# Print the response
|
|
45
|
+
puts response
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
### Error Handling
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
begin
|
|
53
|
+
response = client.execute({ muscle: "chest", name: "barbell", equipment: "barbell" })
|
|
54
|
+
puts response["data"]
|
|
55
|
+
rescue APIVerve::Exercises::ValidationError => e
|
|
56
|
+
puts "Validation error: #{e.errors.join(', ')}"
|
|
57
|
+
rescue APIVerve::Exercises::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::Exercises::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
|
+
"count": 5,
|
|
81
|
+
"filteredOn": [
|
|
82
|
+
"name",
|
|
83
|
+
"muscle"
|
|
84
|
+
],
|
|
85
|
+
"exercises": [
|
|
86
|
+
{
|
|
87
|
+
"name": "Barbell Ab Rollout",
|
|
88
|
+
"force": "pull",
|
|
89
|
+
"level": "intermediate",
|
|
90
|
+
"mechanic": "compound",
|
|
91
|
+
"equipment": "barbell",
|
|
92
|
+
"instructions": [
|
|
93
|
+
"For this exercise you will need to get into a pushup position, but instead of having your hands of the floor, you will be grabbing on to an Olympic barbell (loaded with 5-10 lbs on each side) instead. This will be your starting position.",
|
|
94
|
+
"While keeping a slight arch on your back, lift your hips and roll the barbell towards your feet as you exhale. Tip: As you perform the movement, your glutes should be coming up, you should be keeping the abs tight and should maintain your back posture at all times. Also your arms should be staying perpendicular to the floor throughout the movement. If you don't, you will work out your shoulders and back more than the abs.",
|
|
95
|
+
"After a second contraction at the top, start to roll the barbell back forward to the starting position slowly as you inhale.",
|
|
96
|
+
"Repeat for the recommended amount of repetitions."
|
|
97
|
+
],
|
|
98
|
+
"muscle": "biceps"
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"name": "Barbell Ab Rollout - On Knees",
|
|
102
|
+
"force": "pull",
|
|
103
|
+
"level": "expert",
|
|
104
|
+
"mechanic": "compound",
|
|
105
|
+
"equipment": "barbell",
|
|
106
|
+
"instructions": [
|
|
107
|
+
"Hold an Olympic barbell loaded with 5-10lbs on each side and kneel on the floor.",
|
|
108
|
+
"Now place the barbell on the floor in front of you so that you are on all your hands and knees (as in a kneeling push up position). This will be your starting position.",
|
|
109
|
+
"Slowly roll the barbell straight forward, stretching your body into a straight position. Tip: Go down as far as you can without touching the floor with your body. Breathe in during this portion of the movement.",
|
|
110
|
+
"After a second pause at the stretched position, start pulling yourself back to the starting position as you breathe out. Tip: Go slowly and keep your abs tight at all times."
|
|
111
|
+
],
|
|
112
|
+
"muscle": "biceps"
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"name": "Barbell Bench Press - Medium Grip",
|
|
116
|
+
"force": "push",
|
|
117
|
+
"level": "beginner",
|
|
118
|
+
"mechanic": "compound",
|
|
119
|
+
"equipment": "barbell",
|
|
120
|
+
"instructions": [
|
|
121
|
+
"Lie back on a flat bench. Using a medium width grip (a grip that creates a 90-degree angle in the middle of the movement between the forearms and the upper arms), lift the bar from the rack and hold it straight over you with your arms locked. This will be your starting position.",
|
|
122
|
+
"From the starting position, breathe in and begin coming down slowly until the bar touches your middle chest.",
|
|
123
|
+
"After a brief pause, push the bar back to the starting position as you breathe out. Focus on pushing the bar using your chest muscles. Lock your arms and squeeze your chest in the contracted position at the top of the motion, hold for a second and then start coming down slowly again. Tip: Ideally, lowering the weight should take about twice as long as raising it.",
|
|
124
|
+
"Repeat the movement for the prescribed amount of repetitions.",
|
|
125
|
+
"When you are done, place the bar back in the rack."
|
|
126
|
+
],
|
|
127
|
+
"muscle": "biceps"
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
"name": "Barbell Curl",
|
|
131
|
+
"force": "pull",
|
|
132
|
+
"level": "beginner",
|
|
133
|
+
"mechanic": "isolation",
|
|
134
|
+
"equipment": "barbell",
|
|
135
|
+
"instructions": [
|
|
136
|
+
"Stand up with your torso upright while holding a barbell at a shoulder-width grip. The palm of your hands should be facing forward and the elbows should be close to the torso. This will be your starting position.",
|
|
137
|
+
"While holding the upper arms stationary, curl the weights forward while contracting the biceps as you breathe out. Tip: Only the forearms should move.",
|
|
138
|
+
"Continue the movement until your biceps are fully contracted and the bar is at shoulder level. Hold the contracted position for a second and squeeze the biceps hard.",
|
|
139
|
+
"Slowly begin to bring the bar back to starting position as your breathe in.",
|
|
140
|
+
"Repeat for the recommended amount of repetitions."
|
|
141
|
+
],
|
|
142
|
+
"muscle": "biceps"
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
"name": "Barbell Curls Lying Against An Incline",
|
|
146
|
+
"force": "pull",
|
|
147
|
+
"level": "beginner",
|
|
148
|
+
"mechanic": "isolation",
|
|
149
|
+
"equipment": "barbell",
|
|
150
|
+
"instructions": [
|
|
151
|
+
"Lie against an incline bench, with your arms holding a barbell and hanging down in a horizontal line. This will be your starting position.",
|
|
152
|
+
"While keeping the upper arms stationary, curl the weight up as high as you can while squeezing the biceps. Breathe out as you perform this portion of the movement. Tip: Only the forearms should move. Do not swing the arms.",
|
|
153
|
+
"After a second contraction, slowly go back to the starting position as you inhale. Tip: Make sure that you go all of the way down.",
|
|
154
|
+
"Repeat for the recommended amount of repetitions."
|
|
155
|
+
],
|
|
156
|
+
"muscle": "biceps"
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Documentation
|
|
164
|
+
|
|
165
|
+
For more information, visit the [API Documentation](https://docs.apiverve.com/ref/exercises?utm_source=rubygems&utm_medium=readme).
|
|
166
|
+
|
|
167
|
+
## Support
|
|
168
|
+
|
|
169
|
+
- Website: [https://apiverve.com/marketplace/exercises?utm_source=ruby&utm_medium=readme](https://apiverve.com/marketplace/exercises?utm_source=ruby&utm_medium=readme)
|
|
170
|
+
- Email: hello@apiverve.com
|
|
171
|
+
|
|
172
|
+
## License
|
|
173
|
+
|
|
174
|
+
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 Exercises
|
|
9
|
+
# Client for the Exercises API
|
|
10
|
+
#
|
|
11
|
+
# @example Basic usage
|
|
12
|
+
# client = APIVerve::Exercises::Client.new(api_key: "your_api_key")
|
|
13
|
+
# response = client.execute({ muscle: "chest", name: "barbell", equipment: "barbell" })
|
|
14
|
+
# puts response
|
|
15
|
+
#
|
|
16
|
+
# @see https://apiverve.com/marketplace/exercises?utm_source=ruby&utm_medium=readme
|
|
17
|
+
class Client
|
|
18
|
+
BASE_URL = "https://api.apiverve.com/v1/exercises"
|
|
19
|
+
DEFAULT_TIMEOUT = 30
|
|
20
|
+
|
|
21
|
+
# Validation rules for parameters
|
|
22
|
+
VALIDATION_RULES = { 'muscle' => { type: 'string', required: true }, 'name' => { type: 'string', required: false }, 'equipment' => { 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 GET request to #{BASE_URL}")
|
|
67
|
+
log("Parameters: #{params.inspect}") if params.any?
|
|
68
|
+
|
|
69
|
+
response = @connection.get do |req|
|
|
70
|
+
params.each { |k, v| req.params[k.to_s] = v }
|
|
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::Exercises] #{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,188 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "faraday"
|
|
4
|
+
require "faraday/multipart"
|
|
5
|
+
require "json"
|
|
6
|
+
|
|
7
|
+
module APIVerve
|
|
8
|
+
module Exercises
|
|
9
|
+
# Client for the Exercises API
|
|
10
|
+
#
|
|
11
|
+
# @example Basic usage
|
|
12
|
+
# client = APIVerve::Exercises::Client.new(api_key: "your_api_key")
|
|
13
|
+
# response = client.execute({ muscle: "chest", name: "barbell", equipment: "barbell" })
|
|
14
|
+
# puts response
|
|
15
|
+
#
|
|
16
|
+
# @see https://apiverve.com/marketplace/exercises?utm_source=ruby&utm_medium=readme
|
|
17
|
+
class Client
|
|
18
|
+
BASE_URL = "https://api.apiverve.com/v1/exercises"
|
|
19
|
+
DEFAULT_TIMEOUT = 30
|
|
20
|
+
|
|
21
|
+
# Validation rules for parameters
|
|
22
|
+
VALIDATION_RULES = {
|
|
23
|
+
'muscle' => { type: 'string', required: false },
|
|
24
|
+
'name' => { type: 'string', required: false },
|
|
25
|
+
'equipment' => { type: 'string', required: false }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
# Format validation patterns
|
|
29
|
+
FORMAT_PATTERNS = {
|
|
30
|
+
'email' => /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
31
|
+
'url' => /^https?:\/\/.+/,
|
|
32
|
+
'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}$/,
|
|
33
|
+
'date' => /^\d{4}-\d{2}-\d{2}$/,
|
|
34
|
+
'hexColor' => /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
|
|
35
|
+
}.freeze
|
|
36
|
+
|
|
37
|
+
# Initialize the client
|
|
38
|
+
#
|
|
39
|
+
# @param api_key [String] Your APIVerve API key
|
|
40
|
+
# @param timeout [Integer] Request timeout in seconds (default: 30)
|
|
41
|
+
# @param debug [Boolean] Enable debug logging (default: false)
|
|
42
|
+
# @raise [ArgumentError] If API key is invalid
|
|
43
|
+
def initialize(api_key:, timeout: DEFAULT_TIMEOUT, debug: false)
|
|
44
|
+
validate_api_key!(api_key)
|
|
45
|
+
|
|
46
|
+
@api_key = api_key
|
|
47
|
+
@timeout = timeout
|
|
48
|
+
@debug = debug
|
|
49
|
+
|
|
50
|
+
@connection = Faraday.new(url: BASE_URL) do |conn|
|
|
51
|
+
conn.request :multipart
|
|
52
|
+
conn.request :url_encoded
|
|
53
|
+
conn.adapter Faraday.default_adapter
|
|
54
|
+
conn.options.timeout = @timeout
|
|
55
|
+
conn.headers["x-api-key"] = @api_key
|
|
56
|
+
conn.headers["auth-mode"] = "rubygems-package"
|
|
57
|
+
conn.headers["Content-Type"] = "application/json"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Execute the API request
|
|
62
|
+
#
|
|
63
|
+
# @param params [Hash] Query parameters or request body
|
|
64
|
+
# @return [Hash] API response
|
|
65
|
+
# @raise [APIError] If the request fails
|
|
66
|
+
# @raise [ValidationError] If parameter validation fails
|
|
67
|
+
def execute(params = {})
|
|
68
|
+
validate_params!(params)
|
|
69
|
+
|
|
70
|
+
log("Making GET request to #{BASE_URL}")
|
|
71
|
+
log("Parameters: #{params.inspect}") if params.any?
|
|
72
|
+
|
|
73
|
+
response = @connection.get do |req|
|
|
74
|
+
params.each { |k, v| req.params[k.to_s] = v }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
handle_response(response)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def validate_api_key!(api_key)
|
|
83
|
+
raise ArgumentError, "API key is required. Get your API key at: https://apiverve.com" if api_key.nil? || api_key.strip.empty?
|
|
84
|
+
|
|
85
|
+
unless api_key.match?(/^[a-zA-Z0-9_-]+$/)
|
|
86
|
+
raise ArgumentError, "Invalid API key format. API key should only contain letters, numbers, hyphens, and underscores."
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def validate_params!(params)
|
|
91
|
+
return if VALIDATION_RULES.empty?
|
|
92
|
+
|
|
93
|
+
errors = []
|
|
94
|
+
|
|
95
|
+
VALIDATION_RULES.each do |param_name, rules|
|
|
96
|
+
value = params[param_name.to_sym] || params[param_name]
|
|
97
|
+
|
|
98
|
+
# Check required
|
|
99
|
+
if rules[:required] && (value.nil? || value.to_s.empty?)
|
|
100
|
+
errors << "Required parameter [#{param_name}] is missing."
|
|
101
|
+
next
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
next if value.nil?
|
|
105
|
+
|
|
106
|
+
case rules[:type]
|
|
107
|
+
when "integer", "number"
|
|
108
|
+
begin
|
|
109
|
+
num_value = rules[:type] == "number" ? Float(value) : Integer(value)
|
|
110
|
+
errors << "Parameter [#{param_name}] must be at least #{rules[:min]}." if rules[:min] && num_value < rules[:min]
|
|
111
|
+
errors << "Parameter [#{param_name}] must be at most #{rules[:max]}." if rules[:max] && num_value > rules[:max]
|
|
112
|
+
rescue ArgumentError, TypeError
|
|
113
|
+
errors << "Parameter [#{param_name}] must be a valid #{rules[:type]}."
|
|
114
|
+
end
|
|
115
|
+
when "string"
|
|
116
|
+
unless value.is_a?(String)
|
|
117
|
+
errors << "Parameter [#{param_name}] must be a string."
|
|
118
|
+
next
|
|
119
|
+
end
|
|
120
|
+
errors << "Parameter [#{param_name}] must be at least #{rules[:min_length]} characters." if rules[:min_length] && value.length < rules[:min_length]
|
|
121
|
+
errors << "Parameter [#{param_name}] must be at most #{rules[:max_length]} characters." if rules[:max_length] && value.length > rules[:max_length]
|
|
122
|
+
|
|
123
|
+
if rules[:format] && FORMAT_PATTERNS[rules[:format]]
|
|
124
|
+
unless value.match?(FORMAT_PATTERNS[rules[:format]])
|
|
125
|
+
errors << "Parameter [#{param_name}] must be a valid #{rules[:format]}."
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
when "boolean"
|
|
129
|
+
unless [true, false, "true", "false"].include?(value)
|
|
130
|
+
errors << "Parameter [#{param_name}] must be a boolean."
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Enum validation
|
|
135
|
+
if rules[:enum] && !rules[:enum].include?(value)
|
|
136
|
+
errors << "Parameter [#{param_name}] must be one of: #{rules[:enum].join(', ')}."
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
raise ValidationError, errors unless errors.empty?
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def handle_response(response)
|
|
144
|
+
log("Response status: #{response.status}")
|
|
145
|
+
|
|
146
|
+
data = JSON.parse(response.body)
|
|
147
|
+
|
|
148
|
+
if data["status"] == "error"
|
|
149
|
+
raise APIError.new(data["error"] || "Unknown API error", response.status, data)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
unless response.success?
|
|
153
|
+
raise APIError.new(data["error"] || "HTTP #{response.status} error", response.status, data)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
log("Request successful")
|
|
157
|
+
data
|
|
158
|
+
rescue JSON::ParserError => e
|
|
159
|
+
raise APIError.new("Invalid JSON response: #{e.message}", response.status)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def log(message)
|
|
163
|
+
puts "[APIVerve::Exercises] #{message}" if @debug
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Custom error class for API errors
|
|
168
|
+
class APIError < StandardError
|
|
169
|
+
attr_reader :status_code, :response
|
|
170
|
+
|
|
171
|
+
def initialize(message, status_code = nil, response = nil)
|
|
172
|
+
@status_code = status_code
|
|
173
|
+
@response = response
|
|
174
|
+
super(message)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Custom error class for validation errors
|
|
179
|
+
class ValidationError < StandardError
|
|
180
|
+
attr_reader :errors
|
|
181
|
+
|
|
182
|
+
def initialize(errors)
|
|
183
|
+
@errors = errors
|
|
184
|
+
super("Validation failed: #{errors.join(' ')}")
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: apiverve_exercises
|
|
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: Exercises is a simple tool for getting exercise information. It returns
|
|
61
|
+
information on various exercises.
|
|
62
|
+
email:
|
|
63
|
+
- hello@apiverve.com
|
|
64
|
+
executables: []
|
|
65
|
+
extensions: []
|
|
66
|
+
extra_rdoc_files: []
|
|
67
|
+
files:
|
|
68
|
+
- LICENSE
|
|
69
|
+
- README.md
|
|
70
|
+
- lib/apiverve/client.rb
|
|
71
|
+
- lib/apiverve_exercises.rb
|
|
72
|
+
- lib/apiverve_exercises/client.rb
|
|
73
|
+
homepage: https://apiverve.com/marketplace/exercises?utm_source=ruby&utm_medium=homepage
|
|
74
|
+
licenses:
|
|
75
|
+
- MIT
|
|
76
|
+
metadata:
|
|
77
|
+
homepage_uri: https://apiverve.com/marketplace/exercises?utm_source=ruby&utm_medium=homepage
|
|
78
|
+
source_code_uri: https://github.com/apiverve/exercises-API/tree/main/ruby
|
|
79
|
+
changelog_uri: https://apiverve.com/changelog
|
|
80
|
+
documentation_uri: https://docs.apiverve.com/ref/exercises
|
|
81
|
+
rdoc_options: []
|
|
82
|
+
require_paths:
|
|
83
|
+
- lib
|
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: 2.7.0
|
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
|
+
requirements:
|
|
91
|
+
- - ">="
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: '0'
|
|
94
|
+
requirements: []
|
|
95
|
+
rubygems_version: 3.6.9
|
|
96
|
+
specification_version: 4
|
|
97
|
+
summary: Exercises API - Ruby Client
|
|
98
|
+
test_files: []
|