Class: DistributedPress::V1::Social::Hook
- Inherits:
-
Object
- Object
- DistributedPress::V1::Social::Hook
- 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
- #actor ⇒ String readonly
- #client ⇒ DistributedPress::V1::Social::Client readonly
Instance Method Summary collapse
-
#delete(event:) ⇒ HTTParty::Response
Removes a hook for an event.
-
#endpoint ⇒ String
Endpoint.
-
#get(event:) ⇒ HTTParty::Response
Gets a hook and validates return, or 404 when not found.
-
#initialize(client:, actor:) ⇒ Hook
constructor
A new instance of Hook.
-
#put(event:, hook:) ⇒ HTTParty::Response
Creates a webhook.
Constructor Details
#initialize(client:, actor:) ⇒ Hook
Returns a new instance of Hook.
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, | .map do || "#{key} #{}" 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
#actor ⇒ String (readonly)
23 24 25 |
# File 'lib/distributed_press/v1/social/hook.rb', line 23 def actor @actor end |
#client ⇒ DistributedPress::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
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 |
#endpoint ⇒ String
Endpoint
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
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
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 |