Class: CcMe::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cc_me.rb

Overview

A client bound to a single private key and base URL.

Instance Method Summary collapse

Constructor Details

#initialize(private_key:, base_url: nil) ⇒ Client

Returns a new instance of Client.

Raises:



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/cc_me.rb', line 248

def initialize(private_key:, base_url: nil)
  raise Error, "private_key is required" if private_key.nil? || private_key.empty?

  @private_key = private_key
  @base_url = CcMe.normalize_base(base_url)
  @signing_key = CcMe.signing_key(private_key)
  @public_key = CcMe.b64u_encode(@signing_key.verify_key.to_bytes)

  # Recipient X25519 secret key, derived from the Ed25519 seed the same way
  # libsodium's crypto_sign_ed25519_sk_to_curve25519 does: the first 32
  # bytes of SHA512(seed). The X25519 public key is then scalarmult_base of
  # that secret, which equals the Montgomery form of the Ed25519 public key.
  seed = CcMe.seed_bytes(private_key)
  @x_secret = RbNaCl::PrivateKey.new(Digest::SHA512.digest(seed)[0, 32])
  @x_public = @x_secret.public_key
end

Instance Method Details

#ack(ids) ⇒ Object



322
323
324
# File 'lib/cc_me.rb', line 322

def ack(ids)
  signed_post("ack", JSON.generate("ids" => Array(ids)))
end

#claim(limit: nil, poll: false, decrypt: true) ⇒ Object



315
316
317
318
319
320
# File 'lib/cc_me.rb', line 315

def claim(limit: nil, poll: false, decrypt: true)
  payload = { "poll" => poll }
  payload["limit"] = limit unless limit.nil?
  body = JSON.generate(payload)
  decrypt_response(signed_post("claim", body), decrypt)
end

#cloudevents_urlObject



294
295
296
# File 'lib/cc_me.rb', line 294

def cloudevents_url
  protocol_url("cloudevents")
end

#discord_url(app_public_key) ⇒ Object



298
299
300
301
302
303
304
# File 'lib/cc_me.rb', line 298

def discord_url(app_public_key)
  if app_public_key.nil? || app_public_key.to_s.empty?
    raise Error, "app_public_key is required"
  end

  "#{CcMe.trim_trailing_slash(@base_url)}#{inbox_path}/discord/#{CcMe.percent_encode(app_public_key)}"
end

#inbox_url(limit: nil, cursor: nil, poll: false) ⇒ Object

– URL helpers –



267
268
269
# File 'lib/cc_me.rb', line 267

def inbox_url(limit: nil, cursor: nil, poll: false)
  "#{CcMe.trim_trailing_slash(@base_url)}#{inbox_query(limit: limit, cursor: cursor, poll: poll)}"
end

#meta_url(verify_token = nil) ⇒ Object



287
288
289
290
291
292
# File 'lib/cc_me.rb', line 287

def meta_url(verify_token = nil)
  base = protocol_url("meta")
  return base if verify_token.nil?

  "#{base}?v=#{CcMe.encode_query_value(verify_token)}"
end

#peek(limit: nil, cursor: nil, poll: false, decrypt: true) ⇒ Object

– requests –



308
309
310
311
312
313
# File 'lib/cc_me.rb', line 308

def peek(limit: nil, cursor: nil, poll: false, decrypt: true)
  path_and_query = inbox_query(limit: limit, cursor: cursor, poll: poll)
  url = "#{CcMe.trim_trailing_slash(@base_url)}#{path_and_query}"
  headers = sign("GET", path_and_query, "")
  decrypt_response(CcMe.http_request("GET", url, nil, headers), decrypt)
end

#pingback_urlObject



283
284
285
# File 'lib/cc_me.rb', line 283

def pingback_url
  protocol_url("pingback")
end

#release(ids) ⇒ Object



326
327
328
# File 'lib/cc_me.rb', line 326

def release(ids)
  signed_post("release", JSON.generate("ids" => Array(ids)))
end

#slack_urlObject



279
280
281
# File 'lib/cc_me.rb', line 279

def slack_url
  protocol_url("slack")
end

#webmention_urlObject



271
272
273
# File 'lib/cc_me.rb', line 271

def webmention_url
  protocol_url("webmention")
end

#websub_urlObject



275
276
277
# File 'lib/cc_me.rb', line 275

def websub_url
  protocol_url("websub")
end