Class: SpotifyWeb::Handler Private

Inherits:
Object
  • Object
show all
Includes:
Assertions, Loggable
Defined in:
lib/spotify_web/handler.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents a callback that’s been bound to a particular event

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#assert_valid_keys, #assert_valid_values

Constructor Details

#initialize(event, options = {}, &block) ⇒ Handler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds a new handler bound to the given event.

Parameters:

  • event (String)

    The name of the event to bind to

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

    The configuration options

Options Hash (options):

  • :once (Boolean) — default: false

    Whether to only call the handler once

  • :if (Hash) — default: nil

    Data that must be matched to run

Raises:

  • (ArgumentError)

    if an invalid option is specified



31
32
33
34
35
36
37
38
39
40
# File 'lib/spotify_web/handler.rb', line 31

def initialize(event, options = {}, &block)
  assert_valid_values(event, *Event.commands.values)
  assert_valid_keys(options, :once, :if)
  options = {:once => false, :if => nil}.merge(options)

  @event = event
  @once = options[:once]
  @conditions = options[:if]
  @block = block
end

Instance Attribute Details

#conditionsHash<String, Object> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The data that must be matched in order for the handler to run

Returns:

  • (Hash<String, Object>)


22
23
24
# File 'lib/spotify_web/handler.rb', line 22

def conditions
  @conditions
end

#eventString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The event this handler is bound to

Returns:

  • (String)


14
15
16
# File 'lib/spotify_web/handler.rb', line 14

def event
  @event
end

#onceBoolean (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether to only call the handler once and then never again

Returns:

  • (Boolean)

    true if only called once, otherwise false



18
19
20
# File 'lib/spotify_web/handler.rb', line 18

def once
  @once
end

Instance Method Details

#run(event) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs this handler for each result from the given event.

Parameters:

Returns:

  • (Boolean)

    true if conditions were matched to run the handler, otherwise false



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spotify_web/handler.rb', line 46

def run(event)
  if conditions_match?(event.data)
    # Run the block for each individual result
    event.results.each do |args|
      begin
        @block.call(*args)
      rescue StandardError => ex
        logger.error(([ex.message] + ex.backtrace) * "\n")
      end
    end

    true
  else
    false
  end
end