Permalink
Browse files

Utilising already existing libraries

  • Loading branch information...
1 parent 515955e commit f6dfb121a6cc5d8c8cc0ce0fcd8976c640c7fcc3 @lloydwatkin lloydwatkin committed Aug 3, 2012
Showing with 35 additions and 25 deletions.
  1. +0 −1 Gemfile
  2. +1 −1 docs/buddycloud
  3. +34 −23 services/buddycloud.rb
  4. BIN vendor/cache/rest-client-1.6.7.gem
View
@@ -23,7 +23,6 @@ gem "SystemTimer", :platforms => :ruby_18
gem "statsd-ruby", "0.3.0.github.1"
gem "twilio-ruby", "3.4.2"
gem "rack-test", :require => false
-gem "rest-client", "~> 1.6.7"
gem "right_aws", "3.0.3"
gem "right_http_connection", "1.3.0"
gem "mqtt", "0.0.8"
View
@@ -13,4 +13,4 @@ Install Notes
2. Username -- username for the posting account
3. Password -- password for the posting account
4. Channel -- the channel to which to post
-
+ 5. Choose whether to show a summary of commit messages, detailed commit information, or both
View
@@ -9,7 +9,8 @@ class Service::Buddycloud < Service
string :buddycloud_base_api, :username, :password, :channel
password :password
boolean :show_commits, :show_files
- white_list :buddycloud_base_api, :username, :password, :channel
+ white_list :buddycloud_base_api, :username, :channel, :show_commits, :show_files
+
def receive_push
check_config data
@@ -37,24 +38,18 @@ def check_config(data)
end
def make_request(entry, node)
- headers = { :accept => 'application/xml+atom', :content_type => :xml }
- headers['X-Session-Id'] = @session if defined? @session
-
- request = RestClient::Request.new(
- :method => :post,
- :url => @url + node,
- :user => @username,
- :password => @password,
- :headers => headers,
- :payload => entry
- )
- begin
- response = request.execute
- @session = response.headers['X-Session-Id'] if defined? response.headers['X-Session-Id']
- response.code
- rescue
- raise "buddycloud channel not responding as expected, post not made"
- return 500
+
+ http.basic_auth @username, @password
+ http.headers['Content-Type'] = 'application/xml'
+ http.headers['X-Session-Id'] = @session if defined? @session
+ http.ssl[:verify] = false
+ response = http_post @url + node, entry
+
+ @session = response.headers['X-Session-Id'] if defined? response.headers['X-Session-Id']
+ case response.status
+ when 403, 401, 422 then raise_config_error("Permission denied")
+ when 404, 301 then raise_config_error("Invalid base url or channel name")
+ when 200, 201 then return response.status
end
end
@@ -73,9 +68,9 @@ def generate_message(payload)
now = Time.now
message = <<-EOM
A push has been made to repository "#{payload['repository']['name']}"
- #{payload['repository']['url']}
+#{payload['repository']['url']}
-Changes: #{payload['compare']}
+Changes: #{shorten_url(payload['compare'])}
Made by: #{payload['pusher']['name']} (https://github.com/#{payload['pusher']['name']})
When: #{now.strftime(@time_format)}
@@ -101,9 +96,11 @@ def generate_message(payload)
end
def commit_summary_message(c, i)
+ at = Time.parse(c['timestamp'])
description = c['message'][0 .. 60]
commit_message = <<-EOC
(#{i}) "#{description}" - #{c['author']['name']}
+ #{shorten_url(c['url'])} - #{at.strftime(@time_format)} - #{at.strftime(@time_format)}
EOC
end
@@ -114,8 +111,7 @@ def commit_detail_message(c, i)
modified = c['modified'].join(' ')
commit_message = <<-EOC
(#{i}) #{c['author']['name']} <#{c['author']['email']}>
-#{at.strftime(@time_format)}
-#{c['url']}
+#{shorten_url(c['url'])} - #{at.strftime(@time_format)}
Files added: #{added}
Files removed: #{removed}
Files modified: #{modified}
@@ -125,4 +121,19 @@ def commit_detail_message(c, i)
EOC
end
+
+ def shorten_url(url)
+ res = http_post "https://www.googleapis.com/urlshortener/v1/url" do |req|
+ req.headers['Content-Type'] = 'application/json'
+ req.body = {"longUrl" => url}.to_json
+ end
+ if res.status != 200 && res.status != 201
+ url
+ else
+ url = JSON.parse(res.body)['id']
+ end
+ rescue TimeoutError
+ url
+ end
+
end
Binary file not shown.

0 comments on commit f6dfb12

Please sign in to comment.