Skip to content
This repository has been archived by the owner on Apr 11, 2022. It is now read-only.

Commit

Permalink
moved everything into a module
Browse files Browse the repository at this point in the history
  • Loading branch information
langalex committed Dec 21, 2009
1 parent b864910 commit 030fa58
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 124 deletions.
48 changes: 25 additions & 23 deletions lib/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
require 'yaml'

class Configuration
def initialize(config_file = nil)
@config_file = config_file || ENV['HOME'] + '/.git2mite.yml'
if File.exist?(@config_file)
@config = load_config
else
@config = {}
module Git2Mite
class Configuration
def initialize(config_file = nil)
@config_file = config_file || ENV['HOME'] + '/.git2mite.yml'
if File.exist?(@config_file)
@config = load_config
else
@config = {}
end
end
end

def api_key
@config[:api_key]
end
def api_key
@config[:api_key]
end

def api_key=(value)
@config[:api_key] = value
store_config
end
def api_key=(value)
@config[:api_key] = value
store_config
end

private
private

def store_config
File.open(@config_file, 'w') do |f|
f << @config.to_yaml
def store_config
File.open(@config_file, 'w') do |f|
f << @config.to_yaml
end
end
end

def load_config
YAML.load(File.read(@config_file))
def load_config
YAML.load(File.read(@config_file))
end
end
end
end
12 changes: 6 additions & 6 deletions lib/git2mite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# * check if api key is valid
# * run as post commit hook?

$LOAD_PATH << File.dirname(__FILE__) + '/../lib'
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
require 'configuration'
require 'user'
require 'mite_client'
Expand All @@ -11,7 +11,7 @@


def configuration
@configuration ||= Configuration.new
@configuration ||= Git2Mite::Configuration.new
end

def get_api_key
Expand All @@ -26,15 +26,15 @@ def check_ruby_version!(gui)
gui.error "Sorry you need Ruby 1.9 for this." if RUBY_VERSION < '1.9.1'
end

gui = Gui.new
repo = GitRepo.new
gui = Git2Mite::Gui.new
repo = Git2Mite::GitRepo.new

gui.print_welcome
check_if_git_repo!(repo, gui)
check_ruby_version!(gui)
client = MiteClient.new('http://upstream.mite.yo.lk', get_api_key)
client = Git2Mite::MiteClient.new('http://upstream.mite.yo.lk', get_api_key)
project_id = gui.get_project_id(client.projects)
user_id = gui.get_user_id(User.all(client))
user_id = gui.get_user_id(Git2Mite::User.all(client))
start_date = gui.get_date('start date')
end_date = gui.get_date('end date')
commits = repo.commits start_date, end_date
Expand Down
28 changes: 15 additions & 13 deletions lib/git_repo.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
class GitRepo
def is_git_repo?
status = `git status 2>&1`
!status.downcase.include?('not a git repo')
end
module Git2Mite
class GitRepo
def is_git_repo?
status = `git status 2>&1`
!status.downcase.include?('not a git repo')
end

def commits(start_date, end_date)
lines = []
IO.popen("git log --pretty=format:%ai\\|%s\\|%ae --no-merges --before=#{end_date + 1} --after=#{start_date}") do |io|
while line = io.gets
date, message, author = line.split('|')
lines.unshift [Date.parse(date), message.strip, author]
def commits(start_date, end_date)
lines = []
IO.popen("git log --pretty=format:%ai\\|%s\\|%ae --no-merges --before=#{end_date + 1} --after=#{start_date}") do |io|
while line = io.gets
date, message, author = line.split('|')
lines.unshift [Date.parse(date), message.strip, author]
end
end
lines
end
lines
end

end
end
90 changes: 46 additions & 44 deletions lib/gui.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
class Gui
def print_welcome
puts <<-WELCOME
Welcome to git2mite
module Git2Mite
class Gui
def print_welcome
puts <<-WELCOME
Welcome to git2mite
This tool allows to you to write the commit log of
your git repository to your mite account in order
to auto-fill your timesheets.
This tool allows to you to write the commit log of
your git repository to your mite account in order
to auto-fill your timesheets.
Brought to you by http://upstream-berlin.com
Question, Problems, Source Code: http://github.com/upstream/git2mite
Brought to you by http://upstream-berlin.com
Question, Problems, Source Code: http://github.com/upstream/git2mite
WELCOME
end
WELCOME
end

def ask(question)
print "#{question}: "
gets
end
def ask(question)
print "#{question}: "
gets
end

def error(reason)
STDERR.puts reason
exit(-1)
end
def error(reason)
STDERR.puts reason
exit(-1)
end

def get_project_id(projects)
puts "=== Projects ==="
projects.each.with_index do |project, i|
puts "#{i+1}\t#{project['project']['name']}"
def get_project_id(projects)
puts "=== Projects ==="
projects.each.with_index do |project, i|
puts "#{i+1}\t#{project['project']['name']}"
end
choice = ask('Which project do you want to write your commits to?')
(projects[choice.to_i - 1] || error('invalid project id'))['project']['id']
end
choice = ask('Which project do you want to write your commits to?')
(projects[choice.to_i - 1] || error('invalid project id'))['project']['id']
end

def get_user_id(users)
puts "\n=== Users ==="
users.each.with_index do |user, i|
puts "#{i+1}\t#{user.name}"
def get_user_id(users)
puts "\n=== Users ==="
users.each.with_index do |user, i|
puts "#{i+1}\t#{user.name}"
end
choice = ask('Which user do you want to write your commits to?')
(users[choice.to_i - 1] || error('invalid user id')).id
end
choice = ask('Which user do you want to write your commits to?')
(users[choice.to_i - 1] || error('invalid user id')).id
end

def get_author(authors)
puts "\n=== Git Authors ==="
authors.each.with_index do |name, i|
puts "#{i+1}\t#{name}"
def get_author(authors)
puts "\n=== Git Authors ==="
authors.each.with_index do |name, i|
puts "#{i+1}\t#{name}"
end
choice = ask('Which author\'s commits to you want to use?')
authors[choice.to_i - 1] || error('Invalid author')
end
choice = ask('Which author\'s commits to you want to use?')
authors[choice.to_i - 1] || error('Invalid author')
end

def get_date(label)
answer = ask("Enter the #{label} (yyyy-mm-dd):")
Date.parse(answer) rescue error('invalid date')
end
def get_date(label)
answer = ask("Enter the #{label} (yyyy-mm-dd):")
Date.parse(answer) rescue error('invalid date')
end

end
end
50 changes: 26 additions & 24 deletions lib/mite_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,38 @@
gem 'builder'
require 'builder'

class MiteClient
module Git2Mite
class MiteClient

def initialize(url, api_key)
@url = url
@api_key = api_key
end
def initialize(url, api_key)
@url = url
@api_key = api_key
end

def time_entries(project_id, user_id, date)
get("/time_entries.json?project-id=#{project_id}&user-id=#{user_id}&at=#{date.to_s}")
end
def time_entries(project_id, user_id, date)
get("/time_entries.json?project-id=#{project_id}&user-id=#{user_id}&at=#{date.to_s}")
end

def projects
get '/projects.json'
end
def projects
get '/projects.json'
end

def add_message_to_entry(entry, message)
builder = Builder::XmlMarkup.new
builder.tag!('time-entry') do |time_entry|
time_entry.note((entry['note'].size == 0 ? '' : entry['note'] + ', ') + message)
def add_message_to_entry(entry, message)
builder = Builder::XmlMarkup.new
builder.tag!('time-entry') do |time_entry|
time_entry.note((entry['note'].size == 0 ? '' : entry['note'] + ', ') + message)
end
put "/time_entries/#{entry['id']}.xml", builder.target!
end
put "/time_entries/#{entry['id']}.xml", builder.target!
end

def get(path)
JSON.parse(RestClient.get(@url + path, {'X-MiteApiKey' => @api_key, 'Content-Type' => 'application/json'}))
end
def get(path)
JSON.parse(RestClient.get(@url + path, {'X-MiteApiKey' => @api_key, 'Content-Type' => 'application/json'}))
end

private
private

def put(path, xml)
RestClient.put(@url + path, xml, {'X-MiteApiKey' => @api_key, 'Content-Type' => 'application/xml'})
def put(path, xml)
RestClient.put(@url + path, xml, {'X-MiteApiKey' => @api_key, 'Content-Type' => 'application/xml'})
end
end
end
end
22 changes: 12 additions & 10 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
class User
attr_accessor :name, :id
module Git2Mite
class User
attr_accessor :name, :id

def initialize(attributes = {})
self.name = attributes[:name]
self.id = attributes[:id]
end
def initialize(attributes = {})
self.name = attributes[:name]
self.id = attributes[:id]
end

def self.all(mite_client)
mite_client.get('/users.json').map do |json|
User.new :name => json['user']['name'], :id => json['user']['id']
def self.all(mite_client)
mite_client.get('/users.json').map do |json|
User.new :name => json['user']['name'], :id => json['user']['id']
end
end
end
end
end
8 changes: 4 additions & 4 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/spec_helper'
require 'configuration'

describe Configuration do
describe Git2Mite::Configuration do
before(:each) do
@path = 'test_config.yml'
File.unlink(@path) if File.exist?(@path)
Expand All @@ -12,13 +12,13 @@
end

it "should persist a value" do
config = Configuration.new(@path)
config = Git2Mite::Configuration.new(@path)
config.api_key = '123'
Configuration.new(@path).api_key.should == '123'
Git2Mite::Configuration.new(@path).api_key.should == '123'
end

it "should return the value on setting it" do
returned = Configuration.new(@path).api_key = '234'
returned = Git2Mite::Configuration.new(@path).api_key = '234'
returned.should == '234'
end
end

0 comments on commit 030fa58

Please sign in to comment.