Class: Hootenanny::Subscription
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Hootenanny::Subscription
- Extended by:
- Chronological
- Defined in:
- app/models/hootenanny/subscription.rb
Class Method Summary collapse
-
.subscribe(options = {}) ⇒ Object
Private: Subscribes a given subscriber to a given topic URL by creating a Subscription.
-
.to(topic) ⇒ Object
Private: Finds all subscriptions which are subscribed to a given topic.
Instance Method Summary collapse
Class Method Details
.subscribe(options = {}) ⇒ Object
Private: Subscribes a given subscriber to a given topic URL by creating a Subscription.
This operation is idempotent. It will not modify a subscription if one already exists for the subscriber/topic passed in.
options - A Hash of options
:subscriber - A String representing the URI which should be
notified when changes occur on the topic
:to - A String representing the topic URI which is
publishing the items that the subscriber is
interested in.
:digest_secret - A String token of no more than 200 bytes which
will be used during content distribution so that
the subscriber can verify that the request is
from the hub and not a undesired 3rd party
(optional).
:lease_duration - A Number representing the length of time (in
seconds) after the subscription is created until
it should be considered invalid
Example:
# If a subscription does not yet exist for the subscriber/topic, it
# creates # a new Subscription
Subscription.subscribe(subscriber: 'http://example.com/my_callback',
to: 'http://example.org/my_topic')
# => <Subscription subscriber: 'http://example.com/my_callback',
topic: 'http://example.org/my_topic'>
# If a subscription already exists for the subscriber/topic, it retrieves
# and returns the current Subscription with all other attributes updated
# as if the subscription were new.
Subscription.subscribe(subscriber: 'http://example.com/my_callback',
to: 'http://example.org/my_topic',
digest_secret: 'new_secret')
# => <Subscription subscriber: 'http://example.com/my_callback',
topic: 'http://example.org/my_topic',
digest_secret: 'new_secret'>
Returns a Subscription Raises a Hootenanny::URI::InvalidSchemeError if the URIs are using something
other than HTTP or HTTPS
Raises a Hootenanny::URI::InvalidError if the URI can’t be parsed properly
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/hootenanny/subscription.rb', line 91 def self.subscribe( = {}) subscriber = Hootenanny::URI.parse(.fetch(:subscriber)).to_s topic = Hootenanny::URI.parse(.fetch(:to)).to_s attrs = {} attrs[:started_at] = Time.now.utc attrs[:digest_secret] = .fetch(:digest_secret, nil) attrs[:lease_duration] = .fetch(:lease_duration) subscription = find_or_initialize(subscriber, topic) subscription.update_attributes(attrs) subscription end |
.to(topic) ⇒ Object
Private: Finds all subscriptions which are subscribed to a given topic
It takes into account only active subscriptions. If the subscription has expired, it will not be returned.
topic - A String represeenting a topic which has been subscribed to
Example:
Subscription.to('http://example.com/foo')
# => <ActiveRelation>
Returns an ActiveRelation which (if it has not been chained) represents the
subscriptions which are subscribed to the topic.
34 35 36 |
# File 'app/models/hootenanny/subscription.rb', line 34 def self.to(topic) active.where(:topic => topic.to_s) end |
Instance Method Details
#subscriber ⇒ Object
107 108 109 |
# File 'app/models/hootenanny/subscription.rb', line 107 def subscriber Hootenanny::URI.parse(read_attribute(:subscriber)) end |