crawlora-speedrun 0.1.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/CHANGELOG.md +5 -0
- data/LICENSE +21 -0
- data/README.md +60 -0
- data/lib/crawlora/speedrun/client.rb +276 -0
- data/lib/crawlora/speedrun/version.rb +5 -0
- data/lib/crawlora/speedrun.rb +2 -0
- metadata +52 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 90d14362f0cfa95b63af16922cf09ce48a24414a53cb9df894eae289dc489466
|
|
4
|
+
data.tar.gz: edf508494a230068d44fc2fd5e8ecc25aab34c21ed70ae2bf0c00d60ec9b72a4
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 852e89b09d6c604aa9c5890b8eb17a2dcbfa115ce52eb6db7ca3258b5554d11ea0c0f9943b225e6e1cbe0bae52f246f0d1fc03bfa9b0f8077be81cd18ebe6f08
|
|
7
|
+
data.tar.gz: 934cca8fa5fb611cd4cabaa2e101e704a945a45a83bde5e7ccd01059a5748aef0ab2dd5793e6105206cbf9397f0d6059f96385885e770da47d3522e622749761
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Crawlora
|
|
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,60 @@
|
|
|
1
|
+
# Crawlora Speedrun.com API client for Ruby (`crawlora-speedrun`)
|
|
2
|
+
|
|
3
|
+
This Ruby gem calls Crawlora's hosted Speedrun.com API at `https://api.crawlora.net/api/v1`. It exposes 20 endpoints covering game and category discovery, leaderboards, records, variables, and run metadata; it does not call or scrape Speedrun.com directly. Requests require your Crawlora API key and use your account's service plan.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem "crawlora-speedrun"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Create an account at [crawlora.net](https://crawlora.net/signup?utm_source=rubygems&utm_medium=referral&utm_campaign=platform-clients&utm_content=speedrun-ruby-signup), open the [Crawlora console](https://crawlora.net/app?utm_source=rubygems&utm_medium=referral&utm_campaign=platform-clients&utm_content=speedrun-ruby-console) to get an API key, and set `CRAWLORA_API_KEY` before running the client:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
require "json"
|
|
15
|
+
require "crawlora/speedrun"
|
|
16
|
+
|
|
17
|
+
client = Crawlora::Speedrun::Client.new
|
|
18
|
+
result = client.request("speedrun-catalog", JSON.parse("{\"kind\": \"platforms\"}"))
|
|
19
|
+
puts result
|
|
20
|
+
client.close
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Use an operation-specific method for normal calls. `request(operation_id, params = {}, response_type: :auto)` is available for every operation. `response_type: :text` returns raw response text. This gem includes 20 API operations.
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
client = Crawlora::Speedrun::Client.new(api_key: ENV.fetch("CRAWLORA_API_KEY"), timeout: 30)
|
|
27
|
+
# client.<operation_method>(<endpoint parameters>)
|
|
28
|
+
client.close
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Client options include `api_key`, `base_url`, and `timeout`. Ruby stdlib provides the HTTP and JSON support.
|
|
32
|
+
|
|
33
|
+
## Supported Speedrun.com API endpoints
|
|
34
|
+
|
|
35
|
+
Browse every route included in this gem. The [endpoint reference](https://github.com/Crawlora-org/crawlora-speedrun/blob/main/docs/usage.md) includes parameter and response details.
|
|
36
|
+
|
|
37
|
+
| Method | Endpoint | API operation | Description |
|
|
38
|
+
| --- | --- | --- | --- |
|
|
39
|
+
| `GET` | `/speedrun/catalogs/{kind}` | `speedrun-catalog` | Discover Speedrun.com filter catalogs |
|
|
40
|
+
| `GET` | `/speedrun/categories/{id}` | `speedrun-category-detail` | Get Speedrun category detail |
|
|
41
|
+
| `GET` | `/speedrun/categories/{id}/records` | `speedrun-category-records` | Get Speedrun category records |
|
|
42
|
+
| `GET` | `/speedrun/categories/{id}/variables` | `speedrun-category-variables` | Get Speedrun category variables |
|
|
43
|
+
| `GET` | `/speedrun/games` | `speedrun-games` | Search Speedrun.com games |
|
|
44
|
+
| `GET` | `/speedrun/games/{id}` | `speedrun-game-detail` | Get Speedrun game detail |
|
|
45
|
+
| `GET` | `/speedrun/games/{id}/categories` | `speedrun-categories` | Get native game categories |
|
|
46
|
+
| `GET` | `/speedrun/games/{id}/levels` | `speedrun-levels` | Get native game levels |
|
|
47
|
+
| `GET` | `/speedrun/games/{id}/records` | `speedrun-records` | Get native top-one game record boards |
|
|
48
|
+
| `GET` | `/speedrun/games/{id}/variables` | `speedrun-variables` | Get native game variable definitions |
|
|
49
|
+
| `GET` | `/speedrun/leaderboards/{game}/category/{category}` | `speedrun-game-leaderboard` | Get a Speedrun full-game leaderboard |
|
|
50
|
+
| `GET` | `/speedrun/leaderboards/{game}/level/{level}/{category}` | `speedrun-level-leaderboard` | Get a Speedrun individual-level leaderboard |
|
|
51
|
+
| `GET` | `/speedrun/levels/{id}` | `speedrun-level-detail` | Get Speedrun level detail |
|
|
52
|
+
| `GET` | `/speedrun/levels/{id}/categories` | `speedrun-level-categories` | Get Speedrun level categories |
|
|
53
|
+
| `GET` | `/speedrun/levels/{id}/records` | `speedrun-level-records` | Get Speedrun level records |
|
|
54
|
+
| `GET` | `/speedrun/levels/{id}/variables` | `speedrun-level-variables` | Get Speedrun level variables |
|
|
55
|
+
| `GET` | `/speedrun/options` | `speedrun-resource-options` | Discover Speedrun contextual resource options |
|
|
56
|
+
| `GET` | `/speedrun/runs` | `speedrun-runs` | List native Speedrun run metadata |
|
|
57
|
+
| `GET` | `/speedrun/runs/options` | `speedrun-runs-options` | Discover Speedrun run query options |
|
|
58
|
+
| `GET` | `/speedrun/runs/{id}` | `speedrun-run` | Read native Speedrun run metadata |
|
|
59
|
+
|
|
60
|
+
See [Crawlora](https://crawlora.net/?utm_source=rubygems&utm_medium=referral&utm_campaign=platform-clients&utm_content=speedrun-ruby-homepage), the [API documentation](https://crawlora.net/docs?utm_source=rubygems&utm_medium=referral&utm_campaign=platform-clients&utm_content=speedrun-ruby-api-docs), and [the package repository](https://github.com/Crawlora-org/crawlora-speedrun) for account setup, the API operation reference, and release history.
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "net/http"
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
module Crawlora
|
|
6
|
+
module Speedrun
|
|
7
|
+
module Errors
|
|
8
|
+
class Error < StandardError
|
|
9
|
+
attr_reader :status, :operation_id, :body
|
|
10
|
+
|
|
11
|
+
def initialize(message, status: nil, operation_id: nil, body: nil)
|
|
12
|
+
super(message)
|
|
13
|
+
@status, @operation_id, @body = status, operation_id, body
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
class ClientError < Error; end
|
|
17
|
+
class ServerError < Error; end
|
|
18
|
+
class NetworkError < Error; end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
OPERATIONS = JSON.parse(<<~'JSON').freeze
|
|
22
|
+
{"speedrun-catalog": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-catalog", "method": "GET", "params": [{"description": "Native metadata collection", "enum": ["platforms", "regions", "gametypes", "developers", "publishers", "genres", "engines"], "in": "path", "name": "kind", "required": true, "type": "string"}, {"description": "Native page size from 1 to 200", "in": "query", "name": "max", "type": "integer"}, {"description": "Native zero-based result offset", "in": "query", "name": "offset", "type": "integer"}], "path": "/speedrun/catalogs/{kind}", "pathParams": ["kind"], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "max", "type": "integer"}, {"in": "query", "name": "offset", "type": "integer"}], "security": ["ApiKeyAuth"]}, "speedrun-categories": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-categories", "method": "GET", "params": [{"description": "Native opaque alphanumeric game ID", "in": "path", "name": "id", "required": true, "type": "string"}, {"description": "Source miscellaneous selector; explicit false preserved", "in": "query", "name": "miscellaneous", "type": "boolean"}, {"description": "Native category order", "enum": ["name", "miscellaneous", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"description": "Native order direction", "enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"collectionFormat": "csv", "description": "Metadata embeds", "in": "query", "items": {"enum": ["game", "variables"], "type": "string"}, "name": "embed", "type": "array"}], "path": "/speedrun/games/{id}/categories", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "miscellaneous", "type": "boolean"}, {"enum": ["name", "miscellaneous", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"collectionFormat": "csv", "enum": ["game", "variables"], "in": "query", "name": "embed", "type": "array"}], "security": ["ApiKeyAuth"]}, "speedrun-category-detail": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-category-detail", "method": "GET", "params": [{"description": "Native category ID, opaque alphanumeric", "in": "path", "name": "id", "required": true, "type": "string"}, {"collectionFormat": "csv", "description": "Native metadata embeds, comma-separated", "in": "query", "items": {"enum": ["game", "variables"], "type": "string"}, "name": "embed", "type": "array"}], "path": "/speedrun/categories/{id}", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"collectionFormat": "csv", "enum": ["game", "variables"], "in": "query", "name": "embed", "type": "array"}], "security": ["ApiKeyAuth"]}, "speedrun-category-records": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-category-records", "method": "GET", "params": [{"description": "Native category ID, opaque alphanumeric", "in": "path", "name": "id", "required": true, "type": "string"}, {"collectionFormat": "csv", "description": "Native metadata embeds, comma-separated", "in": "query", "items": {"enum": ["game", "category", "level", "platforms", "regions", "variables"], "type": "string"}, "name": "embed", "type": "array"}, {"description": "Top places, not runs; API budget 1–200; native default 3", "in": "query", "name": "top", "type": "integer"}, {"description": "Omit empty boards when true; explicit false is retained", "in": "query", "name": "skip_empty", "type": "boolean"}, {"description": "Native page size 1–200", "in": "query", "name": "max", "type": "integer"}, {"description": "Native offset, nonnegative", "in": "query", "name": "offset", "type": "integer"}], "path": "/speedrun/categories/{id}/records", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"collectionFormat": "csv", "enum": ["game", "category", "level", "platforms", "regions", "variables"], "in": "query", "name": "embed", "type": "array"}, {"in": "query", "name": "top", "type": "integer"}, {"in": "query", "name": "skip_empty", "type": "boolean"}, {"in": "query", "name": "max", "type": "integer"}, {"in": "query", "name": "offset", "type": "integer"}], "security": ["ApiKeyAuth"]}, "speedrun-category-variables": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-category-variables", "method": "GET", "params": [{"description": "Native category ID, opaque alphanumeric", "in": "path", "name": "id", "required": true, "type": "string"}, {"collectionFormat": "csv", "description": "Native metadata embeds, comma-separated", "in": "query", "items": {"enum": ["game"], "type": "string"}, "name": "embed", "type": "array"}, {"description": "Native contextual order", "enum": ["name", "mandatory", "user-defined", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"description": "Native order direction", "enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}], "path": "/speedrun/categories/{id}/variables", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"collectionFormat": "csv", "enum": ["game"], "in": "query", "name": "embed", "type": "array"}, {"enum": ["name", "mandatory", "user-defined", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}], "security": ["ApiKeyAuth"]}, "speedrun-game-detail": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-game-detail", "method": "GET", "params": [{"description": "Native game ID, opaque alphanumeric", "in": "path", "name": "id", "required": true, "type": "string"}, {"collectionFormat": "csv", "description": "Native metadata embeds, comma-separated", "in": "query", "items": {"enum": ["categories", "levels", "variables", "platforms", "regions", "gametypes", "genres", "engines", "developers", "publishers"], "type": "string"}, "name": "embed", "type": "array"}], "path": "/speedrun/games/{id}", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"collectionFormat": "csv", "enum": ["categories", "levels", "variables", "platforms", "regions", "gametypes", "genres", "engines", "developers", "publishers"], "in": "query", "name": "embed", "type": "array"}], "security": ["ApiKeyAuth"]}, "speedrun-game-leaderboard": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-game-leaderboard", "method": "GET", "params": [{"description": "Native game ID", "in": "path", "name": "game", "required": true, "type": "string"}, {"description": "Native category ID appropriate to this board", "in": "path", "name": "category", "required": true, "type": "string"}, {"collectionFormat": "csv", "description": "Native metadata embeds", "in": "query", "items": {"enum": ["game", "category", "level", "platforms", "regions", "variables"], "type": "string"}, "name": "embed", "type": "array"}, {"default": 1, "description": "Top places, 1–200; API response-budget limit, ties can return more runs", "in": "query", "name": "top", "type": "integer"}, {"description": "Native platform ID from catalogs", "in": "query", "name": "platform", "type": "string"}, {"description": "Native region ID from catalogs", "in": "query", "name": "region", "type": "string"}, {"description": "Omit for both; true for emulators, false for real devices", "in": "query", "name": "emulators", "type": "boolean"}, {"description": "Require a video when true; omission uses source default", "in": "query", "name": "video_only", "type": "boolean"}, {"description": "Native timing method; omission uses source default", "enum": ["realtime", "realtime_noloads", "ingame"], "in": "query", "name": "timing", "type": "string"}, {"description": "Run date cutoff YYYY-MM-DD", "in": "query", "name": "date", "type": "string"}, {"description": "JSON object mapping native variable IDs to value IDs", "in": "query", "name": "variables", "type": "string"}], "path": "/speedrun/leaderboards/{game}/category/{category}", "pathParams": ["game", "category"], "produces": ["application/json"], "queryParams": [{"collectionFormat": "csv", "enum": ["game", "category", "level", "platforms", "regions", "variables"], "in": "query", "name": "embed", "type": "array"}, {"in": "query", "name": "top", "type": "integer"}, {"in": "query", "name": "platform", "type": "string"}, {"in": "query", "name": "region", "type": "string"}, {"in": "query", "name": "emulators", "type": "boolean"}, {"in": "query", "name": "video_only", "type": "boolean"}, {"enum": ["realtime", "realtime_noloads", "ingame"], "in": "query", "name": "timing", "type": "string"}, {"in": "query", "name": "date", "type": "string"}, {"in": "query", "name": "variables", "type": "string"}], "security": ["ApiKeyAuth"]}, "speedrun-games": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-games", "method": "GET", "params": [{"description": "Native game name search", "in": "query", "name": "name", "type": "string"}, {"description": "Platform ID from the platforms catalog", "in": "query", "name": "platform", "type": "string"}, {"description": "Region ID from the regions catalog", "in": "query", "name": "region", "type": "string"}, {"description": "Game-type ID from the gametypes catalog", "in": "query", "name": "gametype", "type": "string"}, {"description": "Native page size1–200, or1–1000 with _bulk=true", "in": "query", "name": "max", "type": "integer"}, {"description": "Native zero-based result offset", "in": "query", "name": "offset", "type": "integer"}, {"description": "Exact native abbreviation", "in": "query", "name": "abbreviation", "type": "string"}, {"description": "Native release year, including0 for unknown", "in": "query", "name": "released", "type": "integer"}, {"description": "Native genre ID from catalog", "in": "query", "name": "genre", "type": "string"}, {"description": "Native engine ID from catalog", "in": "query", "name": "engine", "type": "string"}, {"description": "Native developer ID from catalog", "in": "query", "name": "developer", "type": "string"}, {"description": "Native publisher ID from catalog", "in": "query", "name": "publisher", "type": "string"}, {"description": "Native game order; similarity requires name", "enum": ["name.int", "name.jap", "abbreviation", "released", "created", "similarity"], "in": "query", "name": "orderby", "type": "string"}, {"description": "Native order direction", "enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"description": "Native compact page mode, max up to1000; disables embeds", "in": "query", "name": "_bulk", "type": "boolean"}, {"description": "Legacy inclusion: true only typed games, false only untyped; omission includes both", "in": "query", "name": "romhack", "type": "boolean"}, {"collectionFormat": "csv", "description": "Native metadata embeds", "in": "query", "items": {"enum": ["categories", "levels", "variables", "platforms", "regions", "gametypes", "genres", "engines", "developers", "publishers"], "type": "string"}, "name": "embed", "type": "array"}], "path": "/speedrun/games", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "name", "type": "string"}, {"in": "query", "name": "platform", "type": "string"}, {"in": "query", "name": "region", "type": "string"}, {"in": "query", "name": "gametype", "type": "string"}, {"in": "query", "name": "max", "type": "integer"}, {"in": "query", "name": "offset", "type": "integer"}, {"in": "query", "name": "abbreviation", "type": "string"}, {"in": "query", "name": "released", "type": "integer"}, {"in": "query", "name": "genre", "type": "string"}, {"in": "query", "name": "engine", "type": "string"}, {"in": "query", "name": "developer", "type": "string"}, {"in": "query", "name": "publisher", "type": "string"}, {"enum": ["name.int", "name.jap", "abbreviation", "released", "created", "similarity"], "in": "query", "name": "orderby", "type": "string"}, {"enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"in": "query", "name": "_bulk", "type": "boolean"}, {"in": "query", "name": "romhack", "type": "boolean"}, {"collectionFormat": "csv", "enum": ["categories", "levels", "variables", "platforms", "regions", "gametypes", "genres", "engines", "developers", "publishers"], "in": "query", "name": "embed", "type": "array"}], "security": ["ApiKeyAuth"]}, "speedrun-level-categories": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-level-categories", "method": "GET", "params": [{"description": "Native level ID, opaque alphanumeric", "in": "path", "name": "id", "required": true, "type": "string"}, {"collectionFormat": "csv", "description": "Native metadata embeds, comma-separated", "in": "query", "items": {"enum": ["game", "variables"], "type": "string"}, "name": "embed", "type": "array"}, {"description": "Native contextual order", "enum": ["name", "miscellaneous", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"description": "Native order direction", "enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"description": "Select source miscellaneous flag, preserving explicit false", "in": "query", "name": "miscellaneous", "type": "boolean"}], "path": "/speedrun/levels/{id}/categories", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"collectionFormat": "csv", "enum": ["game", "variables"], "in": "query", "name": "embed", "type": "array"}, {"enum": ["name", "miscellaneous", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"in": "query", "name": "miscellaneous", "type": "boolean"}], "security": ["ApiKeyAuth"]}, "speedrun-level-detail": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-level-detail", "method": "GET", "params": [{"description": "Native level ID, opaque alphanumeric", "in": "path", "name": "id", "required": true, "type": "string"}, {"collectionFormat": "csv", "description": "Native metadata embeds, comma-separated", "in": "query", "items": {"enum": ["categories", "variables"], "type": "string"}, "name": "embed", "type": "array"}], "path": "/speedrun/levels/{id}", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"collectionFormat": "csv", "enum": ["categories", "variables"], "in": "query", "name": "embed", "type": "array"}], "security": ["ApiKeyAuth"]}, "speedrun-level-leaderboard": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-level-leaderboard", "method": "GET", "params": [{"description": "Native game ID", "in": "path", "name": "game", "required": true, "type": "string"}, {"description": "Native category ID appropriate to this board", "in": "path", "name": "category", "required": true, "type": "string"}, {"description": "Native level ID from game level discovery", "in": "path", "name": "level", "required": true, "type": "string"}, {"collectionFormat": "csv", "description": "Native metadata embeds", "in": "query", "items": {"enum": ["game", "category", "level", "platforms", "regions", "variables"], "type": "string"}, "name": "embed", "type": "array"}, {"default": 1, "description": "Top places, 1–200; API response-budget limit, ties can return more runs", "in": "query", "name": "top", "type": "integer"}, {"description": "Native platform ID from catalogs", "in": "query", "name": "platform", "type": "string"}, {"description": "Native region ID from catalogs", "in": "query", "name": "region", "type": "string"}, {"description": "Omit for both; true for emulators, false for real devices", "in": "query", "name": "emulators", "type": "boolean"}, {"description": "Require a video when true; omission uses source default", "in": "query", "name": "video_only", "type": "boolean"}, {"description": "Native timing method; omission uses source default", "enum": ["realtime", "realtime_noloads", "ingame"], "in": "query", "name": "timing", "type": "string"}, {"description": "Run date cutoff YYYY-MM-DD", "in": "query", "name": "date", "type": "string"}, {"description": "JSON object mapping native variable IDs to value IDs", "in": "query", "name": "variables", "type": "string"}], "path": "/speedrun/leaderboards/{game}/level/{level}/{category}", "pathParams": ["game", "category", "level"], "produces": ["application/json"], "queryParams": [{"collectionFormat": "csv", "enum": ["game", "category", "level", "platforms", "regions", "variables"], "in": "query", "name": "embed", "type": "array"}, {"in": "query", "name": "top", "type": "integer"}, {"in": "query", "name": "platform", "type": "string"}, {"in": "query", "name": "region", "type": "string"}, {"in": "query", "name": "emulators", "type": "boolean"}, {"in": "query", "name": "video_only", "type": "boolean"}, {"enum": ["realtime", "realtime_noloads", "ingame"], "in": "query", "name": "timing", "type": "string"}, {"in": "query", "name": "date", "type": "string"}, {"in": "query", "name": "variables", "type": "string"}], "security": ["ApiKeyAuth"]}, "speedrun-level-records": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-level-records", "method": "GET", "params": [{"description": "Native level ID, opaque alphanumeric", "in": "path", "name": "id", "required": true, "type": "string"}, {"collectionFormat": "csv", "description": "Native metadata embeds, comma-separated", "in": "query", "items": {"enum": ["game", "category", "level", "platforms", "regions", "variables"], "type": "string"}, "name": "embed", "type": "array"}, {"description": "Top places, not runs; API budget 1–200; native default 3", "in": "query", "name": "top", "type": "integer"}, {"description": "Omit empty boards when true; explicit false is retained", "in": "query", "name": "skip_empty", "type": "boolean"}, {"description": "Native page size 1–200", "in": "query", "name": "max", "type": "integer"}, {"description": "Native offset, nonnegative", "in": "query", "name": "offset", "type": "integer"}], "path": "/speedrun/levels/{id}/records", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"collectionFormat": "csv", "enum": ["game", "category", "level", "platforms", "regions", "variables"], "in": "query", "name": "embed", "type": "array"}, {"in": "query", "name": "top", "type": "integer"}, {"in": "query", "name": "skip_empty", "type": "boolean"}, {"in": "query", "name": "max", "type": "integer"}, {"in": "query", "name": "offset", "type": "integer"}], "security": ["ApiKeyAuth"]}, "speedrun-level-variables": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-level-variables", "method": "GET", "params": [{"description": "Native level ID, opaque alphanumeric", "in": "path", "name": "id", "required": true, "type": "string"}, {"collectionFormat": "csv", "description": "Native metadata embeds, comma-separated", "in": "query", "items": {"enum": ["game"], "type": "string"}, "name": "embed", "type": "array"}, {"description": "Native contextual order", "enum": ["name", "mandatory", "user-defined", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"description": "Native order direction", "enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}], "path": "/speedrun/levels/{id}/variables", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"collectionFormat": "csv", "enum": ["game"], "in": "query", "name": "embed", "type": "array"}, {"enum": ["name", "mandatory", "user-defined", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}], "security": ["ApiKeyAuth"]}, "speedrun-levels": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-levels", "method": "GET", "params": [{"description": "Native opaque alphanumeric game ID", "in": "path", "name": "id", "required": true, "type": "string"}, {"description": "Native level order", "enum": ["name", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"description": "Native order direction", "enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"collectionFormat": "csv", "description": "Metadata embeds", "in": "query", "items": {"enum": ["categories", "variables"], "type": "string"}, "name": "embed", "type": "array"}], "path": "/speedrun/games/{id}/levels", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"enum": ["name", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"collectionFormat": "csv", "enum": ["categories", "variables"], "in": "query", "name": "embed", "type": "array"}], "security": ["ApiKeyAuth"]}, "speedrun-records": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-records", "method": "GET", "params": [{"description": "Native opaque alphanumeric game ID", "in": "path", "name": "id", "required": true, "type": "string"}, {"description": "Native page size from 1 to 200", "in": "query", "name": "max", "type": "integer"}, {"description": "Native zero-based result offset", "in": "query", "name": "offset", "type": "integer"}, {"default": 1, "description": "Top places, API budget1–200; compatibility default1", "in": "query", "name": "top", "type": "integer"}, {"description": "Omit empty boards when true; explicit false preserved", "in": "query", "name": "skip_empty", "type": "boolean"}, {"collectionFormat": "csv", "description": "Metadata embeds", "in": "query", "items": {"enum": ["game", "category", "level", "platforms", "regions", "variables"], "type": "string"}, "name": "embed", "type": "array"}], "path": "/speedrun/games/{id}/records", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "max", "type": "integer"}, {"in": "query", "name": "offset", "type": "integer"}, {"in": "query", "name": "top", "type": "integer"}, {"in": "query", "name": "skip_empty", "type": "boolean"}, {"collectionFormat": "csv", "enum": ["game", "category", "level", "platforms", "regions", "variables"], "in": "query", "name": "embed", "type": "array"}], "security": ["ApiKeyAuth"]}, "speedrun-resource-options": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-resource-options", "method": "GET", "params": [], "path": "/speedrun/options", "pathParams": [], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "speedrun-run": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-run", "method": "GET", "params": [{"description": "Opaque native run ID from runs, records or leaderboards", "in": "path", "name": "id", "required": true, "type": "string"}, {"collectionFormat": "csv", "description": "Native metadata embeds, comma-separated", "in": "query", "items": {"enum": ["game", "category", "level", "platform", "region"], "type": "string"}, "name": "embed", "type": "array"}], "path": "/speedrun/runs/{id}", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"collectionFormat": "csv", "enum": ["game", "category", "level", "platform", "region"], "in": "query", "name": "embed", "type": "array"}], "security": ["ApiKeyAuth"]}, "speedrun-runs": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-runs", "method": "GET", "params": [{"description": "Native game ID from games discovery", "in": "query", "name": "game", "type": "string"}, {"description": "Native category ID from category discovery", "in": "query", "name": "category", "type": "string"}, {"description": "Native level ID from level discovery", "in": "query", "name": "level", "type": "string"}, {"description": "Native platform ID from platforms catalog", "in": "query", "name": "platform", "type": "string"}, {"description": "Native region ID from regions catalog", "in": "query", "name": "region", "type": "string"}, {"description": "Omit for both; explicit true or false selects the native device kind", "in": "query", "name": "emulated", "type": "boolean"}, {"description": "Native verification status", "enum": ["new", "verified", "rejected"], "in": "query", "name": "status", "type": "string"}, {"description": "Native ordering", "enum": ["game", "category", "level", "platform", "region", "emulated", "date", "submitted", "status", "verify-date"], "in": "query", "name": "orderby", "type": "string"}, {"description": "Native ordering direction", "enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"description": "Native page size, 1..200", "in": "query", "maximum": 200, "minimum": 1, "name": "max", "type": "integer"}, {"description": "Native zero-based offset", "in": "query", "minimum": 0, "name": "offset", "type": "integer"}, {"collectionFormat": "csv", "description": "Native metadata embeds, comma-separated", "in": "query", "items": {"enum": ["game", "category", "level", "platform", "region"], "type": "string"}, "name": "embed", "type": "array"}], "path": "/speedrun/runs", "pathParams": [], "produces": ["application/json"], "queryParams": [{"in": "query", "name": "game", "type": "string"}, {"in": "query", "name": "category", "type": "string"}, {"in": "query", "name": "level", "type": "string"}, {"in": "query", "name": "platform", "type": "string"}, {"in": "query", "name": "region", "type": "string"}, {"in": "query", "name": "emulated", "type": "boolean"}, {"enum": ["new", "verified", "rejected"], "in": "query", "name": "status", "type": "string"}, {"enum": ["game", "category", "level", "platform", "region", "emulated", "date", "submitted", "status", "verify-date"], "in": "query", "name": "orderby", "type": "string"}, {"enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"in": "query", "name": "max", "type": "integer"}, {"in": "query", "name": "offset", "type": "integer"}, {"collectionFormat": "csv", "enum": ["game", "category", "level", "platform", "region"], "in": "query", "name": "embed", "type": "array"}], "security": ["ApiKeyAuth"]}, "speedrun-runs-options": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-runs-options", "method": "GET", "params": [], "path": "/speedrun/runs/options", "pathParams": [], "produces": ["application/json"], "queryParams": [], "security": ["ApiKeyAuth"]}, "speedrun-variables": {"bodyParam": null, "bodyRequired": false, "id": "speedrun-variables", "method": "GET", "params": [{"description": "Native opaque alphanumeric game ID", "in": "path", "name": "id", "required": true, "type": "string"}, {"description": "Native variable order", "enum": ["name", "mandatory", "user-defined", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"description": "Native order direction", "enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"collectionFormat": "csv", "description": "Metadata embed", "in": "query", "items": {"enum": ["game"], "type": "string"}, "name": "embed", "type": "array"}], "path": "/speedrun/games/{id}/variables", "pathParams": ["id"], "produces": ["application/json"], "queryParams": [{"enum": ["name", "mandatory", "user-defined", "pos"], "in": "query", "name": "orderby", "type": "string"}, {"enum": ["asc", "desc"], "in": "query", "name": "direction", "type": "string"}, {"collectionFormat": "csv", "enum": ["game"], "in": "query", "name": "embed", "type": "array"}], "security": ["ApiKeyAuth"]}}
|
|
23
|
+
JSON
|
|
24
|
+
OPERATION_IDS = JSON.parse(<<~'JSON').freeze
|
|
25
|
+
["speedrun-catalog", "speedrun-categories", "speedrun-category-detail", "speedrun-category-records", "speedrun-category-variables", "speedrun-game-detail", "speedrun-game-leaderboard", "speedrun-games", "speedrun-level-categories", "speedrun-level-detail", "speedrun-level-leaderboard", "speedrun-level-records", "speedrun-level-variables", "speedrun-levels", "speedrun-records", "speedrun-resource-options", "speedrun-run", "speedrun-runs", "speedrun-runs-options", "speedrun-variables"]
|
|
26
|
+
JSON
|
|
27
|
+
OPERATION_COUNT = OPERATION_IDS.length
|
|
28
|
+
|
|
29
|
+
class Client
|
|
30
|
+
attr_reader :base_url
|
|
31
|
+
|
|
32
|
+
def initialize(api_key: ENV["CRAWLORA_API_KEY"], base_url: "https://api.crawlora.net/api/v1", timeout: 30, user_agent: "crawlora-speedrun-ruby/0.1.0", transport: nil)
|
|
33
|
+
@api_key = api_key
|
|
34
|
+
@base_url = base_url.to_s.sub(%r{/+$}, "")
|
|
35
|
+
@timeout = Float(timeout)
|
|
36
|
+
@user_agent = user_agent
|
|
37
|
+
@transport = transport
|
|
38
|
+
@closed = false
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def request(operation_id, params = {}, response_type: :auto)
|
|
42
|
+
raise Errors::ClientError, "client is closed" if @closed
|
|
43
|
+
operation_id = operation_id.to_s
|
|
44
|
+
operation = OPERATIONS[operation_id]
|
|
45
|
+
raise Errors::ClientError.new("unknown operation: #{operation_id}", operation_id: operation_id) unless operation
|
|
46
|
+
raise Errors::ClientError.new("Crawlora API key is required", operation_id: operation_id) if @api_key.nil? || @api_key.to_s.empty?
|
|
47
|
+
normalized = params.each_with_object({}) { |(key, value), out| out[key.to_s] = value }
|
|
48
|
+
payload = request_body(operation, normalized)
|
|
49
|
+
url = build_url(operation, normalized)
|
|
50
|
+
uri = URI.parse(url)
|
|
51
|
+
request = operation["method"] == "POST" ? Net::HTTP::Post.new(uri) : Net::HTTP::Get.new(uri)
|
|
52
|
+
unless payload.nil?
|
|
53
|
+
request.body = payload
|
|
54
|
+
request["Content-Type"] = "application/json"
|
|
55
|
+
end
|
|
56
|
+
request["x-api-key"] = @api_key
|
|
57
|
+
request["User-Agent"] = @user_agent
|
|
58
|
+
request["Accept"] = operation["produces"].include?("text/plain") ? "application/json, text/plain" : "application/json"
|
|
59
|
+
begin
|
|
60
|
+
if @transport
|
|
61
|
+
response = if operation["method"] == "POST"
|
|
62
|
+
@transport.call(url, request.to_hash, @timeout, "POST", payload)
|
|
63
|
+
else
|
|
64
|
+
@transport.call(url, request.to_hash, @timeout)
|
|
65
|
+
end
|
|
66
|
+
status = Integer(response.fetch(:status) { response.fetch("status") })
|
|
67
|
+
body = response.fetch(:body) { response.fetch("body", "") }
|
|
68
|
+
headers = response.fetch(:headers) { response.fetch("headers", {}) }
|
|
69
|
+
content_type = headers["content-type"] || headers["Content-Type"]
|
|
70
|
+
else
|
|
71
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
72
|
+
http.use_ssl = uri.scheme == "https"
|
|
73
|
+
http.open_timeout = @timeout
|
|
74
|
+
http.read_timeout = @timeout
|
|
75
|
+
response = http.start { |connection| connection.request(request) }
|
|
76
|
+
status = response.code.to_i
|
|
77
|
+
body = response.body
|
|
78
|
+
content_type = response["content-type"]
|
|
79
|
+
end
|
|
80
|
+
rescue Timeout::Error, SocketError, SystemCallError, IOError, EOFError, Net::HTTPBadResponse, Net::ProtocolError, OpenSSL::SSL::SSLError => error
|
|
81
|
+
raise Errors::NetworkError.new("Crawlora request failed: #{error.message}", operation_id: operation_id)
|
|
82
|
+
end
|
|
83
|
+
unless status >= 200 && status < 300
|
|
84
|
+
klass = status >= 500 ? Errors::ServerError : Errors::ClientError
|
|
85
|
+
raise klass.new("Crawlora returned HTTP #{status}", status: status, operation_id: operation_id, body: body)
|
|
86
|
+
end
|
|
87
|
+
parse_response(body, content_type, operation, normalized, response_type)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def close
|
|
91
|
+
@closed = true
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def closed?
|
|
95
|
+
@closed
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def with
|
|
99
|
+
return self unless block_given?
|
|
100
|
+
yield self
|
|
101
|
+
ensure
|
|
102
|
+
close if block_given?
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def self.operation_count
|
|
106
|
+
OPERATION_COUNT
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def self.operation_ids
|
|
110
|
+
OPERATION_IDS
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def self.operations
|
|
114
|
+
OPERATIONS
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
define_method('catalog') do |**params|
|
|
118
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
119
|
+
request('speedrun-catalog', params, response_type: response_type)
|
|
120
|
+
end
|
|
121
|
+
define_method('category_detail') do |**params|
|
|
122
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
123
|
+
request('speedrun-category-detail', params, response_type: response_type)
|
|
124
|
+
end
|
|
125
|
+
define_method('category_records') do |**params|
|
|
126
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
127
|
+
request('speedrun-category-records', params, response_type: response_type)
|
|
128
|
+
end
|
|
129
|
+
define_method('category_variables') do |**params|
|
|
130
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
131
|
+
request('speedrun-category-variables', params, response_type: response_type)
|
|
132
|
+
end
|
|
133
|
+
define_method('games') do |**params|
|
|
134
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
135
|
+
request('speedrun-games', params, response_type: response_type)
|
|
136
|
+
end
|
|
137
|
+
define_method('game_detail') do |**params|
|
|
138
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
139
|
+
request('speedrun-game-detail', params, response_type: response_type)
|
|
140
|
+
end
|
|
141
|
+
define_method('categories') do |**params|
|
|
142
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
143
|
+
request('speedrun-categories', params, response_type: response_type)
|
|
144
|
+
end
|
|
145
|
+
define_method('levels') do |**params|
|
|
146
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
147
|
+
request('speedrun-levels', params, response_type: response_type)
|
|
148
|
+
end
|
|
149
|
+
define_method('records') do |**params|
|
|
150
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
151
|
+
request('speedrun-records', params, response_type: response_type)
|
|
152
|
+
end
|
|
153
|
+
define_method('variables') do |**params|
|
|
154
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
155
|
+
request('speedrun-variables', params, response_type: response_type)
|
|
156
|
+
end
|
|
157
|
+
define_method('game_leaderboard') do |**params|
|
|
158
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
159
|
+
request('speedrun-game-leaderboard', params, response_type: response_type)
|
|
160
|
+
end
|
|
161
|
+
define_method('level_leaderboard') do |**params|
|
|
162
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
163
|
+
request('speedrun-level-leaderboard', params, response_type: response_type)
|
|
164
|
+
end
|
|
165
|
+
define_method('level_detail') do |**params|
|
|
166
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
167
|
+
request('speedrun-level-detail', params, response_type: response_type)
|
|
168
|
+
end
|
|
169
|
+
define_method('level_categories') do |**params|
|
|
170
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
171
|
+
request('speedrun-level-categories', params, response_type: response_type)
|
|
172
|
+
end
|
|
173
|
+
define_method('level_records') do |**params|
|
|
174
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
175
|
+
request('speedrun-level-records', params, response_type: response_type)
|
|
176
|
+
end
|
|
177
|
+
define_method('level_variables') do |**params|
|
|
178
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
179
|
+
request('speedrun-level-variables', params, response_type: response_type)
|
|
180
|
+
end
|
|
181
|
+
define_method('resource_options') do |**params|
|
|
182
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
183
|
+
request('speedrun-resource-options', params, response_type: response_type)
|
|
184
|
+
end
|
|
185
|
+
define_method('runs') do |**params|
|
|
186
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
187
|
+
request('speedrun-runs', params, response_type: response_type)
|
|
188
|
+
end
|
|
189
|
+
define_method('runs_options') do |**params|
|
|
190
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
191
|
+
request('speedrun-runs-options', params, response_type: response_type)
|
|
192
|
+
end
|
|
193
|
+
define_method('run') do |**params|
|
|
194
|
+
response_type = params.delete(:response_type) || params.delete(:_response_type) || :auto
|
|
195
|
+
request('speedrun-run', params, response_type: response_type)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
private
|
|
199
|
+
|
|
200
|
+
# POST operations take their JSON body under the body parameter name or "body".
|
|
201
|
+
def request_body(operation, params)
|
|
202
|
+
name = operation["bodyParam"]
|
|
203
|
+
return nil if name.nil?
|
|
204
|
+
value = params.key?(name) ? params[name] : params["body"]
|
|
205
|
+
if value.nil?
|
|
206
|
+
raise Errors::ClientError.new("missing request body: #{name}", operation_id: operation["id"]) if operation["bodyRequired"]
|
|
207
|
+
return nil
|
|
208
|
+
end
|
|
209
|
+
value.is_a?(String) ? value : JSON.generate(value)
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def build_url(operation, params)
|
|
213
|
+
known = operation["params"].map { |param| param["name"] }
|
|
214
|
+
known << "body" if operation["bodyParam"]
|
|
215
|
+
unknown = params.keys - known
|
|
216
|
+
raise Errors::ClientError.new("unknown parameters: #{unknown.join(', ')}", operation_id: operation["id"]) unless unknown.empty?
|
|
217
|
+
path = operation["path"].dup
|
|
218
|
+
operation["params"].select { |param| param["in"] == "path" }.each do |param|
|
|
219
|
+
value = params[param["name"]]
|
|
220
|
+
raise Errors::ClientError.new("missing path parameter: #{param['name']}", operation_id: operation["id"]) if value.nil?
|
|
221
|
+
path.sub!("{" + param["name"] + "}", percent_encode(value.to_s))
|
|
222
|
+
end
|
|
223
|
+
pairs = []
|
|
224
|
+
operation["queryParams"].each do |param|
|
|
225
|
+
name = param["name"]
|
|
226
|
+
value = params.key?(name) ? params[name] : param["default"]
|
|
227
|
+
if value.nil?
|
|
228
|
+
raise Errors::ClientError.new("missing query parameter: #{name}", operation_id: operation["id"]) if param["required"]
|
|
229
|
+
next
|
|
230
|
+
end
|
|
231
|
+
enum_values = param["enum"] || (param["items"] && param["items"]["enum"])
|
|
232
|
+
if enum_values && !(value.is_a?(Array) ? value : [value]).all? { |item| enum_values.map(&:to_s).include?(item.to_s) }
|
|
233
|
+
raise Errors::ClientError.new("invalid value for #{name}", operation_id: operation["id"])
|
|
234
|
+
end
|
|
235
|
+
if value.is_a?(Array)
|
|
236
|
+
format = param["collectionFormat"] || "csv"
|
|
237
|
+
if format == "multi"
|
|
238
|
+
value.each { |item| pairs << [name, scalar(item)] }
|
|
239
|
+
else
|
|
240
|
+
separator = {"csv" => ",", "ssv" => " ", "tsv" => "\t", "pipes" => "|"}[format] || ","
|
|
241
|
+
pairs << [name, value.map { |item| scalar(item) }.join(separator)]
|
|
242
|
+
end
|
|
243
|
+
else
|
|
244
|
+
pairs << [name, scalar(value)]
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
query = pairs.map { |name, value| "#{percent_encode(name)}=#{percent_encode(value)}" }.join("&")
|
|
248
|
+
@base_url + path + (query.empty? ? "" : "?" + query)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def scalar(value)
|
|
252
|
+
value == true ? "true" : (value == false ? "false" : value.to_s)
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def percent_encode(value)
|
|
256
|
+
URI::DEFAULT_PARSER.escape(value.to_s, /[^A-Za-z0-9\-._~]/)
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def parse_response(body, content_type, operation, params, response_type)
|
|
260
|
+
type = response_type.to_s
|
|
261
|
+
raise Errors::ClientError.new("response_type must be auto, json, or text", operation_id: operation["id"]) unless %w[auto json text].include?(type)
|
|
262
|
+
format = operation["params"].find { |param| param["name"] == "format" }
|
|
263
|
+
text_formats = format && format["enum"] ? format["enum"].reject { |value| %w[json application/json].include?(value.to_s.downcase) } : []
|
|
264
|
+
raw_format = params["format"] && text_formats.include?(params["format"].to_s)
|
|
265
|
+
json_format = format && format["enum"] && format["enum"].any? { |value| %w[json application/json].include?(value.to_s.downcase) } && %w[json application/json].include?(params["format"].to_s.downcase)
|
|
266
|
+
is_json = json_format || content_type.to_s.downcase.include?("json") || operation["produces"] == ["application/json"]
|
|
267
|
+
return body if type == "text" || raw_format || (type == "auto" && !is_json)
|
|
268
|
+
JSON.parse(body)
|
|
269
|
+
rescue JSON::ParserError => error
|
|
270
|
+
raise Errors::Error.new("invalid JSON response from Crawlora: #{error.message}", operation_id: operation["id"], body: body)
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
public
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: crawlora-speedrun
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Crawlora
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-13 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Ruby client for Crawlora's hosted Speedrun.com API. Covers game and category
|
|
14
|
+
discovery, leaderboards, records, variables, and run metadata across 20 endpoints.
|
|
15
|
+
email:
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- CHANGELOG.md
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- lib/crawlora/speedrun.rb
|
|
24
|
+
- lib/crawlora/speedrun/client.rb
|
|
25
|
+
- lib/crawlora/speedrun/version.rb
|
|
26
|
+
homepage: https://crawlora.net/?utm_source=rubygems&utm_medium=referral&utm_campaign=platform-clients&utm_content=speedrun-ruby-homepage
|
|
27
|
+
licenses:
|
|
28
|
+
- MIT
|
|
29
|
+
metadata:
|
|
30
|
+
source_code_uri: https://github.com/Crawlora-org/crawlora-speedrun
|
|
31
|
+
documentation_uri: https://github.com/Crawlora-org/crawlora-speedrun/blob/main/ruby/README.md
|
|
32
|
+
rubygems_mfa_required: 'true'
|
|
33
|
+
post_install_message:
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '2.6'
|
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
requirements: []
|
|
48
|
+
rubygems_version: 3.4.19
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: Speedrun.com API client for Crawlora (20 endpoints)
|
|
52
|
+
test_files: []
|