-
Notifications
You must be signed in to change notification settings - Fork 0
/
pub_sub_hubbub.cr
116 lines (109 loc) · 4.27 KB
/
pub_sub_hubbub.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
require "http/params"
module Octokit
class Client
# Methods for the PubSubHubbub API
#
# **Note:** The pubsub api requires clients to be OAuth authenticated.
#
# **See Also:**
# - [https://developer.github.com/v3/repos/hooks/#pubsubhubbub](https://developer.github.com/v3/repos/hooks/#pubsubhubbub)
module PubSubHubbub
# Subscribe to a pubsub topic
#
# **See Also:**
# - [https://developer.github.com/v3/repos/hooks/#subscribing](https://developer.github.com/v3/repos/hooks/#subscribing)
#
# **Example:**
#
# Subscribe to push events from one of your repositories, having an email sent when fired.
# ```
# @client = Octokit.client.(oauth_token: "token")
# @client.subscribe("https://github.com/watzon/cadmium/events/push", "github://Email?address=chris@watzon.me")
# ```
def subscribe(topic, callback, secret = nil)
options = {
"hub.callback": callback,
"hub.mode": "subscribe",
"hub.topic": topic,
}
options = options.merge({"hub.secret": secret}) unless secret.nil?
pub_sub_hubbub_request(options)
end
# Unsubscribe from a pubsub topic
#
# **See Also:**
# - [https://developer.github.com/v3/repos/hooks/#subscribing](https://developer.github.com/v3/repos/hooks/#subscribing)
#
# **Example:**
#
# Unsubscribe to push events from one of your repositories, no longer having an email sent when fired
# ```
# @client = Octokit.client(oauth_token: "token")
# @client.unsubscribe("https://github.com/watzon/cadmium/events/push", "github://Email?address=chris@watzon.me")
# ```
def unsubscribe(topic, callback)
options = {
"hub.callback": callback,
"hub.mode": "unsubscribe",
"hub.topic": topic,
}
pub_sub_hubbub_request(options)
end
# Subscribe to a repository through pubsub.
#
# **Note:** A list of services is available @
# [https://github.com/github/github-services/tree/master/docs](https://github.com/github/github-services/tree/master/docs).
#
# **See Also:**
# - [https://developer.github.com/v3/repos/hooks/#subscribing](https://developer.github.com/v3/repos/hooks/#subscribing)
#
# **Example:**
#
# Subscribe to push events to one of your repositories to Travis-CI
# ```
# @client = Octokit.client.(oauth_token: "token")
# @client.subscribe_service_hook("watzon/cadmium", "Travis", { :token => "test", :domain => "domain", :user => "user" })
# ```
def subscribe_service_hook(
repo,
service_name,
service_arguments = {} of String => String,
secret = nil
)
topic = "#{File.join(Octokit.web_endpoint, Repository.new(repo))}/events/push"
callback = "github://#{service_name}?#{HTTP::Params.encode(service_arguments)}"
subscribe(topic, callback, secret)
end
# Unsubscribe from a repository through pubsub.
#
# **Note:** A list of services is available @
# [https://github.com/github/github-services/tree/master/docs](https://github.com/github/github-services/tree/master/docs).
#
# **See Also:**
# - [https://developer.github.com/v3/repos/hooks/#subscribing](https://developer.github.com/v3/repos/hooks/#subscribing)
#
# **Example:**
#
# Subscribe to push events to one of your repositories to Travis-CI
# ```
# @client = Octokit.client.(oauth_token: "token")
# @client.unsubscribe_service_hook("watzon/cadmium", "Travis")
# ```
def unsubscribe_service_hook(
repo,
service_name
)
topic = "#{File.join(Octokit.web_endpoint, Repository.new(repo))}/events/push"
callback = "github://#{service_name}"
unsubscribe(topic, callback)
end
# Send the pubsub requests as form-urlencoded data and ensure that
# they are token authenticated before doing so.
protected def pub_sub_hubbub_request(options)
ensure_token_authenticated!
headers = {"Content-Type": "application/x-www-form-urlencoded"}
boolean_from_response :post, "hub", {headers: headers, form_data: options}
end
end
end
end