at_protocol 0.0.4.2 → 0.0.4.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bac3005062f2494c16a3fcabc3af9f45daf3b127b81624872ab71603de1f9fbc
4
- data.tar.gz: 28088b1b416163feab91d06e42245279216673cf2ae1d077eebc78bfb79a0b1c
3
+ metadata.gz: e0dd450b9e79e59d5df52330a9e0fd00135db4e2a8eddce654780133835ca69f
4
+ data.tar.gz: bda2354c8c63d85eee0075c5fda834e1018e31e9c8b5daba221695644998e338
5
5
  SHA512:
6
- metadata.gz: eeb00d26cead39126432d2be52540da02f5d9734eebe9c5c4735dd4e4e556ae88a0886fe8455c98a72969b7728e2213ad0f957317a1d89852cdbcb687e7d44f7
7
- data.tar.gz: deb75bae3fe96f9eb2831f0e61ce48f23aa9030b5fe1714123cb0c7293e7378ac3a13cef0850f3b653e5467376b19fbf07de5a9d50e21d2a775527ce16b507ba
6
+ metadata.gz: 19eec8bd3383a55ae752ed3561bcced63c5a438fe1060dcd1a08fe46249e56d469b7ded6a026c73d82a84a78ef4fd2af35572ccd610fa854287b1babf43e3e8f
7
+ data.tar.gz: e8d0dc32c9e1a4101fb8dc30b0ca16a49955f3d096fbc75d36ce20486590cefb67df66c83268dabde43716495247706b6f8a7599a6b530a67b75d1b096c621db
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ create_makefile("at_protocol/tid")
@@ -0,0 +1,130 @@
1
+ #include <ruby.h>
2
+ #include <time.h>
3
+ #include <stdio.h>
4
+ #include <stdint.h>
5
+ #include <math.h>
6
+
7
+ static VALUE atproto_module;
8
+ static VALUE tid_class;
9
+ const char b32_chars[32] = "234567abcdefghijklmnopqrstuvwxyz";
10
+
11
+ typedef struct {
12
+ uint64_t timestamp;
13
+ uint64_t clock_identifier;
14
+ } TID;
15
+
16
+ static VALUE tid_alloc(VALUE klass) {
17
+ TID *tid = ALLOC(TID);
18
+ return Data_Wrap_Struct(klass, 0, xfree, tid);
19
+ }
20
+
21
+ static VALUE tid_initialize(int argc, VALUE *argv, VALUE self) {
22
+ VALUE time_arg = Qnil;
23
+ rb_scan_args(argc, argv, "01", &time_arg);
24
+
25
+ TID *tid;
26
+ Data_Get_Struct(self, TID, tid);
27
+
28
+ if (NIL_P(time_arg)) {
29
+ struct timeval tv;
30
+ gettimeofday(&tv, NULL);
31
+ tid->timestamp = (uint64_t)tv.tv_sec * 1000000 + (uint64_t)tv.tv_usec;
32
+ } else {
33
+ if (!rb_obj_is_kind_of(time_arg, rb_cTime)) {
34
+ rb_raise(rb_eTypeError, "Argument must be a Time object or nil");
35
+ return Qnil;
36
+ }
37
+
38
+ struct timeval tv;
39
+ tv.tv_sec = NUM2LONG(rb_funcall(time_arg, rb_intern("tv_sec"), 0));
40
+ tv.tv_usec = NUM2LONG(rb_funcall(time_arg, rb_intern("tv_usec"), 0));
41
+ tid->timestamp = (uint64_t)tv.tv_sec * 1000000 + (uint64_t)tv.tv_usec;
42
+ }
43
+
44
+ tid->clock_identifier = rand() & 0x3FF;
45
+
46
+ return self;
47
+ }
48
+
49
+ static VALUE tid_to_time(VALUE self) {
50
+ TID *tid;
51
+ Data_Get_Struct(self, TID, tid);
52
+
53
+ VALUE sec = LL2NUM(tid->timestamp / 1000000);
54
+ VALUE usec = LL2NUM(tid->timestamp % 1000000);
55
+
56
+ return rb_funcall(rb_cTime, rb_intern("at"), 2, sec, usec);
57
+ }
58
+
59
+ static VALUE tid_to_s(VALUE self) {
60
+ TID *tid;
61
+ Data_Get_Struct(self, TID, tid);
62
+
63
+ char tid_str[14];
64
+ for (int i = 0; i < 11; i++) {
65
+ int index = (int)((tid->timestamp >> (50 - i * 5)) & 0x1F);
66
+ tid_str[i] = b32_chars[index];
67
+ }
68
+ tid_str[11] = b32_chars[(int)(tid->clock_identifier >> 6) & 0x1F];
69
+ tid_str[12] = b32_chars[(int)tid->clock_identifier & 0x1F];
70
+ tid_str[13] = '\0';
71
+
72
+ return rb_str_new_cstr(tid_str);
73
+ }
74
+
75
+ static VALUE tid_from_string(VALUE klass, VALUE str) {
76
+ Check_Type(str, T_STRING);
77
+
78
+ TID *tid;
79
+ VALUE tid_obj = Data_Make_Struct(klass, TID, NULL, xfree, tid);
80
+
81
+ if (RSTRING_LEN(str) != 13) {
82
+ rb_raise(rb_eArgError, "TID string must be 13 characters long");
83
+ return Qnil;
84
+ }
85
+
86
+ char tid_str[14];
87
+ strncpy(tid_str, StringValueCStr(str), sizeof(tid_str));
88
+ tid_str[13] = '\0';
89
+
90
+ // Convert the TID string back to a TID object
91
+ uint64_t timestamp = 0;
92
+ for (int i = 0; i < 11; i++) {
93
+ char c = tid_str[i];
94
+ const char *pos = strchr(b32_chars, c);
95
+ if (pos == NULL) {
96
+ rb_raise(rb_eArgError, "Invalid character in TID string");
97
+ return Qnil;
98
+ }
99
+ int index = (int)(pos - b32_chars);
100
+ timestamp = (timestamp << 5) | (index & 0x1F);
101
+ }
102
+
103
+ uint64_t clock_identifier = 0;
104
+ for (int i = 11; i < 13; i++) {
105
+ char c = tid_str[i];
106
+ const char *pos = strchr(b32_chars, c);
107
+ if (pos == NULL) {
108
+ rb_raise(rb_eArgError, "Invalid character in TID string");
109
+ return Qnil;
110
+ }
111
+ int index = (int)(pos - b32_chars);
112
+ clock_identifier = (clock_identifier << 5) | (index & 0x1F);
113
+ }
114
+
115
+ tid->timestamp = timestamp;
116
+ tid->clock_identifier = clock_identifier;
117
+
118
+ return tid_obj;
119
+ }
120
+
121
+ void Init_tid(void) {
122
+ atproto_module = rb_define_module("ATProto");
123
+
124
+ tid_class = rb_define_class_under(atproto_module, "TID", rb_cObject);
125
+ rb_define_alloc_func(tid_class, tid_alloc);
126
+ rb_define_method(tid_class, "initialize", tid_initialize, -1);
127
+ rb_define_method(tid_class, "to_time", tid_to_time, 0);
128
+ rb_define_method(tid_class, "to_s", tid_to_s, 0);
129
+ rb_define_singleton_method(tid_class, "from_string", tid_from_string, 1);
130
+ }
@@ -0,0 +1,19 @@
1
+ require "at_protocol/tid"
2
+
3
+ class Time
4
+ def to_tid
5
+ ATProto::TID.new(self)
6
+ end
7
+ end
8
+
9
+ class String
10
+ def to_tid
11
+ ATProto::TID.from_string(self)
12
+ end
13
+ end
14
+
15
+ class ATProto::TID
16
+ def inspect
17
+ "#<ATProto::TID(#{self.to_s})>"
18
+ end
19
+ end
@@ -1,4 +1,4 @@
1
1
  # typed: strict
2
2
  module ATProto
3
- VERSION = "0.0.4.2"
3
+ VERSION = "0.0.4.3"
4
4
  end
data/lib/at_protocol.rb CHANGED
@@ -30,6 +30,6 @@ end
30
30
  require "skyfall/cid"
31
31
  require "xrpc"
32
32
 
33
- %w(requests session repo collection at_uri writes record record/strongref).each do |name|
33
+ %w(requests session repo collection at_uri writes record record/strongref tid_ext).each do |name|
34
34
  require "at_protocol/#{name}"
35
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: at_protocol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4.2
4
+ version: 0.0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shreyan Jain
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-09-26 00:00:00.000000000 Z
12
+ date: 2023-10-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: xrpc
@@ -25,13 +25,30 @@ dependencies:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: 0.1.5
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake-compiler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
28
42
  description: A Ruby gem for interacting with AT Protocol
29
43
  email:
30
44
  - shreyan.jain.9@outlook.com
31
45
  executables: []
32
- extensions: []
46
+ extensions:
47
+ - ext/at_protocol/extconf.rb
33
48
  extra_rdoc_files: []
34
49
  files:
50
+ - "./ext/at_protocol/extconf.rb"
51
+ - "./ext/at_protocol/tid.c"
35
52
  - "./lib/at_protocol.rb"
36
53
  - "./lib/at_protocol/at_uri.rb"
37
54
  - "./lib/at_protocol/collection.rb"
@@ -40,8 +57,10 @@ files:
40
57
  - "./lib/at_protocol/repo.rb"
41
58
  - "./lib/at_protocol/requests.rb"
42
59
  - "./lib/at_protocol/session.rb"
60
+ - "./lib/at_protocol/tid_ext.rb"
43
61
  - "./lib/at_protocol/version.rb"
44
62
  - "./lib/at_protocol/writes.rb"
63
+ - ext/at_protocol/extconf.rb
45
64
  homepage: https://github.com/ShreyanJain9/at_protocol
46
65
  licenses:
47
66
  - MIT
@@ -50,6 +69,7 @@ post_install_message:
50
69
  rdoc_options: []
51
70
  require_paths:
52
71
  - lib
72
+ - ext
53
73
  required_ruby_version: !ruby/object:Gem::Requirement
54
74
  requirements:
55
75
  - - ">="