Module: Tempest::Commands::Post
- Defined in:
- lib/tempest/commands/post.rb
Constant Summary collapse
- MAX_GRAPHEMES =
300
Class Method Summary collapse
-
.build_reply(uri, client:) ⇒ Object
Look up the parent’s cid via com.atproto.repo.getRecord.
- .call(argv:, session:, client:, stdout:, stderr:, stdin:) ⇒ Object
- .parse(argv) ⇒ Object
- .parse_at_uri(uri) ⇒ Object
- .read_text(positional, stdin:) ⇒ Object
Class Method Details
.build_reply(uri, client:) ⇒ Object
Look up the parent’s cid via com.atproto.repo.getRecord. AT Proto requires both uri and cid on a reply ref; we only have the URI from the CLI flag, so the lookup is necessary.
84 85 86 87 88 89 90 91 92 |
# File 'lib/tempest/commands/post.rb', line 84 def build_reply(uri, client:) return nil if uri.nil? || uri.empty? repo, collection, rkey = parse_at_uri(uri) record = client.get( "com.atproto.repo.getRecord", query: { "repo" => repo, "collection" => collection, "rkey" => rkey }, ) { uri: record.fetch("uri"), cid: record.fetch("cid") } end |
.call(argv:, session:, client:, stdout:, stderr:, stdin:) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tempest/commands/post.rb', line 11 def call(argv:, session:, client:, stdout:, stderr:, stdin:) opts, positional = parse(argv) return 64 if opts[:invalid] text = read_text(positional, stdin: stdin) if text.nil? || text.strip.empty? stderr.puts "error: empty post text" return 64 end if text.grapheme_clusters.length > MAX_GRAPHEMES stderr.puts "error: post exceeds #{MAX_GRAPHEMES} graphemes" return 64 end reply = build_reply(opts[:reply_to], client: client) response = Tempest::Post.create( client, did: session.did, text: text, reply: reply, langs: opts[:langs], ) if opts[:json] require "json" stdout.puts JSON.generate( "uri" => response["uri"], "cid" => response["cid"], ) else stdout.puts "posted: #{response["uri"]}" end 0 end |
.parse(argv) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/tempest/commands/post.rb', line 43 def parse(argv) opts = { langs: ["ja"], json: false, reply_to: nil, invalid: false } positional = [] i = 0 while i < argv.length a = argv[i] case a when "--lang" opts[:langs] = argv[i + 1].to_s.split(",") i += 2 when /\A--lang=(.+)\z/ opts[:langs] = $1.split(",") i += 1 when "--reply-to" opts[:reply_to] = argv[i + 1] i += 2 when /\A--reply-to=(.+)\z/ opts[:reply_to] = $1 i += 1 when "--json" opts[:json] = true i += 1 else positional << a i += 1 end end [opts, positional] end |
.parse_at_uri(uri) ⇒ Object
94 95 96 97 98 |
# File 'lib/tempest/commands/post.rb', line 94 def parse_at_uri(uri) match = uri.match(%r{\Aat://([^/]+)/([^/]+)/(.+)\z}) raise ArgumentError, "invalid at:// URI: #{uri.inspect}" unless match [match[1], match[2], match[3]] end |
.read_text(positional, stdin:) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/tempest/commands/post.rb', line 73 def read_text(positional, stdin:) if positional == ["-"] stdin.read.to_s.chomp else positional.join(" ") end end |