Class: OStatus2::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/ostatus2/subscription.rb

Instance Method Summary collapse

Constructor Details

#initialize(topic_url, options = {}) ⇒ Subscription

Returns a new instance of Subscription.

Parameters:

  • topic_url (String)

    The URL of the topic of the subscription

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

Options Hash (options):

  • :webhook (String)

    Webhook URL

  • :hub (String)

    Hub URL to work with

  • :secret (String)

    Secret key of the subscription

  • :lease_seconds (String)

    Number of seconds to request subscription for (may not be respected by hub)

  • :token (String)

    Verification token of the subscription



10
11
12
13
14
15
16
# File 'lib/ostatus2/subscription.rb', line 10

def initialize(topic_url, options = {})
  @topic_url     = topic_url
  @webhook_url   = options[:webhook]         || ''
  @secret        = options[:secret]          || ''
  @lease_seconds = options[:lease_seconds]   || ''
  @hub           = options[:hub]             || ''
end

Instance Method Details

#subscribeHTTP::Response

Subscribe to the topic via a specified hub

Returns:

  • (HTTP::Response)

Raises:

  • (HTTP::Error)

    Error raised upon delivery failure

  • (OpenSSL::SSL::SSLError)

    Error raised upon SSL-related failure during delivery



22
23
24
# File 'lib/ostatus2/subscription.rb', line 22

def subscribe
  update_subscription(:subscribe)
end

#unsubscribeHTTP::Response

Unsubscribe from the topic via a specified hub

Returns:

  • (HTTP::Response)

Raises:

  • (HTTP::Error)

    Error raised upon delivery failure

  • (OpenSSL::SSL::SSLError)

    Error raised upon SSL-related failure during delivery



30
31
32
# File 'lib/ostatus2/subscription.rb', line 30

def unsubscribe
  update_subscription(:unsubscribe)
end

#valid?(topic_url) ⇒ Boolean

Check if the hub is responding to the right subscription request

Parameters:

  • topic_url (String)

    A hub.topic from the hub

Returns:

  • (Boolean)


37
38
39
# File 'lib/ostatus2/subscription.rb', line 37

def valid?(topic_url)
  @topic_url == topic_url
end

#verify(content, signature) ⇒ Boolean

Verify that the feed contents were meant for this subscription

Parameters:

  • content (String)
  • signature (String)

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/ostatus2/subscription.rb', line 45

def verify(content, signature)
  hmac = OpenSSL::HMAC.hexdigest('sha1', @secret, content)
  signature.downcase == "sha1=#{hmac}"
end