Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yammer plugin #160

Merged
merged 20 commits into from
Apr 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/lolcommits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
require 'lolcommits/plugins/lol_twitter'
require 'lolcommits/plugins/uploldz'
require 'lolcommits/plugins/lolsrv'
require 'lolcommits/plugins/lol_yammer'

# require runner after all the plugins have been required
require 'lolcommits/runner'
85 changes: 85 additions & 0 deletions lib/lolcommits/plugins/lol_yammer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- encoding : utf-8 -*-
require 'yammer'
require 'rest_client'

# https://developer.yammer.com/oauth2-quickstart/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these two constants (CLIENT_ID and CLIENT_SECRET) also be asked for (and stored) in the configuration? OR are these CLIENT constants merely to identify the lolcommits yammer plugin client to yammer itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthutchinson

CLIENT_ID and CLIENT_SECRET is for the registered app in Yammer Application directory, so it's merely to identify the lolcommits yammer plugin client to yammer itself.

I'll lookup transferring ownership procedure of the app, if anyone wants to take it :)

Right now, I don't there's much to configure on it, unless someone wants it published in Yammer Application directory (maybe add proper description, screenshots, etc).

YAMMER_CLIENT_ID = 'bgORyeKtnjZJSMwp8oln9g'
YAMMER_CLIENT_SECRET = 'oer2WdGzh74a5QBbW3INUxblHK3yg9KvCZmiBa2r0'
YAMMER_ACCESS_TOKEN_URL = 'https://www.yammer.com/oauth2/access_token.json'
YAMMER_RETRY_COUNT = 2

module Lolcommits
class LolYammer < Plugin
def self.name
'yammer'
end

def configured?
!configuration['access_token'].nil?
end

def configure_access_token
print "Open the URL below and copy the `code` param from query after redirected, enter it as `access_token`:\n"
print "https://www.yammer.com/dialog/oauth?client_id=#{YAMMER_CLIENT_ID}&response_type=code\n"
print 'Enter code param from the redirected URL, then press enter: '
code = STDIN.gets.to_s.strip

url = "#{YAMMER_ACCESS_TOKEN_URL}"
debug "access_token url: #{url}"
params = {
'client_id' => YAMMER_CLIENT_ID,
'client_secret' => YAMMER_CLIENT_SECRET,
'code' => code
}
debug "params : #{params.inspect}"
result = JSON.parse(RestClient.post(url, params))
debug "response : #{result.inspect}"
# no need for 'return', last line is always the return value
{ 'access_token' => result['access_token']['token'] }
end

def configure_options!
options = super
if options['enabled'] == true
auth_config = configure_access_token
if auth_config
options.merge!(auth_config)
else
return
end
end
options
end

def run
return unless valid_configuration?

commit_msg = self.runner.message
post = "#{commit_msg} #lolcommits"
puts "Yammer post: #{post}" unless self.runner.capture_stealth

Yammer.configure do |c|
c.client_id = YAMMER_CLIENT_ID
c.client_secret = YAMMER_CLIENT_SECRET
end

client = Yammer::Client.new(:access_token => configuration['access_token'])

retries = YAMMER_RETRY_COUNT
begin
lolimage = File.new(self.runner.main_image)
response = client.create_message(post, :attachment1 => lolimage)
debug response.body.inspect
if response
puts "\t--> Status posted!" unless self.runner.capture_stealth
end
rescue => e
retries -= 1
retry if retries > 0
puts "Status not posted - #{e.message}"
puts 'Try running config again:'
puts "\tlolcommits --config --plugin yammer"
end
end
end
end
1 change: 1 addition & 0 deletions lib/lolcommits/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Runner
set_callback :run, :after, :execute_lolcommits_lolsrv
set_callback :run, :after, :execute_lolcommits_lol_twitter
set_callback :run, :after, :execute_lolcommits_dot_com
set_callback :run, :after, :execute_lolcommits_lol_yammer
set_callback :run, :after, :execute_lolcommits_loltext
# Executed First

Expand Down
1 change: 1 addition & 0 deletions lolcommits.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Gem::Specification.new do |s|

# plugin dependencies
s.add_runtime_dependency('twitter', '~> 4.8.1') #twitter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can just say #yammer (in the comment below)

s.add_runtime_dependency('yam', '~> 2.0.1') #yammer
s.add_runtime_dependency('oauth') #twitter
s.add_runtime_dependency('rest-client') #uploldz
s.add_runtime_dependency('httmultiparty') #dot_com
Expand Down