Class: Pears::Provider::Subscription

Inherits:
Base
  • Object
show all
Defined in:
lib/pears/provider/subscription.rb

Overview

Config using redis subscription. This updates automatically upon Redis channel updates. This class would register as “alternative classes with different interfaces”. I ought to clean this up and maybe make “triggers” a different entity from a provider available in the builder.

Instance Attribute Summary collapse

Attributes inherited from Base

#data

Instance Method Summary collapse

Methods inherited from Base

#[], #each, #has_key?

Constructor Details

#initialize(channel = nil, redis_host: Pears.config.redis_host, **opts, &fetcher) ⇒ Subscription

Returns a new instance of Subscription.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pears/provider/subscription.rb', line 15

def initialize(channel=nil,
    redis_host: Pears.config.redis_host,
    **opts,
    &fetcher)
  @builder = opts[:builder] || Builder.new(nil)
  @channel = channel

  # Fetcher is a "Provider" of some sort.
  @fetcher = fetcher

  # Setup redis subscription
  establish_connection(redis_host)
  load_config
  subscribe
end

Instance Attribute Details

#subscriptionObject (readonly)

Returns the value of attribute subscription.



14
15
16
# File 'lib/pears/provider/subscription.rb', line 14

def subscription
  @subscription
end

Instance Method Details

#channelObject



31
32
33
# File 'lib/pears/provider/subscription.rb', line 31

def channel
  @channel ||= @builder.subject_name
end

#load_configObject



35
36
37
38
39
# File 'lib/pears/provider/subscription.rb', line 35

def load_config
  @builder.freeze_layers do
    @data = @fetcher.call.data
  end
end

#purgeObject



47
48
49
50
# File 'lib/pears/provider/subscription.rb', line 47

def purge
  unsubscribe
  true
end

#unsubscribeObject

This seems a bit “rough” of an approach. we should check if unsubscribing can be done more gracefully.



43
44
45
# File 'lib/pears/provider/subscription.rb', line 43

def unsubscribe
  @subscription.terminate
end