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) ⇒ Subscription

Returns a new instance of Subscription.



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

def initialize(callback_url, topic_url, secret = nil)
  @tokens = []

  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, token, redirect_limit = 10) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/osub/subscription.rb', line 43

def change_subscription(mode, hub_url, token, redirect_limit = 10)
  hub_uri = URI.parse(hub_url)

  req = Net::HTTP::Post.new(hub_uri.request_uri)
  req.set_form_data({
    'hub.mode' => mode.to_s,
    'hub.callback' => @callback_url,
    'hub.verify' => 'async',
    'hub.verify_token' => token,
    'hub.lease_seconds' => '',
    'hub.secret' => @secret,
    'hub.topic' => @topic_url
  })

  http = Net::HTTP.new(hub_uri.hostname, hub_uri.port)
  if hub_uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  end

  response = http.request(req)

  if response.is_a?(Net::HTTPRedirection) && redirect_limit > 0
    location = response['location']
    self.change_subscription(mode, hub_url, token, redirect_limit - 1)
  else
    response
  end

rescue OpenSSL::SSL::SSLError
  return nil
end

#hubsObject

Actively searches for hubs by talking to publisher directly



23
24
25
# File 'lib/osub/subscription.rb', line 23

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

#perform_challenge(challenge_code) ⇒ Object



89
90
91
# File 'lib/osub/subscription.rb', line 89

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

#subscribe(hub_url, token = nil) ⇒ Object

Subscribe to the topic through the given hub.



28
29
30
31
32
33
# File 'lib/osub/subscription.rb', line 28

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

#unsubscribe(hub_url, token = nil) ⇒ Object

Unsubscribe to the topic through the given hub.



36
37
38
39
40
41
# File 'lib/osub/subscription.rb', line 36

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

#verify_content(body, signature) ⇒ Object



83
84
85
86
87
# File 'lib/osub/subscription.rb', line 83

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

#verify_subscription(token) ⇒ Object



76
77
78
79
80
81
# File 'lib/osub/subscription.rb', line 76

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

  result
end