Class: Hootenanny::Request::Subscription

Inherits:
Hootenanny::Request show all
Defined in:
lib/hootenanny/request/subscription.rb

Instance Attribute Summary collapse

Attributes inherited from Hootenanny::Request

#type

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Subscription

Returns a new instance of Subscription.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hootenanny/request/subscription.rb', line 16

def initialize(options = {})
  self.subscriber         = options.fetch(:subscriber)
  self.topic              = options.fetch(:topic)
  self.lease_duration     = options.fetch(:lease_duration)
  self.requester_token    = options.fetch(:requester_token, nil)
  self.digest_secret      = options.fetch(:secret, nil)
  self.verification_class = options.fetch(:verification_class,
                                          Hootenanny::Verification)
rescue KeyError => e
  raise Hootenanny::Request::BuildError.wrap(e)
end

Instance Attribute Details

#digest_secretObject

Returns the value of attribute digest_secret.



9
10
11
# File 'lib/hootenanny/request/subscription.rb', line 9

def digest_secret
  @digest_secret
end

#lease_durationObject

Returns the value of attribute lease_duration.



9
10
11
# File 'lib/hootenanny/request/subscription.rb', line 9

def lease_duration
  @lease_duration
end

#requester_tokenObject

Returns the value of attribute requester_token.



9
10
11
# File 'lib/hootenanny/request/subscription.rb', line 9

def requester_token
  @requester_token
end

#subscriberObject

Returns the value of attribute subscriber.



9
10
11
# File 'lib/hootenanny/request/subscription.rb', line 9

def subscriber
  @subscriber
end

#topicObject

Returns the value of attribute topic.



9
10
11
# File 'lib/hootenanny/request/subscription.rb', line 9

def topic
  @topic
end

#verification_classObject

Returns the value of attribute verification_class.



9
10
11
# File 'lib/hootenanny/request/subscription.rb', line 9

def verification_class
  @verification_class
end

Class Method Details

.build(options = {}) ⇒ Object

Private: Builds a subscription request object based on the options passed in.

options - A Hash of options representing the subscription request that will

be built.

:subscriber      - A String or Ruby URI representing the URI which
                   the topic updates will be sent to.
:topic           - A String or Ruby URI representing the URI which
                   publish the activities for a given topic.
:requester_token - A String token sent from the requester which
                   they can use to identify the request on their
                   system (optional).
: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 amount of time after
                   the subscription is created during which time
                   the subscription will be active. This is
                   a _request_ and may be overridden (optional).

Examples:

Hootenanny::Request::Subscription.build(callback:   'http://example.com',
                                        topic:      'http://another.org')

# => <Hootenanny::Request::Subscription subscriber: 'http://example.com',
                                        topic:      'http://another.org'>

Returns a Hootenanny::Request::Subscription Raises Hootenanny::Request::BuildError if there is a problem



63
64
65
66
67
68
69
70
# File 'lib/hootenanny/request/subscription.rb', line 63

def self.build(options = {})
  options = normalize_build_options(options)
  request = allocate

  request.send(:initialize, options)

  request
end

Instance Method Details

#applyObject

Private: Applies the subscription that the request is asking for. It does this in an idempotent way. If the subscription being requested already exists, it is not modified but is instead retrieved and returned.

The request itself must be verified prior to the request being applied. An unverifiable request will raise a Hootenanny::SubscriptionError.

Example:

# If the request for the subscription is verified

subscription_request =  Hootenanny::Request::Subscription.build(
                          callback: 'http://example.com',
                          topic:    'http://another.org')

subscription_request.apply

# => <Hootenanny::Subscription subscriber: 'http://example.com',
                               topic:      'http://another.org'>

# If the request for the subscription is unverifiable

subscription_request =  Hootenanny::Request::Subscription.build(
                          callback: 'http://iamahacker.com',
                          topic:    'http://thisdoesnotexist.org')

subscription_request.apply

# => <Hootenanny::SubscriptionError>

Returns a Hootenanny::Subscription if the request is verified Raises Hootenanny::SubscriptionError if there is a problem



108
109
110
111
112
113
114
115
# File 'lib/hootenanny/request/subscription.rb', line 108

def apply
  raise Hootenanny::SubscriptionError.new('Request could not be verified.') unless verified?

  Hootenanny::Subscription.subscribe( subscriber:     subscriber,
                                      to:             topic,
                                      lease_duration: lease_duration,
                                      digest_secret:  digest_secret)
end

#verificationObject



121
122
123
124
125
126
127
128
# File 'lib/hootenanny/request/subscription.rb', line 121

def verification
  @verification ||= verification_class.new(
                      url:             subscriber,
                      mode:            :subscribe,
                      topic:           topic,
                      requester_token: requester_token,
                    )
end

#verified?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/hootenanny/request/subscription.rb', line 117

def verified?
  verification.verified?
end