Class: DistributedPress::V1::Social::Client
- Inherits:
-
Object
- Object
- DistributedPress::V1::Social::Client
- Includes:
- HTTParty, HTTParty::Cache
- Defined in:
- lib/distributed_press/v1/social/client.rb
Overview
It’d be nice to implement this on HTTParty itself
Social Distributed Press APIv1 client
Inspired by Mastodon’s Request
Defined Under Namespace
Classes: BrotliUnsupportedError, ContentTooLargeError, Error
Constant Summary collapse
- ACCEPT =
Content types sent and accepted
%w[application/activity+json application/ld+json application/json].freeze
- CONTENT_TYPE =
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
Instance Attribute Summary collapse
-
#key_size ⇒ Integer
readonly
RSA key size.
-
#logger ⇒ Logger
readonly
Logger.
-
#public_key_url ⇒ String
readonly
Public key URL.
-
#uri ⇒ URI
readonly
API URI.
-
#url ⇒ String
readonly
API URL.
Class Method Summary collapse
Instance Method Summary collapse
- #_dump(_) ⇒ Object
-
#absolute_endpoint(endpoint) ⇒ String
If the endpoint is on a subdirectory, we need the absolute path for signing.
-
#absolute_url(endpoint) ⇒ String
Absolute URL for an endpoint.
-
#delete(endpoint:, body: nil, serializer: @serializer, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) ⇒ HTTParty::Response
DELETE request with optional body.
-
#get(endpoint:, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) ⇒ HTTParty::Response
GET request.
-
#host ⇒ String
Host.
-
#initialize(public_key_url:, url: 'https://social.distributed.press', private_key_pem: nil, key_size: 2048, logger: nil, cache_store: :memory) ⇒ Client
constructor
A new instance of Client.
-
#post(endpoint:, body: nil, serializer: @serializer, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) ⇒ HTTParty::Response
POST request.
-
#private_key ⇒ OpenSSL::PKey::RSA
Loads or generates a private key.
-
#public_key ⇒ OpenSSL::PKey::RSA
Public key.
-
#put(endpoint:, body: nil, serializer: @serializer, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) ⇒ HTTParty::Response
PUT request.
Constructor Details
#initialize(public_key_url:, url: 'https://social.distributed.press', private_key_pem: nil, key_size: 2048, logger: nil, cache_store: :memory) ⇒ Client
Returns a new instance of Client.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/distributed_press/v1/social/client.rb', line 81 def initialize(public_key_url:, url: 'https://social.distributed.press', private_key_pem: nil, key_size: 2048, logger: nil, cache_store: :memory) self.class.[:logger] = @logger = logger self.class.[:log_level] = :debug self.class.cache_store cache_store @url = HTTParty.normalize_base_uri(url) @public_key_url = public_key_url @key_size = key_size @private_key_pem = private_key_pem @uri = URI.parse(url) @serializer ||= proc do |body| body.to_json end end |
Instance Attribute Details
#key_size ⇒ Integer (readonly)
RSA key size
63 64 65 |
# File 'lib/distributed_press/v1/social/client.rb', line 63 def key_size @key_size end |
#logger ⇒ Logger (readonly)
Logger
73 74 75 |
# File 'lib/distributed_press/v1/social/client.rb', line 73 def logger @logger end |
#public_key_url ⇒ String (readonly)
Public key URL
68 69 70 |
# File 'lib/distributed_press/v1/social/client.rb', line 68 def public_key_url @public_key_url end |
#uri ⇒ URI (readonly)
API URI
58 59 60 |
# File 'lib/distributed_press/v1/social/client.rb', line 58 def uri @uri end |
#url ⇒ String (readonly)
API URL
53 54 55 |
# File 'lib/distributed_press/v1/social/client.rb', line 53 def url @url end |
Class Method Details
._load(hash) ⇒ Object
107 108 109 |
# File 'lib/distributed_press/v1/social/client.rb', line 107 def self._load(hash) new(**Marshal.load(hash)) end |
Instance Method Details
#_dump(_) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/distributed_press/v1/social/client.rb', line 97 def _dump(_) Marshal.dump({ public_key_url: public_key_url, url: url, private_key_pem: @private_key_pem, key_size: key_size, cache_store: self.class.cache_store }) end |
#absolute_endpoint(endpoint) ⇒ String
If the endpoint is on a subdirectory, we need the absolute path for signing
195 196 197 |
# File 'lib/distributed_press/v1/social/client.rb', line 195 def absolute_endpoint(endpoint) "#{uri.path}/#{endpoint}".squeeze('/') end |
#absolute_url(endpoint) ⇒ String
Absolute URL for an endpoint
203 204 205 206 207 |
# File 'lib/distributed_press/v1/social/client.rb', line 203 def absolute_url(endpoint) uri.dup.tap do |u| u.path = absolute_endpoint(endpoint) end.to_s end |
#delete(endpoint:, body: nil, serializer: @serializer, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) ⇒ HTTParty::Response
DELETE request with optional body
163 164 165 166 167 |
# File 'lib/distributed_press/v1/social/client.rb', line 163 def delete(endpoint:, body: nil, serializer: @serializer, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) perform_signed_request method: Net::HTTP::Delete, endpoint: endpoint, body: body, serializer: serializer, parser: parser, content_type: content_type, accept: accept end |
#get(endpoint:, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) ⇒ HTTParty::Response
GET request
118 119 120 121 |
# File 'lib/distributed_press/v1/social/client.rb', line 118 def get(endpoint:, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) perform_signed_request method: Net::HTTP::Get, endpoint: endpoint, parser: parser, content_type: content_type, accept: accept end |
#host ⇒ String
Host
186 187 188 |
# File 'lib/distributed_press/v1/social/client.rb', line 186 def host @host ||= uri.host end |
#post(endpoint:, body: nil, serializer: @serializer, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) ⇒ HTTParty::Response
Use DRY-schemas
POST request
133 134 135 136 137 |
# File 'lib/distributed_press/v1/social/client.rb', line 133 def post(endpoint:, body: nil, serializer: @serializer, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) perform_signed_request method: Net::HTTP::Post, endpoint: endpoint, body: body, serializer: serializer, parser: parser, content_type: content_type, accept: accept end |
#private_key ⇒ OpenSSL::PKey::RSA
Loads or generates a private key
172 173 174 |
# File 'lib/distributed_press/v1/social/client.rb', line 172 def private_key @private_key ||= OpenSSL::PKey::RSA.new(@private_key_pem || key_size) end |
#public_key ⇒ OpenSSL::PKey::RSA
Public key
179 180 181 |
# File 'lib/distributed_press/v1/social/client.rb', line 179 def public_key private_key.public_key end |
#put(endpoint:, body: nil, serializer: @serializer, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) ⇒ HTTParty::Response
PUT request
148 149 150 151 152 |
# File 'lib/distributed_press/v1/social/client.rb', line 148 def put(endpoint:, body: nil, serializer: @serializer, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) perform_signed_request method: Net::HTTP::Put, endpoint: endpoint, body: body, serializer: serializer, parser: parser, content_type: content_type, accept: accept end |