pocketdb 1.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/1.0.0.rb +159 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 34fabe4f4fe561e779e8923ffbb027a31fb42c3fde5cd38894357c5ea9c62169
4
+ data.tar.gz: 46b0170c348a6d80c3422739db35d9bc42ff86c688458174b540f3f26f5a01f2
5
+ SHA512:
6
+ metadata.gz: b42414ce47cbed246fa26cda6a22444ce6d3d0a02f82abacc933a3d736e711e884315fb5e256be14625fcfcd7f87ee548d4558614905475087fe4a1ab08056c9
7
+ data.tar.gz: 1e7fbb53b4b26ecbb959d1d329fac547f3dd752cfcf5f3c8634884117ddd5b11c561021c6a62f67fc9fa5369c1fa17850e6f4211fcb28fb1418fb1c263762350
data/1.0.0.rb ADDED
@@ -0,0 +1,159 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ class TelegraphDB
6
+ def initialize(token=nil)
7
+ @cache = []
8
+ @collections = []
9
+ if token.nil?
10
+ req = request("createAccount", {
11
+ "short_name" => rand
12
+ })
13
+ raise req["error"] unless req["ok"]
14
+ @token = req["result"]["access_token"]
15
+ else
16
+ req = request("getPageList", {
17
+ "access_token" => token,
18
+ "limit" => 200
19
+ })
20
+ raise req["error"] unless req["ok"]
21
+ req["result"]["pages"].each do |page|
22
+ @collections << page["title"]
23
+ @cache << {
24
+ "title" => page["title"],
25
+ "path" => page["path"],
26
+ "content" => nil
27
+ }
28
+ end
29
+ @token = token
30
+ end
31
+ end
32
+
33
+ def token
34
+ @token.clone
35
+ end
36
+
37
+ def clear_cache
38
+ @cache.each_with_index do |_, index|
39
+ @cache[index]["content"] = nil
40
+ end
41
+ end
42
+
43
+ def write(key, value)
44
+ content = [{
45
+ "tag" => "p",
46
+ "children" => [compress(value.to_json)]
47
+ }].to_json
48
+ if @collections.include?(key)
49
+ # edit a page
50
+ path = (@cache.find { |page| page["title"] == key })["path"]
51
+ req = request("editPage", {
52
+ "access_token" => @token,
53
+ "title" => key,
54
+ "path" => path,
55
+ "content" => content
56
+ })
57
+ raise req["error"] unless req["ok"]
58
+ store_cache(key, value)
59
+ else
60
+ # create new page
61
+ req = request("createPage", {
62
+ "access_token" => @token,
63
+ "title" => key,
64
+ "content" => content
65
+ })
66
+ raise req["error"] unless req["ok"]
67
+ @collections << req["result"]["title"]
68
+ @cache << {
69
+ "title" => req["result"]["title"],
70
+ "path" => req["result"]["path"],
71
+ "content" => value.transform_keys(&:to_s)
72
+ }
73
+ end
74
+ end
75
+
76
+ def read(key, placeholder=nil)
77
+ return placeholder unless @collections.include?(key)
78
+ cache = @cache.find { |page| page["title"] == key }
79
+ return cache["content"] unless cache["content"].nil?
80
+ req = request("getPage", {
81
+ "access_token" => @token,
82
+ "path" => cache["path"],
83
+ "return_content" => true
84
+ })
85
+ raise req["error"] unless req["ok"]
86
+ content = req["result"]["content"][0]["children"][0]
87
+ value = JSON.parse(decompress(content))
88
+ store_cache(key, value)
89
+ return value
90
+ end
91
+
92
+ def delete(key)
93
+ write(key, nil)
94
+ end
95
+
96
+ def has(key)
97
+ begin
98
+ value = read(key)
99
+ return !value.nil?
100
+ rescue
101
+ return false
102
+ end
103
+ end
104
+
105
+ private
106
+ def compress(t)
107
+ size = (t.size / 2).floor + 1
108
+ out = Array.new(size, 0)
109
+ out.each_with_index do |_, i|
110
+ left = t[i * 2].ord + 1
111
+ right_pos = i * 2 + 1
112
+ right = t.size == right_pos ? 0 : t[right_pos].ord + 1
113
+ p = pair(left, right)
114
+ out[i] = p.floor.chr(Encoding::UTF_8)
115
+ end
116
+ out.join
117
+ end
118
+
119
+ def decompress(t)
120
+ out = t.chars.map do |char|
121
+ p = unpair(char.ord)
122
+ left = (p[0] - 1).chr(Encoding::UTF_8)
123
+ right = p[1] == 0 ? "" : (p[1] - 1).chr(Encoding::UTF_8)
124
+ next left + right
125
+ end
126
+ out.join
127
+ end
128
+
129
+ def pair(a, b)
130
+ (0.5 * (a + b) * (a + b + 1) + b).floor
131
+ end
132
+
133
+ def unpair(n)
134
+ w = ((Math.sqrt(8 * n + 1) - 1) / 2).floor
135
+ t = (w ** 2 + w) / 2
136
+ left = w - (n - t)
137
+ right = n - t
138
+ [left.floor, right.floor]
139
+ end
140
+
141
+ def store_cache(key, content)
142
+ @cache.each_with_index do |page, index|
143
+ if page["title"] == key
144
+ @cache[index]["content"] = content.transform_keys(&:to_s)
145
+ end
146
+ end
147
+ end
148
+
149
+ def request(endpoint, payload)
150
+ uri = URI("https://api.telegra.ph/#{endpoint}")
151
+ req = Net::HTTP::Post.new(uri)
152
+ req.body = payload.to_json
153
+ req["Content-Type"] = "application/json"
154
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
155
+ http.request(req)
156
+ end
157
+ return JSON.parse(res.body)
158
+ end
159
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pocketdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - trulyursdelv
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-05-08 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: 'Pocket Database is a lightweight tool that utilizes Telegraph API as
13
+ a database. It is limitless, public, and on-the-go.
14
+
15
+ '
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - 1.0.0.rb
21
+ homepage: https://rubygemspec.org/gems/pocketdb
22
+ licenses:
23
+ - CC0-1.0
24
+ metadata:
25
+ homepage_uri: https://github.com/trulyursdelv/pocketdb
26
+ documentation_uri: https://github.com/trulyursdelv/pocketdb/blob/main/docs/README.md
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '3.0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.6.2
42
+ specification_version: 4
43
+ summary: Telegra.ph as a pocket database
44
+ test_files: []