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: 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

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)


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.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)


63
64
65
# File 'lib/distributed_press/v1/social/client.rb', line 63

def key_size
  @key_size
end

#loggerLogger (readonly)

Logger

Returns:

  • (Logger)


73
74
75
# File 'lib/distributed_press/v1/social/client.rb', line 73

def logger
  @logger
end

#public_key_urlString (readonly)

Public key URL

Returns:

  • (String)


68
69
70
# File 'lib/distributed_press/v1/social/client.rb', line 68

def public_key_url
  @public_key_url
end

#uriURI (readonly)

API URI

Returns:

  • (URI)


58
59
60
# File 'lib/distributed_press/v1/social/client.rb', line 58

def uri
  @uri
end

#urlString (readonly)

API URL

Returns:

  • (String)


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

Parameters:

  • :endpoint (String)

Returns:

  • (String)


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

Parameters:

  • :endpoint (String)

Returns:

  • (String)


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

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)


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

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)


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

#hostString

Host

Returns:

  • (String)


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

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)


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

Loads or generates a private key

Returns:

  • (OpenSSL::PKey::RSA)


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

Public key

Returns:

  • (OpenSSL::PKey::RSA)


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

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)


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