Class: OSub::Subscription

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callback_url, topic_url, secret = nil, token = nil) ⇒ Subscription

Returns a new instance of Subscription.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/osub/subscription.rb', line 12

def initialize(callback_url, topic_url, secret = nil, token = nil)
  @tokens = []
  if token != nil
    @tokens << token
  end

  secret = "" if secret == nil
  @secret = secret.to_s

  @callback_url = callback_url
  @topic_url = topic_url
end

Instance Attribute Details

#callback_urlObject (readonly)

Returns the value of attribute callback_url.



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

def callback_url
  @callback_url
end

#topic_urlObject (readonly)

Returns the value of attribute topic_url.



10
11
12
# File 'lib/osub/subscription.rb', line 10

def topic_url
  @topic_url
end

Instance Method Details

#change_subscription(mode, hub_url, async, token) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/osub/subscription.rb', line 46

def change_subscription(mode, hub_url, async, token)
  res = Net::HTTP.post_form(URI.parse(hub_url),
                            { 'hub.mode' => mode.to_s,
                              'hub.callback' => @callback_url,
                              'hub.verify' => async ? 'async' : 'sync',
                              'hub.verify_token' => token,
                              'hub.lease_seconds' => '',
                              'hub.secret' => @secret,
                              'hub.topic' => @topic_url})
end

#hubsObject

Actively searches for hubs by talking to publisher directly



26
27
28
# File 'lib/osub/subscription.rb', line 26

def hubs
  OStatus::Feed.from_url(topic_url).hubs
end

#perform_challenge(challenge_code) ⇒ Object



70
71
72
# File 'lib/osub/subscription.rb', line 70

def perform_challenge(challenge_code)
  {:body => challenge_code, :status => 200}
end

#subscribe(hub_url, async = false, token = nil) ⇒ Object

Subscribe to the topic through the given hub.



31
32
33
34
35
36
# File 'lib/osub/subscription.rb', line 31

def subscribe(hub_url, async = false, token = nil)
  if token != nil
    @tokens << token.to_s
  end
  change_subscription(:subscribe, hub_url, async, token)
end

#unsubscribe(hub_url, async = false, token = nil) ⇒ Object

Unsubscribe to the topic through the given hub.



39
40
41
42
43
44
# File 'lib/osub/subscription.rb', line 39

def unsubscribe(hub_url, async = false, token = nil)
  if token != nil
    @tokens << token.to_s
  end
  change_subscription(:unsubscribe, hub_url, async, token)
end

#verify_content(body, signature) ⇒ Object



64
65
66
67
68
# File 'lib/osub/subscription.rb', line 64

def verify_content(body, signature)
  hmac = HMAC::SHA1.hexdigest(@secret, body)
  check = "sha1=" + hmac
  check == signature
end

#verify_subscription(token) ⇒ Object



57
58
59
60
61
62
# File 'lib/osub/subscription.rb', line 57

def verify_subscription(token)
  result = @tokens.index(token) != nil
  @tokens.delete(token)

  result
end