Class: Todone::MessageProcessor

Inherits:
Object
  • Object
show all
Includes:
Consts, Views
Defined in:
lib/todone.rb

Constant Summary

Constants included from Consts

Consts::CONFIG, Consts::CONFIG_DIR, Consts::CONFIG_FILE, Consts::HOOK_FILE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Views

#api_problem, #exists_prepare_commit_msg_hook, #missing_hooks_dir, #missing_project, #missing_project_id, #missing_view, #missing_write_file, #show_pivotal_stories, #updated_prepare_commit_msg_hook

Constructor Details

#initialize(opts = {}) ⇒ MessageProcessor

Returns a new instance of MessageProcessor.



57
58
59
60
61
# File 'lib/todone.rb', line 57

def initialize opts = {}
	self.config_dir = opts[:config_dir]
	project_id = opts[:project_id] || Todone::MessageProcessor.load_project_id
	@pp = Todone::PivotalPuller.new(:project_id => project_id, :tracker_token => config.api_key) if project_id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/todone.rb', line 122

def method_missing(method_id, *args)
	super unless match = /view_(.*)/.match(method_id.to_s) and Todone::MessageProcessor.method_defined?(match[1])
	method = match[1]
	view, data = self.send(method, *args)
	return self.send('missing_view',:method=> method) unless Todone::Views.method_defined? view
	
	self.send(view, data)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/todone.rb', line 7

def config
  @config
end

#config_dirObject



94
95
96
# File 'lib/todone.rb', line 94

def config_dir
	(@config_dir.nil? && Todone::Consts::CONFIG_DIR) || @config_dir
end

Class Method Details

.commit_msg_fileObject



43
44
45
# File 'lib/todone.rb', line 43

def commit_msg_file
	File.join(git_dir,'COMMIT_EDITMSG')
end

.git_configObject



53
# File 'lib/todone.rb', line 53

def git_config; File.join(git_dir,'config') end

.git_dirObject



47
48
49
50
51
# File 'lib/todone.rb', line 47

def git_dir
	if Dir.getwd.split('/').last == 'hooks' then '..'
	elsif File.exists? '.git'               then '.git'
	end
end

.init(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/todone.rb', line 17

def init options
	return if options[:owner].nil? or options[:api_key].nil?
	dir = options.delete(:dir) || Todone::Consts::CONFIG_DIR
	conf_file = File.join(dir, Todone::Consts::CONFIG_FILE)
	
	FileUtils.mkdir_p dir unless File.exists? dir
	unless File.exists? conf_file
		File.open(conf_file, 'w') do |f|
			YAML.dump({ 
					:owner    => options[:owner], 
					:api_key  => options[:api_key]
				}, f)
		end
	end

end

.load_project_idObject



34
35
36
37
38
39
40
41
# File 'lib/todone.rb', line 34

def load_project_id
	File.open(Todone::MessageProcessor.git_config) do |file| 
		file.each_line do |line| 
			return file.gets.tr("\s\t\n",'').split('=').last if line.slice('[pivotal]')
		end
	end
	return nil
end

.needs_init?(dir = Todone::Consts::CONFIG_DIR) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/todone.rb', line 13

def needs_init? dir = Todone::Consts::CONFIG_DIR
	not File.exists? File.join(dir, Todone::Consts::CONFIG_FILE)
end

Instance Method Details

#add_hook(project_id) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/todone.rb', line 73

def add_hook project_id
	if File.exists? Todone::Consts::HOOK_FILE
		return ['exists_prepare_commit_msg_hook',{:project_id => project_id}]
	else
		File.open(Todone::Consts::HOOK_FILE,'w') do |f|
			f.write("todone tickets -s -w")
		end
		FileUtils.chmod 0751, Todone::Consts::HOOK_FILE
		return "prepare_commit_msg_hook_updated" 
	end 
end

#add_project(project) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/todone.rb', line 63

def add_project project
	return 'missing_project' if project.nil? or project[:id].nil? or project[:users].nil?
	save_project_id project[:id]
	config[project[:id]] = project[:users].split(',')
	config.save config_dir
	return ['missing_hooks_dir', {:project => project}] unless File.exists? File.join('.git','hooks')

	return self.add_hook project[:id]	
end

#save_project_id(project_id) ⇒ Object



85
86
87
# File 'lib/todone.rb', line 85

def save_project_id project_id
	`git config -f .git/config pivotal.project-id #{project_id}`
end

#ticketsObject



98
99
100
101
102
103
104
105
106
# File 'lib/todone.rb', line 98

def tickets
	return ["missing_project_id"] if @pp.nil?
	api_data = @pp.pull_stories("started")
	if api_data.class == Hash
		api_data.delete("error")
	else
		["show_pivotal_stories", { :stories => api_data } ]
	end
end

#write_tickets(opts = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/todone.rb', line 108

def write_tickets opts = {} 
	file = opts[:file] || Todone::MessageProcessor.commit_msg_file
	if File.exists? file
		File.open(file, 'r+') do |f| 
			original_message = f.read
			f.pos = 0
			f.write( view_tickets + original_message  )
		end
	else
		puts missing_write_file( :file => file )
	end
end