Class: DistributedPress::V1::Social::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, HTTParty::Cache
Defined in:
lib/distributed_press/v1/social/client.rb

Overview

TODO:

It’d be nice to implement this on HTTParty itself

Social Distributed Press APIv1 client

Inspired by Mastodon’s Request

Defined Under Namespace

Classes: ActivityIdTooLargeError, BrotliUnsupportedError, ContentTooLargeError, EmptyResponseError, 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

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • :url (String)

    Social Distributed Press URL

  • :public_key (String)

    URL where public key is available

  • :private_key_pem (String)

    RSA Private key, PEM encoded

  • :key_size (Integer)
  • :logger (Logger)
  • :cache_store (HTTParty::Cache::Store::Abstract, Symbol)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/distributed_press/v1/social/client.rb', line 83

def initialize(public_key_url:, url: 'https://social.distributed.press', private_key_pem: nil, key_size: 2048,
               logger: nil, cache_store: :memory)
  self.class.default_options[:logger] = @logger = logger
  self.class.default_options[: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_sizeInteger (readonly)

RSA key size

Returns:

  • (Integer)


65
66
67
# File 'lib/distributed_press/v1/social/client.rb', line 65

def key_size
  @key_size
end

#loggerLogger (readonly)

Logger

Returns:

  • (Logger)


75
76
77
# File 'lib/distributed_press/v1/social/client.rb', line 75

def logger
  @logger
end

#public_key_urlString (readonly)

Public key URL

Returns:

  • (String)


70
71
72
# File 'lib/distributed_press/v1/social/client.rb', line 70

def public_key_url
  @public_key_url
end

#uriURI (readonly)

API URI

Returns:

  • (URI)


60
61
62
# File 'lib/distributed_press/v1/social/client.rb', line 60

def uri
  @uri
end

#urlString (readonly)

API URL

Returns:

  • (String)


55
56
57
# File 'lib/distributed_press/v1/social/client.rb', line 55

def url
  @url
end

Class Method Details

._load(hash) ⇒ Object



109
110
111
# File 'lib/distributed_press/v1/social/client.rb', line 109

def self._load(hash)
  new(**Marshal.load(hash))
end

Instance Method Details

#_dump(_) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/distributed_press/v1/social/client.rb', line 99

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

Parameters:

  • :endpoint (String)

Returns:

  • (String)


197
198
199
# File 'lib/distributed_press/v1/social/client.rb', line 197

def absolute_endpoint(endpoint)
  "#{uri.path}/#{endpoint}".squeeze('/')
end

#absolute_url(endpoint) ⇒ String

Absolute URL for an endpoint

Parameters:

  • :endpoint (String)

Returns:

  • (String)


205
206
207
208
209
# File 'lib/distributed_press/v1/social/client.rb', line 205

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

Parameters:

  • endpoint (String)
  • body (Hash) (defaults to: nil)
  • serializer (Proc) (defaults to: @serializer)
  • parser (HTTParty::Parser, Proc) (defaults to: HTTParty::Parser)
  • content_type (String) (defaults to: CONTENT_TYPE)
  • accept (Array<String>) (defaults to: ACCEPT)

Returns:

  • (HTTParty::Response)


165
166
167
168
169
# File 'lib/distributed_press/v1/social/client.rb', line 165

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

Parameters:

  • endpoint (String)
  • parser (HTTParty::Parser, Proc) (defaults to: HTTParty::Parser)
  • content_type (String) (defaults to: CONTENT_TYPE)
  • accept (Array<String>) (defaults to: ACCEPT)

Returns:

  • (HTTParty::Response)


120
121
122
123
# File 'lib/distributed_press/v1/social/client.rb', line 120

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

#hostString

Host

Returns:

  • (String)


188
189
190
# File 'lib/distributed_press/v1/social/client.rb', line 188

def host
  @host ||= uri.host
end

#post(endpoint:, body: nil, serializer: @serializer, parser: HTTParty::Parser, content_type: CONTENT_TYPE, accept: ACCEPT) ⇒ HTTParty::Response

TODO:

Use DRY-schemas

POST request

Parameters:

  • endpoint (String)
  • body (Hash) (defaults to: nil)
  • serializer (Proc) (defaults to: @serializer)
  • parser (HTTParty::Parser, Proc) (defaults to: HTTParty::Parser)
  • content_type (String) (defaults to: CONTENT_TYPE)
  • accept (Array<String>) (defaults to: ACCEPT)

Returns:

  • (HTTParty::Response)


135
136
137
138
139
# File 'lib/distributed_press/v1/social/client.rb', line 135

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_keyOpenSSL::PKey::RSA

Loads or generates a private key

Returns:

  • (OpenSSL::PKey::RSA)


174
175
176
# File 'lib/distributed_press/v1/social/client.rb', line 174

def private_key
  @private_key ||= OpenSSL::PKey::RSA.new(@private_key_pem || key_size)
end

#public_keyOpenSSL::PKey::RSA

Public key

Returns:

  • (OpenSSL::PKey::RSA)


181
182
183
# File 'lib/distributed_press/v1/social/client.rb', line 181

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

Parameters:

  • endpoint (String)
  • body (Hash) (defaults to: nil)
  • serializer (Proc) (defaults to: @serializer)
  • parser (HTTParty::Parser, Proc) (defaults to: HTTParty::Parser)
  • content_type (String) (defaults to: CONTENT_TYPE)
  • accept (Array<String>) (defaults to: ACCEPT)

Returns:

  • (HTTParty::Response)


150
151
152
153
154
# File 'lib/distributed_press/v1/social/client.rb', line 150

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