Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Commit

Permalink
port freckle service
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Jun 8, 2011
1 parent 39db84b commit e9408ff
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 137 deletions.
40 changes: 21 additions & 19 deletions services/freckle.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
service :freckle do |data, payload|
class Service::Freckle < Service
self.hook_name = :freckle

entries, subdomain, token, project =
[], data['subdomain'].strip, data['token'].strip, data['project'].strip
def receive_push
entries, subdomain, token, project =
[], data['subdomain'].strip, data['token'].strip, data['project'].strip

payload['commits'].each do |commit|
minutes = (commit["message"].split(/\s/).find { |item| /^f:/ =~ item } || '')[2,100]
next unless minutes
entries << {
:date => commit["timestamp"],
:minutes => minutes,
:description => commit["message"].gsub(/(\s|^)f:.*(\s|$)/, '').strip,
:url => commit['url'],
:project_name => project,
:user => commit['author']['email']
}
payload['commits'].each do |commit|
minutes = (commit["message"].split(/\s/).find { |item| /^f:/ =~ item } || '')[2,100]
next unless minutes
entries << {
:date => commit["timestamp"],
:minutes => minutes,
:description => commit["message"].gsub(/(\s|^)f:.*(\s|$)/, '').strip,
:url => commit['url'],
:project_name => project,
:user => commit['author']['email']
}
end

http.headers['Content-Type'] = 'application/json'
http_post "http://#{data['subdomain']}.letsfreckle.com/api/entries/import",
{:entries => entries, :token => data['token']}.to_json
end
uri = URI.parse("http://#{data['subdomain']}.letsfreckle.com/api/entries/import")
req = Net::HTTP::Post.new(uri.path)
req.set_content_type('application/json')
req.body = { :entries => entries, :token => data['token'] }.to_json
Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }
end
118 changes: 0 additions & 118 deletions spec/freckle_spec.rb

This file was deleted.

79 changes: 79 additions & 0 deletions test/freckle_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require File.expand_path('../helper', __FILE__)

class FreckleTest < Service::TestCase
def setup
@stubs = Faraday::Adapter::Test::Stubs.new
end

def test_posts_with_2_entries
data = call_service :push
assert_equal 2, data['entries'].size
end

def test_includes_auth_token
data = call_service :push
assert_equal '12345', data['token']
end

def test_parses_minutes_from_commit_message
data = call_service :push
assert_equal '15', data['entries'][0]['minutes']
assert_equal '2hrs', data['entries'][1]['minutes']
end

def test_strips_freckle_tags
data = call_service :push
assert_equal 'stub git call for Grit#heads test',
data['entries'][0]['description']
assert_equal 'clean up heads test',
data['entries'][1]['description']
end

def test_includes_project_name
data = call_service :push
assert_equal 'Test Project',
data['entries'][0]['project_name']
end

def test_includes_author_email_as_user
data = call_service :push
assert_equal 'tom@mojombo.com',
data['entries'][0]['user']
end

def test_includes_commit_url
data = call_service :push
assert_equal 'http://github.com/mojombo/grit/commit/06f63b43050935962f84fe54473a7c5de7977325',
data['entries'][0]['url']
end

def test_includes_timestamp_as_date
data = call_service :push
assert_equal '2007-10-10T00:11:02-07:00',
data['entries'][0]['date']
end

def data
{
"subdomain" => "abloom",
"token" => "12345",
"project" => "Test Project"
}
end

def service(*args)
super Service::Freckle, *args
end

def call_service(event)
res = nil
svc = service event, data, payload
@stubs.post '/api/entries/import' do |env|
res = JSON.parse env[:body]
end
svc.send "receive_#{event}"
res
end
end


0 comments on commit e9408ff

Please sign in to comment.