Skip to content

Commit

Permalink
Merge pull request #1 from zrob/add_user_agent
Browse files Browse the repository at this point in the history
Add user agent to github api calls
  • Loading branch information
mark-rushakoff committed Dec 3, 2013
2 parents ee977fc + d03c969 commit f2165dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
16 changes: 14 additions & 2 deletions lib/right_hook/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,26 @@ class Subscriber
# See http://developer.github.com/v3/repos/hooks/ for a complete list of valid types.
attr_accessor :event_type

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

# 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+, and +event_type+].
# The valid keys in the options are [+base_url+, +oauth_token+, +owner+, +event_type+, and +user_agent+].
# @param [Hash] opts Subscription options. Defaults to attr_reader methods when such methods exist.
# @option opts [String] :owner The owner of the repository
# @option opts [String] :event_type A constant under RightHook::Event representing an event type
# @option opts [String] :base_url The URL of where the {RightHook::App} is hosted
# @option opts [String] :url The URL to receive requests from GitHub when a hook is activated
# @option opts [String] :oauth_token The OAuth token to use to authenticate with GitHub when subscribing
# @option opts [String] :user_agent The value to send for the User-Agent header when talking with GitHub
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

# Subscribe an instance of {RightHook::App} hosted at +base_url+ to a hook for +owner+/+repo_name+, authenticating with +oauth_token+.
Expand All @@ -49,6 +55,7 @@ def initialize(default_opts = {})
# @option opts [String] :base_url The URL of where the {RightHook::App} is hosted
# @option opts [String] :secret The secret to use to validate that a request came from GitHub. May be omitted
# @option opts [String] :oauth_token The OAuth token to use to authenticate with GitHub when subscribing
# @option opts [String] :user_agent The value to send for the User-Agent header when talking with GitHub
def subscribe(opts)
hub_request_with_mode('subscribe', opts)
end
Expand All @@ -64,6 +71,7 @@ def subscribe(opts)
# @option opts [String] :base_url The URL of where the {RightHook::App} is hosted
# @option opts [String] :secret The secret to use to validate that a request came from GitHub. May be omitted
# @option opts [String] :oauth_token The OAuth token to use to authenticate with GitHub when subscribing
# @option opts [String] :user_agent The value to send for the User-Agent header when talking with GitHub
def unsubscribe(opts)
hub_request_with_mode('unsubscribe', opts)
end
Expand All @@ -77,6 +85,7 @@ def unsubscribe(opts)
# @option opts [String] :url The URL to receive requests from GitHub when a hook is activated
# @option opts [String] :secret The secret to use to validate that a request came from GitHub. May be omitted
# @option opts [String] :oauth_token The OAuth token to use to authenticate with GitHub when subscribing
# @option opts [String] :user_agent The value to send for the User-Agent header when talking with GitHub
def subscribe_direct(opts)
direct_hub_request_with_mode('subscribe', opts)
end
Expand All @@ -90,6 +99,7 @@ def subscribe_direct(opts)
# @option opts [String] :url The URL to receive requests from GitHub when a hook is activated
# @option opts [String] :secret The secret to use to validate that a request came from GitHub. May be omitted
# @option opts [String] :oauth_token The OAuth token to use to authenticate with GitHub when subscribing
# @option opts [String] :user_agent The value to send for the User-Agent header when talking with GitHub
def unsubscribe_direct(opts)
direct_hub_request_with_mode('unsubscribe', opts)
end
Expand All @@ -112,11 +122,13 @@ def direct_hub_request_with_mode(mode, opts)
owner = opts.fetch(:owner) { self.owner }
oauth_token = opts.fetch(:oauth_token) { self.oauth_token }
event_type = opts.fetch(:event_type) { self.event_type }
user_agent = opts.fetch(:user_agent) { self.user_agent }

response = HTTParty.post('https://api.github.com/hub',
headers: {
# http://developer.github.com/v3/#authentication
'Authorization' => "token #{oauth_token}"
'Authorization' => "token #{oauth_token}",
'User-Agent' => user_agent.to_s
},
body: {
'hub.mode' => mode,
Expand Down
14 changes: 9 additions & 5 deletions spec/subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
owner: 'mark-rushakoff',
base_url: 'http://example.com',
event_type: RightHook::Event::ISSUE,
user_agent: 'My-User-Agent',
)
end

describe '.subscribe' do
let!(:stubbed_request) do
stub_request(:post, 'https://api.github.com/hub').
with(:body => 'hub.mode=subscribe&hub.topic=https%3A%2F%2Fgithub.com%2Fmark-rushakoff%2Fright_hook%2Fevents%2Fissues&hub.callback=http%3A%2F%2Fexample.com%2Fhook%2Fmark-rushakoff%2Fright_hook%2Fissue&hub.secret=the-secret',
:headers => {'Authorization' => 'token my_token'}
:headers => {'Authorization' => 'token my_token', 'User-Agent' => 'My-User-Agent'}
).to_return(:status => status_code, :body => '', :headers => {})
end

Expand Down Expand Up @@ -50,7 +51,8 @@
oauth_token: 'my_token',
owner: 'mark-rushakoff',
base_url: 'http://example.com',
event_type: RightHook::Event::ISSUE
event_type: RightHook::Event::ISSUE,
user_agent: 'My-User-Agent',
)
}.not_to raise_error

Expand All @@ -63,7 +65,7 @@
let!(:stubbed_request) do
stub_request(:post, 'https://api.github.com/hub').
with(:body => 'hub.mode=subscribe&hub.topic=https%3A%2F%2Fgithub.com%2Fmark-rushakoff%2Fright_hook%2Fevents%2Fissues&hub.callback=http%3A%2F%2Fhook.example.com&hub.secret=the-secret',
:headers => {'Authorization' => 'token my_token'}
:headers => {'Authorization' => 'token my_token', 'User-Agent' => 'My-User-Agent'}
).to_return(:status => status_code, :body => '', :headers => {})
end

Expand Down Expand Up @@ -100,6 +102,7 @@
url: 'http://hook.example.com',
secret: 'the-secret',
oauth_token: 'my_token',
user_agent: 'My-User-Agent',
)
}.not_to raise_error

Expand All @@ -112,7 +115,7 @@
let!(:stubbed_request) do
stub_request(:post, 'https://api.github.com/hub').
with(:body => 'hub.mode=unsubscribe&hub.topic=https%3A%2F%2Fgithub.com%2Fmark-rushakoff%2Fright_hook%2Fevents%2Fissues&hub.callback=http%3A%2F%2Fexample.com%2Fhook%2Fmark-rushakoff%2Fright_hook%2Fissue&hub.secret=the-secret',
:headers => {'Authorization' => 'token my_token'}
:headers => {'Authorization' => 'token my_token', 'User-Agent' => 'My-User-Agent'}
).to_return(:status => status_code, :body => '', :headers => {})
end

Expand Down Expand Up @@ -144,7 +147,7 @@
let!(:stubbed_request) do
stub_request(:post, 'https://api.github.com/hub').
with(:body => 'hub.mode=unsubscribe&hub.topic=https%3A%2F%2Fgithub.com%2Fmark-rushakoff%2Fright_hook%2Fevents%2Fissues&hub.callback=http%3A%2F%2Fhook.example.com&hub.secret=the-secret',
:headers => {'Authorization' => 'token my_token'}
:headers => {'Authorization' => 'token my_token', 'User-Agent' => 'My-User-Agent'}
).to_return(:status => status_code, :body => '', :headers => {})
end

Expand Down Expand Up @@ -181,6 +184,7 @@
url: 'http://hook.example.com',
secret: 'the-secret',
oauth_token: 'my_token',
user_agent: 'My-User-Agent',
)
}.not_to raise_error

Expand Down

0 comments on commit f2165dc

Please sign in to comment.