Skip to content
Browse files

Added first shot at Trello service

  • Loading branch information...
1 parent 7f43232 commit 8bd6aa82a508457a3f7833c5875915aac978ccf3 @snikch snikch committed May 7, 2012
Showing with 115 additions and 0 deletions.
  1. +74 −0 services/trello.rb
  2. +41 −0 test/trello_test.rb
View
74 services/trello.rb
@@ -0,0 +1,74 @@
+class Service::Trello < Service
+ string :consumer_token, :list_id
+
+ def receive_push
+ return if payload['commits'].size == 0
+
+ # Confirm all required config is present
+ assert_required_credentials
+
+ # Create the card
+ create_card
+ end
+
+ private
+
+ def create_card
+ http.url_prefix = "https://api.trello.com/1"
+
+ payload['commits'].each do |commit|
+ http_post "cards",
+ :name => name_for_commit(commit),
+ :desc => desc_for_commit(commit),
+ :idList => list_id,
+ :key => application_key,
+ :token => consumer_token
+ end
+ end
+
+ def name_for_commit commit
+ commit['message'].length > message_max_length ? \
+ commit['message'][0...message_max_length] + '...' : \
+ commit['message']
+ end
+
+ def desc_for_commit commit
+ author = commit['author'] || {}
+
+ "Author: %s\n\n%s\n\nRepo: %s\n\nCommit Message: %s" % [
+ author['name'] || '[unknown]',
+ commit['url'],
+ repository,
+ commit['message'] || '[no description]'
+ ]
+ end
+
+ def consumer_token
+ data['consumer_token'].to_s
+ end
+
+ def list_id
+ data['list_id'].to_s
+ end
+
+ def application_key
+ "74666606852cabea363165a7cd5b7dc9"
+ end
+
+ def repository
+ payload['repository']['name']
+ end
+
+ def message_max_length
+ 80
+ end
+
+ def assert_required_credentials
+ if consumer_token.empty?
+ raise_config_error "You need an authorization Token. See tips below."
+ end
+ if list_id.empty?
+ raise_config_error "You need to enter a list identifiter. See tips below."
+ end
+ end
+end
View
41 test/trello_test.rb
@@ -0,0 +1,41 @@
+class TrelloTest < Service::TestCase
+ def setup
+ @stubs = Faraday::Adapter::Test::Stubs.new
+ end
+
+ def test_push
+ svc = service :push,
+ {'list_id' => 'abc123', 'consumer_token' => 'blarg'}, payload
+
+ def svc.message_max_length; 4 end
+
+ @stubs.post "/1/cards" do |env|
+ assert_equal 'api.trello.com', env[:url].host
+ assert_match 'token=blarg', env[:body]
+ assert_match 'idList=abc123', env[:body]
+ [200, {}, '']
+ end
+
+ assert_equal 'stub...', svc.send(:name_for_commit, svc.payload['commits'].first)
+
+ assert_equal correct_description, svc.send(:desc_for_commit, svc.payload['commits'].first)
+
+ svc.receive_push
+ end
+
+ private
+
+ def correct_description
+ 'Author: Tom Preston-Werner
+
+http://github.com/mojombo/grit/commit/06f63b43050935962f84fe54473a7c5de7977325
+
+Repo: grit
+
+Commit Message: stub git call for Grit#heads test f:15 Case#1'
+ end
+
+ def service(*args)
+ super Service::Trello, *args
+ end
+end

0 comments on commit 8bd6aa8

Please sign in to comment.
Something went wrong with that request. Please try again.