Class: RightHook::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/right_hook/subscriber.rb

Overview

Subscriber can subscribe and unsubscribe GitHub hooks to a hosted instance of a specified App. See the README for sample usage.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_opts = {}) ⇒ Subscriber

Initialize takes options which will be used as default values in other methods. The valid keys in the options are [base_url, oauth_token, owner, event_type, and user_agent].

Parameters:

  • opts (Hash)

    Subscription options. Defaults to attr_reader methods when such methods exist.



39
40
41
42
43
44
45
46
# File 'lib/right_hook/subscriber.rb', line 39

def initialize(default_opts = {})
  @base_url = default_opts[:base_url]
  @url = default_opts[:url]
  @oauth_token = default_opts[:oauth_token]
  @owner = default_opts[:owner]
  @event_type = default_opts[:event_type]
  @user_agent = default_opts[:user_agent]
end

Instance Attribute Details

#base_urlObject

The base URL for the binding (where your App is hosted).



10
11
12
# File 'lib/right_hook/subscriber.rb', line 10

def base_url
  @base_url
end

#event_typeObject

The event type of the hook. See developer.github.com/v3/repos/hooks/ for a complete list of valid types.



24
25
26
# File 'lib/right_hook/subscriber.rb', line 24

def event_type
  @event_type
end

#oauth_tokenObject

The OAuth token to use for authenticating with GitHub. The token must belong to an account that has the repo scope and collaborator privilege on the given repository.



17
18
19
# File 'lib/right_hook/subscriber.rb', line 17

def oauth_token
  @oauth_token
end

#ownerObject

The owner of the named repository.



20
21
22
# File 'lib/right_hook/subscriber.rb', line 20

def owner
  @owner
end

#urlObject

The full target URL for the binding when used with #subscribe_direct.



13
14
15
# File 'lib/right_hook/subscriber.rb', line 13

def url
  @url
end

#user_agentObject

The user agent value to send on the request to github See: developer.github.com/v3/#user-agent-required



28
29
30
# File 'lib/right_hook/subscriber.rb', line 28

def user_agent
  @user_agent
end

Instance Method Details

#subscribe(opts) ⇒ bool success

Subscribe an instance of App hosted at base_url to a hook for owner/repo_name, authenticating with oauth_token. repo_name and secret are required options and they are intentionally not stored as defaults on the Subscriber instance.

Parameters:

  • opts (Hash)

    Subscription options. Defaults to attr_reader methods when such methods exist.

Options Hash (opts):

  • :owner (String)

    The owner of the repository

  • :repo_name (String)

    The name of the repository

  • :event_type (String)

    A constant under RightHook::Event representing an event type

  • :base_url (String)

    The URL of where the App is hosted

  • :secret (String)

    The secret to use to validate that a request came from GitHub. May be omitted

  • :oauth_token (String)

    The OAuth token to use to authenticate with GitHub when subscribing

  • :user_agent (String)

    The value to send for the User-Agent header when talking with GitHub

Returns:

  • (bool success)

    Whether the request was successful.



59
60
61
# File 'lib/right_hook/subscriber.rb', line 59

def subscribe(opts)
  hub_request_with_mode('subscribe', opts)
end

#subscribe_direct(opts) ⇒ bool success

Subscribe directly to a fixed URL, rather than a calculated URL for an instance of App.

Parameters:

  • opts (Hash)

    Subscription options. Defaults to attr_reader methods when such methods exist.

Options Hash (opts):

  • :owner (String)

    The owner of the repository

  • :repo_name (String)

    The name of the repository

  • :event_type (String)

    A constant under RightHook::Event representing an event type

  • :url (String)

    The URL to receive requests from GitHub when a hook is activated

  • :secret (String)

    The secret to use to validate that a request came from GitHub. May be omitted

  • :oauth_token (String)

    The OAuth token to use to authenticate with GitHub when subscribing

  • :user_agent (String)

    The value to send for the User-Agent header when talking with GitHub

Returns:

  • (bool success)

    Whether the request was successful.



89
90
91
# File 'lib/right_hook/subscriber.rb', line 89

def subscribe_direct(opts)
  direct_hub_request_with_mode('subscribe', opts)
end

#unsubscribe(opts) ⇒ bool success

Unsubscribe an instance of App hosted at base_url to a hook for owner/repo_name, authenticating with oauth_token. repo_name and secret are required options and they are intentionally not stored as defaults on the Subscriber instance. (NB: It’s possible that GitHub’s API *doesn’t* require secret; I haven’t checked.)

Parameters:

  • opts (Hash)

    Subscription options. Defaults to attr_reader methods when such methods exist.

Options Hash (opts):

  • :owner (String)

    The owner of the repository

  • :repo_name (String)

    The name of the repository

  • :event_type (String)

    A constant under RightHook::Event representing an event type

  • :base_url (String)

    The URL of where the App is hosted

  • :secret (String)

    The secret to use to validate that a request came from GitHub. May be omitted

  • :oauth_token (String)

    The OAuth token to use to authenticate with GitHub when subscribing

  • :user_agent (String)

    The value to send for the User-Agent header when talking with GitHub

Returns:

  • (bool success)

    Whether the request was successful.



75
76
77
# File 'lib/right_hook/subscriber.rb', line 75

def unsubscribe(opts)
  hub_request_with_mode('unsubscribe', opts)
end

#unsubscribe_direct(opts) ⇒ bool success

Unsubscribe directly to a fixed URL, rather than a calculated URL for an instance of App.

Parameters:

  • opts (Hash)

    Subscription options. Defaults to attr_reader methods when such methods exist.

Options Hash (opts):

  • :owner (String)

    The owner of the repository

  • :repo_name (String)

    The name of the repository

  • :event_type (String)

    A constant under RightHook::Event representing an event type

  • :url (String)

    The URL to receive requests from GitHub when a hook is activated

  • :secret (String)

    The secret to use to validate that a request came from GitHub. May be omitted

  • :oauth_token (String)

    The OAuth token to use to authenticate with GitHub when subscribing

  • :user_agent (String)

    The value to send for the User-Agent header when talking with GitHub

Returns:

  • (bool success)

    Whether the request was successful.



103
104
105
# File 'lib/right_hook/subscriber.rb', line 103

def unsubscribe_direct(opts)
  direct_hub_request_with_mode('unsubscribe', opts)
end