Class: DistributedPress::V1::Social::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/distributed_press/v1/social/hook.rb

Overview

Manages the actor’s hooks on the Social Inbox

Defined Under Namespace

Classes: Error, EventNotValidError, ValidationError

Constant Summary collapse

ACCEPT =
%w[text/plain].freeze
CONTENT_TYPE =
'text/plain'
EVENTS =
%w[moderationqueued onapproved onrejected].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, actor:) ⇒ Hook

Returns a new instance of Hook.

Parameters:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/distributed_press/v1/social/hook.rb', line 27

def initialize(client:, actor:)
  @client = client
  @actor = actor
  # The serializer validates the body format and raises an
  # exception if it contains errors.
  @serializer = proc do |body|
    body.tap do |b|
      next if b.errors.empty?

      raise ValidationError, body.errors.to_h.map do |key, messages|
        messages.map do |message|
          "#{key} #{message}"
        end
      end.flatten.join(', ')
    end.to_h.to_json
  end

  # XXX: format is nil but should be :json
  @parser = proc do |body, format|
    next HTTParty::Parser.call(body, format || :plain) unless body.start_with? '{'

    json = HTTParty::Parser.call(body, :json)

    DistributedPress::V1::Social::Schemas::Webhook.new.call(json)
  end
end

Instance Attribute Details

#actorString (readonly)

Returns:

  • (String)


23
24
25
# File 'lib/distributed_press/v1/social/hook.rb', line 23

def actor
  @actor
end

#clientDistributedPress::V1::Social::Client (readonly)



20
21
22
# File 'lib/distributed_press/v1/social/hook.rb', line 20

def client
  @client
end

Instance Method Details

#delete(event:) ⇒ HTTParty::Response

Removes a hook for an event

Parameters:

  • :event (String)

Returns:

  • (HTTParty::Response)


79
80
81
82
83
# File 'lib/distributed_press/v1/social/hook.rb', line 79

def delete(event:)
  validate_event! event

  client.delete(endpoint: "#{endpoint}/#{event}", serializer: @serializer, parser: @parser)
end

#endpointString

Endpoint

Returns:

  • (String)


88
89
90
# File 'lib/distributed_press/v1/social/hook.rb', line 88

def endpoint
  @endpoint ||= "/v1/#{actor}/hooks"
end

#get(event:) ⇒ HTTParty::Response

Gets a hook and validates return, or 404 when not found

Parameters:

  • :event (String)

Returns:

  • (HTTParty::Response)


58
59
60
61
62
# File 'lib/distributed_press/v1/social/hook.rb', line 58

def get(event:)
  validate_event! event

  client.get(endpoint: "#{endpoint}/#{event}", parser: @parser)
end

#put(event:, hook:) ⇒ HTTParty::Response

Creates a webhook

Parameters:

Returns:

  • (HTTParty::Response)


69
70
71
72
73
# File 'lib/distributed_press/v1/social/hook.rb', line 69

def put(event:, hook:)
  validate_event! event

  client.put(endpoint: "#{endpoint}/#{event}", body: hook, serializer: @serializer, parser: @parser)
end