diff options
| author | Zack Hobson | 2009-07-19 13:26:55 -0700 |
|---|---|---|
| committer | Zack Hobson | 2009-07-19 13:26:55 -0700 |
| commit | 78f27201877546d998613c7fa2aaa6f174d9c443 (patch) | |
| tree | 17bce3226a669511f711e7d5ae87622c2f7d66f5 /lib | |
| parent | 7867896534da18b77f7304232f8db06ed11d583d (diff) | |
| download | hcl-78f27201877546d998613c7fa2aaa6f174d9c443.tar.bz2 | |
rearranged source files
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/hcl.rb | 4 | ||||
| -rw-r--r-- | lib/hcl/day_entry.rb | 126 | ||||
| -rw-r--r-- | lib/hcl/project.rb | 3 | ||||
| -rw-r--r-- | lib/hcl/task.rb | 42 | ||||
| -rw-r--r-- | lib/hcl/timesheet_resource.rb | 82 |
5 files changed, 132 insertions, 125 deletions
@@ -1,9 +1,13 @@ require 'yaml' +require 'rexml/document' require 'rubygems' require 'curb' require 'chronic' +require 'hcl/timesheet_resource' +require 'hcl/project' +require 'hcl/task' require 'hcl/day_entry' class HCl diff --git a/lib/hcl/day_entry.rb b/lib/hcl/day_entry.rb index d0f1a13..204ff5d 100644 --- a/lib/hcl/day_entry.rb +++ b/lib/hcl/day_entry.rb @@ -1,129 +1,5 @@ -require 'rexml/document' - -class HCl - class TimesheetResource - def self.configure opts = nil - if opts - self.login = opts['login'] - self.password = opts['password'] - self.subdomain = opts['subdomain'] - else - yield self - end - end - - # configuration accessors - %w[ login password subdomain ].each do |config_var| - class_eval <<-EOC - def self.#{config_var}= arg - @@#{config_var} = arg - end - def self.#{config_var} - @@#{config_var} - end - EOC - end - - def initialize params - @data = params - end - - def self.perform action - client = session action - if client.http_get - client.body_str - else - raise "failed" - end - end - - def self.send_data action, data - client = session action - client.multipart_form_post = true - data_field = Curl::PostField.file('data','data') { data } - if client.http_post data_field - client.body_str - else - raise "failed" - end - end - - def self.session action - client = Curl::Easy.new("https://#{subdomain}.harvestapp.com/#{action}") - client.headers['Accept'] = 'application/xml' - client.headers['Content-Type'] = 'application/xml' - client.http_auth_types = Curl::CURLAUTH_BASIC - client.userpwd = "#{login}:#{password}" - client - #client = Patron::Session.new - #client.timeout = 10 - #client.username = login - #client.password = password - #if client.get "https://#{subdomain}.harvestapp.com/#{action}" - end - - def id - @data[:id] - end - - def method_missing method, *args - if @data.key? method.to_sym - @data[method] - else - super - end - end - - def self.xml_to_hash elem - elem.elements.map { |e| e.name }.inject({}) do |a, f| - a[f.to_sym] = elem.elements[f].text if elem.elements[f] - a - end - end - end - - class Project < TimesheetResource;end - - class Task < TimesheetResource - def self.cache_tasks doc - tasks = [] - doc.root.elements.collect('projects/project') do |project_elem| - project = Project.new xml_to_hash(project_elem) - tasks.concat(project_elem.elements.collect('tasks/task') do |task| - new xml_to_hash(task).merge(:project => project) - end) - end - File.open(File.join(ENV['HOME'],'.hcl_tasks'), 'w') do |f| - f.write tasks.uniq.to_yaml - end - end - - def self.all - YAML.load File.read(File.join(ENV['HOME'],'.hcl_tasks')) - end - - def self.find id - all.detect {|t| t.id == id } - end - - def to_s - "#{project.name} #{name}" - end - - def start *args - notes = args.join ' ' - Task.send_data "/daily/add", <<-EOT - <request> - <notes>#{notes}</notes> - <hours></hours> - <project_id type="integer">#{project.id}</project_id> - <task_id type="integer">#{id}</task_id> - <spent_at type="date">#{Date.today}</spent_at> - </request> - EOT - end - end +class HCl class DayEntry < TimesheetResource # Get the time sheet entries for a given day. If no date is provided # defaults to today. diff --git a/lib/hcl/project.rb b/lib/hcl/project.rb new file mode 100644 index 0000000..df9ea0c --- /dev/null +++ b/lib/hcl/project.rb @@ -0,0 +1,3 @@ +class HCl::Project < HCl::TimesheetResource +end + diff --git a/lib/hcl/task.rb b/lib/hcl/task.rb new file mode 100644 index 0000000..761991e --- /dev/null +++ b/lib/hcl/task.rb @@ -0,0 +1,42 @@ +class HCl + class Task < TimesheetResource + def self.cache_tasks doc + tasks = [] + doc.root.elements.collect('projects/project') do |project_elem| + project = Project.new xml_to_hash(project_elem) + tasks.concat(project_elem.elements.collect('tasks/task') do |task| + new xml_to_hash(task).merge(:project => project) + end) + end + File.open(File.join(ENV['HOME'],'.hcl_tasks'), 'w') do |f| + f.write tasks.uniq.to_yaml + end + end + + def self.all + YAML.load File.read(File.join(ENV['HOME'],'.hcl_tasks')) + end + + def self.find id + all.detect {|t| t.id == id } + end + + def to_s + "#{project.name} #{name}" + end + + def start *args + notes = args.join ' ' + Task.send_data "/daily/add", <<-EOT + <request> + <notes>#{notes}</notes> + <hours></hours> + <project_id type="integer">#{project.id}</project_id> + <task_id type="integer">#{id}</task_id> + <spent_at type="date">#{Date.today}</spent_at> + </request> + EOT + end + end +end + diff --git a/lib/hcl/timesheet_resource.rb b/lib/hcl/timesheet_resource.rb new file mode 100644 index 0000000..e13735e --- /dev/null +++ b/lib/hcl/timesheet_resource.rb @@ -0,0 +1,82 @@ +class HCl + class TimesheetResource + def self.configure opts = nil + if opts + self.login = opts['login'] + self.password = opts['password'] + self.subdomain = opts['subdomain'] + else + yield self + end + end + + # configuration accessors + %w[ login password subdomain ].each do |config_var| + class_eval <<-EOC + def self.#{config_var}= arg + @@#{config_var} = arg + end + def self.#{config_var} + @@#{config_var} + end + EOC + end + + def initialize params + @data = params + end + + def self.perform action + client = session action + if client.http_get + client.body_str + else + raise "failed" + end + end + + def self.send_data action, data + client = session action + client.multipart_form_post = true + data_field = Curl::PostField.file('data','data') { data } + if client.http_post data_field + client.body_str + else + raise "failed" + end + end + + def self.session action + client = Curl::Easy.new("https://#{subdomain}.harvestapp.com/#{action}") + client.headers['Accept'] = 'application/xml' + client.headers['Content-Type'] = 'application/xml' + client.http_auth_types = Curl::CURLAUTH_BASIC + client.userpwd = "#{login}:#{password}" + client + #client = Patron::Session.new + #client.timeout = 10 + #client.username = login + #client.password = password + #if client.get "https://#{subdomain}.harvestapp.com/#{action}" + end + + def id + @data[:id] + end + + def method_missing method, *args + if @data.key? method.to_sym + @data[method] + else + super + end + end + + def self.xml_to_hash elem + elem.elements.map { |e| e.name }.inject({}) do |a, f| + a[f.to_sym] = elem.elements[f].text if elem.elements[f] + a + end + end + end +end |
