123456789_123456789_123456789_123456789_123456789_

Module: Octokit::Client::PubSubHubbub

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/octokit/client/pub_sub_hubbub.rb

Overview

Methods for the PubSubHubbub API

Instance Method Summary

Instance Method Details

#pub_sub_hubbub_request(options = {}) (private)

[ GitHub ]

  
# File 'lib/octokit/client/pub_sub_hubbub.rb', line 88

def pub_sub_hubbub_request(options = {})
  # This method is janky, bypass normal stack so we don't
  # serialize request as JSON
  conn = Faraday.new(url: @api_endpoint) do |http|
    http.headers[:user_agent] = user_agent
    if basic_authenticated?
      http.request(*FARADAY_BASIC_AUTH_KEYS, @login, @password)
    elsif token_authenticated?
      http.request :authorization, 'token', @access_token
    end
    http.request :url_encoded
    http.use Octokit::Response::RaiseError
    http.adapter Faraday.default_adapter
  end

  conn.post do |req|
    req.url 'hub'
    req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    req.body = options
  end
end

#subscribe(topic, callback, secret = nil) ⇒ Boolean

Subscribe to a pubsub topic

Examples:

Subscribe to push events from one of your repositories, having an email sent when fired

client = Octokit::Client.new(:oauth_token = "token")
client.subscribe("https://github.com/joshk/devise_imapable/events/push", "github://Email?address=josh.kalderimis@gmail.com")

Parameters:

  • topic (String)

    A recoginized and supported pubsub topic

  • callback (String)

    A callback url to be posted to when the topic event is fired

  • secret (String) (defaults to: nil)

    An optional shared secret used to generate a SHA1 HMAC of the outgoing body content

Returns:

  • (Boolean)

    true if the subscribe was successful, otherwise an error is raised

See Also:

[ GitHub ]

  
# File 'lib/octokit/client/pub_sub_hubbub.rb', line 19

def subscribe(topic, callback, secret = nil)
  options = {
    "hub.callback": callback,
    "hub.mode": 'subscribe',
    "hub.topic": topic
  }
  options.merge!("hub.secret": secret) unless secret.nil?

  response = pub_sub_hubbub_request(options)

  response.status == 204
end

#subscribe_service_hook(repo, service_name, service_arguments = {}, secret = nil) ⇒ Boolean

Subscribe to a repository through pubsub

Examples:

Subscribe to push events to one of your repositories to Travis-CI

client = Octokit::Client.new(:oauth_token = "token")
client.subscribe_service_hook('joshk/device_imapable', 'Travis', { :token => "test", :domain => "domain", :user => "user" })

Parameters:

  • repo (String, Repository, Hash)

    A GitHub repository

  • service_name (String)

    service name owner

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

    params that will be passed by subscribed hook. List of services is available @ https://github.com/github/github-services/tree/master/docs. Please refer Data node for complete list of arguments.

  • secret (String) (defaults to: nil)

    An optional shared secret used to generate a SHA1 HMAC of the outgoing body content

Returns:

  • (Boolean)

    True if subscription successful, false otherwise

See Also:

[ GitHub ]

  
# File 'lib/octokit/client/pub_sub_hubbub.rb', line 65

def subscribe_service_hook(repo, service_name, service_arguments = {}, secret = nil)
  topic = "#{Octokit.web_endpoint}#{Repository.new(repo)}/events/push"
  callback = "github://#{service_name}?#{service_arguments.collect { |k, v| [k, v].map { |p| URI.encode_www_form_component(p) }.join('=') }.join('&')}"
  subscribe(topic, callback, secret)
end

#unsubscribe(topic, callback) ⇒ Boolean

Unsubscribe from a pubsub topic

Examples:

Unsubscribe to push events from one of your repositories, no longer having an email sent when fired

client = Octokit::Client.new(:oauth_token = "token")
client.unsubscribe("https://github.com/joshk/devise_imapable/events/push", "github://Email?address=josh.kalderimis@gmail.com")

Parameters:

  • topic (String)

    A recoginized pubsub topic

  • callback (String)

    A callback url to be unsubscribed from

Returns:

  • (Boolean)

    true if the unsubscribe was successful, otherwise an error is raised

See Also:

[ GitHub ]

  
# File 'lib/octokit/client/pub_sub_hubbub.rb', line 41

def unsubscribe(topic, callback)
  options = {
    "hub.callback": callback,
    "hub.mode": 'unsubscribe',
    "hub.topic": topic
  }
  response = pub_sub_hubbub_request(options)

  response.status == 204
end

#unsubscribe_service_hook(repo, service_name)

Unsubscribe repository through pubsub

Examples:

Subscribe to push events to one of your repositories to Travis-CI

client = Octokit::Client.new(:oauth_token = "token")
client.unsubscribe_service_hook('joshk/device_imapable', 'Travis')

Parameters:

See Also:

[ GitHub ]

  
# File 'lib/octokit/client/pub_sub_hubbub.rb', line 80

def unsubscribe_service_hook(repo, service_name)
  topic = "#{Octokit.web_endpoint}#{Repository.new(repo)}/events/push"
  callback = "github://#{service_name}"
  unsubscribe(topic, callback)
end