sink-cool 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 +26 -0
- data/LICENSE.txt +21 -0
- data/README.md +128 -0
- data/lib/sink/client.rb +115 -0
- data/lib/sink/error.rb +13 -0
- data/lib/sink/version.rb +5 -0
- data/lib/sink-cool.rb +3 -0
- data/lib/sink.rb +39 -0
- metadata +79 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a00d2c68180f646548d25323730f7e93b9ba05304714a2a00964cc7d3891b747
|
|
4
|
+
data.tar.gz: 3f5976058e5efe20ffbdc8b5abbc189c5419a56881584b17bb51cf8fa37d5e52
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f8265250d9c8a378c24e2ea0da1fd811caf2d09f81dbf08950741c0b3dbf297013dd25fbd701607588c4262fa1fea39161a92e72b91d48dbd10b314b7d8047a5
|
|
7
|
+
data.tar.gz: 2af5d5bf19381bbd9ef3708f2f6621f9c076c7d4cb53da6255466d36f176e027e5d2655204116d487f58d0792b155a6b5f50e459b7a4cb09be3952159596280c
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## Unreleased
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- GitHub Actions workflows for CI and trusted publishing to RubyGems.org.
|
|
10
|
+
- MIT License.
|
|
11
|
+
- RubyGems project metadata.
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
|
|
15
|
+
- Require Ruby 3.3 or newer, matching the tested versions.
|
|
16
|
+
- Publish version tags to RubyGems.org, GitHub Packages, and GitHub Releases after running the full Ruby version matrix.
|
|
17
|
+
- Make global client initialization thread-safe.
|
|
18
|
+
- Isolate HTTP transport in tests without patching `Net::HTTP` globally.
|
|
19
|
+
|
|
20
|
+
## 0.1.0 - 2026-07-23
|
|
21
|
+
|
|
22
|
+
### Added
|
|
23
|
+
|
|
24
|
+
- Global configuration for the Sink base URL, site token, and HTTP timeouts.
|
|
25
|
+
- Client methods for authentication, link management, search, tags, and link checks.
|
|
26
|
+
- Structured `Sink::Error` exceptions for unsuccessful API responses.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sink-cool contributors
|
|
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,128 @@
|
|
|
1
|
+
# sink-cool
|
|
2
|
+
|
|
3
|
+
Ruby client for the [Sink](https://sink.cool/) URL shortener API.
|
|
4
|
+
|
|
5
|
+
Requires Ruby 3.3 or newer.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add the gem to your bundle:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bundle add sink-cool
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Or install it directly:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
gem install sink-cool
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Configuration
|
|
22
|
+
|
|
23
|
+
Create an initializer such as `config/initializers/sink.rb`:
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
require "sink-cool"
|
|
27
|
+
|
|
28
|
+
Sink.configure do |config|
|
|
29
|
+
config.base_url = ENV.fetch("SINK_BASE_URL")
|
|
30
|
+
config.token = ENV.fetch("SINK_TOKEN")
|
|
31
|
+
end
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Use HTTPS in production so the bearer token is encrypted in transit.
|
|
35
|
+
|
|
36
|
+
Optional HTTP timeouts:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
Sink.configure do |config|
|
|
40
|
+
config.base_url = ENV.fetch("SINK_BASE_URL")
|
|
41
|
+
config.token = ENV.fetch("SINK_TOKEN")
|
|
42
|
+
config.open_timeout = 5
|
|
43
|
+
config.read_timeout = 30
|
|
44
|
+
end
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
You can also create an independent client:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
client = Sink::Client.new(
|
|
51
|
+
base_url: "https://sink.example.com",
|
|
52
|
+
token: "site-token"
|
|
53
|
+
)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
client = Sink.client
|
|
60
|
+
|
|
61
|
+
client.verify
|
|
62
|
+
|
|
63
|
+
client.create_link(
|
|
64
|
+
url: "https://example.com/articles/1",
|
|
65
|
+
slug: "article-1",
|
|
66
|
+
tags: ["articles"]
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
client.edit_link(
|
|
70
|
+
url: "https://example.com/articles/2",
|
|
71
|
+
slug: "article-1",
|
|
72
|
+
tags: ["articles"]
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
client.upsert_link(url: "https://example.com", slug: "example")
|
|
76
|
+
client.link("example")
|
|
77
|
+
client.links(limit: 20, sort: "newest", status: "active")
|
|
78
|
+
client.search_links(query: "example", tag: "articles")
|
|
79
|
+
client.tags
|
|
80
|
+
client.check_links(limit: 6, timeout: 6)
|
|
81
|
+
client.delete_link("example")
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
API responses are returned as Ruby hashes or arrays. Requests returning HTTP
|
|
85
|
+
204 return `nil`.
|
|
86
|
+
|
|
87
|
+
## Errors
|
|
88
|
+
|
|
89
|
+
Non-successful responses raise `Sink::Error`:
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
begin
|
|
93
|
+
Sink.client.link("missing")
|
|
94
|
+
rescue Sink::Error => error
|
|
95
|
+
error.status
|
|
96
|
+
error.message
|
|
97
|
+
error.body
|
|
98
|
+
end
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Development
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
bundle install
|
|
105
|
+
bundle exec rake test
|
|
106
|
+
gem build sink-cool.gemspec
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
See the [Sink API documentation](https://docs.sink.cool/api/) for endpoint and
|
|
110
|
+
payload details.
|
|
111
|
+
|
|
112
|
+
## Releasing
|
|
113
|
+
|
|
114
|
+
Configure a trusted publisher for this repository and `release.yml` on
|
|
115
|
+
RubyGems.org. Update `Sink::VERSION` and the matching `CHANGELOG.md` section,
|
|
116
|
+
then push a matching version tag:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
git tag v0.2.0
|
|
120
|
+
git push origin v0.2.0
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The release workflow tests Ruby 3.3, 3.4, and 4.0 before publishing to
|
|
124
|
+
RubyGems.org, GitHub Packages, and GitHub Releases.
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
This gem is available under the [MIT License](LICENSE.txt).
|
data/lib/sink/client.rb
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Sink
|
|
8
|
+
class Client
|
|
9
|
+
REQUEST_CLASSES = {
|
|
10
|
+
get: Net::HTTP::Get,
|
|
11
|
+
post: Net::HTTP::Post,
|
|
12
|
+
put: Net::HTTP::Put
|
|
13
|
+
}.freeze
|
|
14
|
+
|
|
15
|
+
def initialize(base_url:, token:, open_timeout: 5, read_timeout: 30)
|
|
16
|
+
@base_url = validate_base_url(base_url)
|
|
17
|
+
@token = token.to_s
|
|
18
|
+
@open_timeout = open_timeout
|
|
19
|
+
@read_timeout = read_timeout
|
|
20
|
+
raise ArgumentError, "token is required" if @token.empty?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def verify = request(:get, "/api/verify")
|
|
24
|
+
|
|
25
|
+
def create_link(attributes) = request(:post, "/api/link/create", body: attributes)
|
|
26
|
+
|
|
27
|
+
def edit_link(attributes) = request(:put, "/api/link/edit", body: attributes)
|
|
28
|
+
|
|
29
|
+
def upsert_link(attributes) = request(:post, "/api/link/upsert", body: attributes)
|
|
30
|
+
|
|
31
|
+
def delete_link(slug) = request(:post, "/api/link/delete", body: { slug: slug })
|
|
32
|
+
|
|
33
|
+
def link(slug) = request(:get, "/api/link/query", query: { slug: slug })
|
|
34
|
+
|
|
35
|
+
def links(limit: nil, cursor: nil, sort: nil, tag: nil, status: nil)
|
|
36
|
+
request(:get, "/api/link/list", query: {
|
|
37
|
+
limit: limit, cursor: cursor, sort: sort, tag: tag, status: status
|
|
38
|
+
})
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def search_links(query: nil, url: nil, limit: nil, tag: nil, status: nil)
|
|
42
|
+
request(:get, "/api/link/search", query: {
|
|
43
|
+
q: query, url: url, limit: limit, tag: tag, status: status
|
|
44
|
+
})
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def tags = request(:get, "/api/link/tags")
|
|
48
|
+
|
|
49
|
+
def check_links(cursor: nil, limit: 6, timeout: 6)
|
|
50
|
+
request(:post, "/api/link/check", body: {
|
|
51
|
+
cursor: cursor, limit: limit, timeout: timeout
|
|
52
|
+
}.compact)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def request(method, path, query: nil, body: nil)
|
|
58
|
+
uri = URI("#{@base_url}#{path}")
|
|
59
|
+
params = query&.compact
|
|
60
|
+
uri.query = URI.encode_www_form(params) if params&.any?
|
|
61
|
+
|
|
62
|
+
headers = {
|
|
63
|
+
"Authorization" => "Bearer #{@token}",
|
|
64
|
+
"Accept" => "application/json",
|
|
65
|
+
"User-Agent" => "sink-cool/#{Sink::VERSION}"
|
|
66
|
+
}
|
|
67
|
+
headers["Content-Type"] = "application/json" if body
|
|
68
|
+
|
|
69
|
+
http_request = REQUEST_CLASSES.fetch(method).new(uri, headers)
|
|
70
|
+
http_request.body = JSON.generate(body) if body
|
|
71
|
+
|
|
72
|
+
parse_response(perform_request(uri, http_request))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def perform_request(uri, request)
|
|
76
|
+
Net::HTTP.start(
|
|
77
|
+
uri.host,
|
|
78
|
+
uri.port,
|
|
79
|
+
use_ssl: uri.scheme == "https",
|
|
80
|
+
open_timeout: @open_timeout,
|
|
81
|
+
read_timeout: @read_timeout
|
|
82
|
+
) { |http| http.request(request) }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def parse_response(response)
|
|
86
|
+
return nil if response.code.to_i == 204
|
|
87
|
+
|
|
88
|
+
body = parse_body(response.body)
|
|
89
|
+
return body if response.is_a?(Net::HTTPSuccess)
|
|
90
|
+
|
|
91
|
+
message = if body.is_a?(Hash)
|
|
92
|
+
body["message"] || body["statusMessage"]
|
|
93
|
+
end
|
|
94
|
+
message ||= "HTTP #{response.code} #{response.message}"
|
|
95
|
+
raise Error.new(status: response.code.to_i, message: message, body: body)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def parse_body(body)
|
|
99
|
+
return nil if body.nil? || body.empty?
|
|
100
|
+
|
|
101
|
+
JSON.parse(body)
|
|
102
|
+
rescue JSON::ParserError
|
|
103
|
+
body
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def validate_base_url(base_url)
|
|
107
|
+
uri = URI(base_url.to_s)
|
|
108
|
+
raise ArgumentError, "base_url must be an HTTP(S) URL" unless %w[http https].include?(uri.scheme) && uri.host
|
|
109
|
+
|
|
110
|
+
base_url.to_s.sub(%r{/+\z}, "")
|
|
111
|
+
rescue URI::InvalidURIError
|
|
112
|
+
raise ArgumentError, "base_url must be an HTTP(S) URL"
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
data/lib/sink/error.rb
ADDED
data/lib/sink/version.rb
ADDED
data/lib/sink-cool.rb
ADDED
data/lib/sink.rb
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "sink/version"
|
|
4
|
+
require_relative "sink/error"
|
|
5
|
+
require_relative "sink/client"
|
|
6
|
+
require "thread"
|
|
7
|
+
|
|
8
|
+
module Sink
|
|
9
|
+
Configuration = Struct.new(:base_url, :token, :open_timeout, :read_timeout, keyword_init: true)
|
|
10
|
+
CLIENT_MUTEX = Mutex.new
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
def configure
|
|
14
|
+
configured = configuration.dup
|
|
15
|
+
yield configured
|
|
16
|
+
|
|
17
|
+
CLIENT_MUTEX.synchronize do
|
|
18
|
+
@configuration = configured
|
|
19
|
+
@client = nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
configured
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def configuration
|
|
26
|
+
return @configuration if @configuration
|
|
27
|
+
|
|
28
|
+
CLIENT_MUTEX.synchronize do
|
|
29
|
+
@configuration ||= Configuration.new(open_timeout: 5, read_timeout: 30)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def client
|
|
34
|
+
return @client if @client
|
|
35
|
+
|
|
36
|
+
CLIENT_MUTEX.synchronize { @client ||= Client.new(**configuration.to_h) }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sink-cool
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Sink Ruby contributors
|
|
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: minitest
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '6.0'
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '6.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rake
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '13.0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '13.0'
|
|
40
|
+
description: A lightweight Ruby client for managing links with the Sink URL shortener
|
|
41
|
+
API.
|
|
42
|
+
executables: []
|
|
43
|
+
extensions: []
|
|
44
|
+
extra_rdoc_files: []
|
|
45
|
+
files:
|
|
46
|
+
- CHANGELOG.md
|
|
47
|
+
- LICENSE.txt
|
|
48
|
+
- README.md
|
|
49
|
+
- lib/sink-cool.rb
|
|
50
|
+
- lib/sink.rb
|
|
51
|
+
- lib/sink/client.rb
|
|
52
|
+
- lib/sink/error.rb
|
|
53
|
+
- lib/sink/version.rb
|
|
54
|
+
homepage: https://docs.sink.cool/api/
|
|
55
|
+
licenses:
|
|
56
|
+
- MIT
|
|
57
|
+
metadata:
|
|
58
|
+
homepage_uri: https://docs.sink.cool/api/
|
|
59
|
+
source_code_uri: https://github.com/7a6163/sink-cool
|
|
60
|
+
changelog_uri: https://github.com/7a6163/sink-cool/blob/main/CHANGELOG.md
|
|
61
|
+
rubygems_mfa_required: 'true'
|
|
62
|
+
rdoc_options: []
|
|
63
|
+
require_paths:
|
|
64
|
+
- lib
|
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '3.3'
|
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
requirements: []
|
|
76
|
+
rubygems_version: 4.0.16
|
|
77
|
+
specification_version: 4
|
|
78
|
+
summary: Ruby client for the Sink URL shortener API
|
|
79
|
+
test_files: []
|