Class: Forem::Services::ReactionService

Inherits:
BaseService show all
Defined in:
lib/forem/services/reaction_service.rb

Overview

Service for interacting with the Forem Reactions API.

Reactions are emoji-based responses to articles, comments, and other reactable content (likes, unicorns, bookmarks, etc.). Access via Client#reactions. All methods inject the client's requestor automatically so no additional configuration is required.

Examples:

client = Forem::Client.new("your-api-key")
client.reactions.create(reactable_type: "Article", reactable_id: 1, category: "like")
client.reactions.toggle(reactable_type: "Article", reactable_id: 1, category: "unicorn")

See Also:

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Constructor Details

This class inherits a constructor from Forem::Services::BaseService

Instance Method Details

#create(params = {}, opts = {}) ⇒ Reaction

Create a reaction on a reactable resource.

Examples:

client.reactions.create(
  reactable_type: "Article",
  reactable_id: 12345,
  category: "unicorn"
)

Parameters:

  • params (Hash) (defaults to: {})

    reaction attributes

  • opts (Hash) (defaults to: {})

    per-request options

Options Hash (params):

  • :reactable_type (String)

    the type of content to react to ("Article", "Comment", or "User")

  • :reactable_id (Integer)

    the ID of the content to react to

  • :category (String)

    the reaction type ("like", "unicorn", "exploding_head", "raised_hands", "fire", "thumbsdown", "vomit", "readinglist")

Returns:

  • (Reaction)

    the newly created reaction

See Also:



38
39
40
# File 'lib/forem/services/reaction_service.rb', line 38

def create(params = {}, opts = {})
  Reaction.create(params, opts_with_requestor(opts))
end

#toggle(params = {}, opts = {}) ⇒ Hash

Toggle a reaction on a reactable resource.

If the reaction already exists for the authenticated user it will be destroyed; otherwise a new reaction will be created.

Examples:

result = client.reactions.toggle(
  reactable_type: "Article",
  reactable_id: 12345,
  category: "like"
)
puts result.result  # => "create" or "destroy"

Parameters:

  • params (Hash) (defaults to: {})

    reaction attributes

  • opts (Hash) (defaults to: {})

    per-request options

Options Hash (params):

  • :reactable_type (String)

    the type of content ("Article", "Comment", or "User")

  • :reactable_id (Integer)

    the ID of the content

  • :category (String)

    the reaction type ("like", "unicorn", "exploding_head", "raised_hands", "fire", "thumbsdown", "vomit", "readinglist")

Returns:

  • (Hash)

    a hash with a :result key indicating "create" or "destroy"

See Also:



67
68
69
# File 'lib/forem/services/reaction_service.rb', line 67

def toggle(params = {}, opts = {})
  Reaction.toggle(params, opts_with_requestor(opts))
end