summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZack Hobson2013-12-22 17:07:49 -0800
committerZack Hobson2013-12-22 17:07:49 -0800
commit0e3581420a697c40ae91c2ad92884cd8e1eebe03 (patch)
treef8228ad368de8e86691c28f7e9e7f3d2c749125c
parent2f6c16ddba55c516b28fc98302c7659bfc06ef3b (diff)
downloadhcl-0e3581420a697c40ae91c2ad92884cd8e1eebe03.tar.bz2
remove xml processing code
-rw-r--r--Gemfile1
-rw-r--r--Gemfile.lock9
-rw-r--r--hcl.gemspec1
-rw-r--r--lib/hcl.rb1
-rw-r--r--lib/hcl/day_entry.rb18
-rw-r--r--lib/hcl/task.rb39
-rw-r--r--lib/hcl/timesheet_resource.rb26
-rw-r--r--lib/hcl/yajl_middleware.rb8
-rw-r--r--test/day_entry_test.rb32
-rw-r--r--test/task_test.rb63
10 files changed, 53 insertions, 145 deletions
diff --git a/Gemfile b/Gemfile
index 432a0f7..b133fde 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,5 +1,6 @@
source "https://rubygems.org"
gemspec
+gem 'pry', group:['test','development']
# XXX this is dumb but it's crazy hard to get platform specfic deps into a gemspec
gem 'rubysl-abbrev', platform:'rbx'
gem 'rubysl-singleton', platform:'rbx'
diff --git a/Gemfile.lock b/Gemfile.lock
index d6f1a8b..db5a77d 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -7,11 +7,13 @@ PATH
faraday_middleware
highline
trollop
+ yajl-ruby
GEM
remote: https://rubygems.org/
specs:
chronic (0.10.2)
+ coderay (1.0.9)
fakeweb (1.3.0)
faraday (0.8.8)
multipart-post (~> 1.2.0)
@@ -19,11 +21,16 @@ GEM
faraday (>= 0.7.4, < 0.9)
highline (1.6.20)
metaclass (0.0.1)
+ method_source (0.8.2)
minitest (4.7.5)
mocha (0.14.0)
metaclass (~> 0.0.1)
multi_json (1.8.2)
multipart-post (1.2.0)
+ pry (0.9.12.2)
+ coderay (~> 1.0.5)
+ method_source (~> 0.8)
+ slop (~> 3.4)
rake (10.1.0)
rubinius-coverage (2.0.3)
rubygems-tasks (0.2.4)
@@ -35,6 +42,7 @@ GEM
multi_json (~> 1.0)
simplecov-html (~> 0.7.1)
simplecov-html (0.7.1)
+ slop (3.4.7)
trollop (2.0)
yajl-ruby (1.1.0)
yard (0.8.7.3)
@@ -48,6 +56,7 @@ DEPENDENCIES
hcl!
minitest
mocha
+ pry
rake
rubinius-coverage
rubygems-tasks
diff --git a/hcl.gemspec b/hcl.gemspec
index d0fa7f8..fd5e526 100644
--- a/hcl.gemspec
+++ b/hcl.gemspec
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'highline'
s.add_runtime_dependency 'faraday'
s.add_runtime_dependency 'faraday_middleware'
+ s.add_runtime_dependency 'yajl-ruby'
s.add_development_dependency 'rake'
s.add_development_dependency 'rubygems-tasks'
s.add_development_dependency 'mocha'
diff --git a/lib/hcl.rb b/lib/hcl.rb
index 8f7be26..f4a085b 100644
--- a/lib/hcl.rb
+++ b/lib/hcl.rb
@@ -7,4 +7,5 @@ module HCl
autoload :Project, 'hcl/project'
autoload :Task, 'hcl/task'
autoload :DayEntry, 'hcl/day_entry'
+ autoload :YajlMiddleware, 'hcl/yajl_middleware'
end
diff --git a/lib/hcl/day_entry.rb b/lib/hcl/day_entry.rb
index 6ef1708..bd14be8 100644
--- a/lib/hcl/day_entry.rb
+++ b/lib/hcl/day_entry.rb
@@ -1,5 +1,3 @@
-require 'rexml/document'
-
module HCl
class DayEntry < TimesheetResource
include Utility
@@ -8,7 +6,9 @@ module HCl
# defaults to today.
def self.all date = nil
url = date.nil? ? 'daily' : "daily/#{date.strftime '%j/%Y'}"
- from_xml get(url)
+ doc = get url
+ Task.cache_tasks_hash doc
+ doc[:day_entries].map {|e| new e}
end
def to_s
@@ -19,15 +19,6 @@ module HCl
@data[:task]
end
- def self.from_xml xml
- doc = REXML::Document.new xml
- raise Failure, "No root node in XML document: #{xml}" if doc.root.nil?
- Task.cache_tasks doc
- doc.root.elements.collect('//day_entry') do |day|
- new xml_to_hash(day)
- end
- end
-
def cancel
begin
DayEntry.delete("daily/delete/#{id}")
@@ -46,8 +37,7 @@ module HCl
# If I don't include hours it gets reset.
# This doens't appear to be the case for task and project.
(self.notes << "\n#{new_notes}").lstrip!
- DayEntry.post "daily/update/#{id}",
- %{<request><notes>#{notes}</notes><hours>#{hours}</hours></request>}
+ DayEntry.post "daily/update/#{id}", notes:notes, hours:hours
end
def self.with_timer date=nil
diff --git a/lib/hcl/task.rb b/lib/hcl/task.rb
index 4bb635c..78efa62 100644
--- a/lib/hcl/task.rb
+++ b/lib/hcl/task.rb
@@ -4,8 +4,8 @@ require 'fileutils'
module HCl
class Task < TimesheetResource
def self.cache_tasks_hash day_entry_hash
- tasks = day_entry_hash['projects'].
- map { |p| p['tasks'].each {|t| new t.merge(project:p) } }.flatten.uniq
+ tasks = day_entry_hash[:projects].
+ map { |p| p[:tasks].map {|t| new t.merge(project:Project.new(p)) } }.flatten.uniq
unless tasks.empty?
FileUtils.mkdir_p(cache_dir)
File.open(cache_file, 'w') do |f|
@@ -14,22 +14,6 @@ module HCl
end
end
- 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
- unless tasks.empty?
- FileUtils.mkdir_p(cache_dir)
- File.open(cache_file, 'w') do |f|
- f.write tasks.uniq.to_yaml
- end
- end
- end
-
def self.cache_file
File.join(cache_dir, 'tasks.yml')
end
@@ -66,16 +50,13 @@ module HCl
def add opts
notes = opts[:note]
starting_time = opts[:starting_time] || 0
- days = DayEntry.from_xml Task.post("daily/add", <<-EOT)
- <request>
- <notes>#{notes}</notes>
- <hours>#{starting_time}</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
- days.first
+ DayEntry.new Task.post("daily/add", {
+ notes: notes,
+ hours: starting_time,
+ project_id: project.id,
+ task_id: id,
+ spent_at: Date.today
+ })
end
def start opts
@@ -83,7 +64,7 @@ module HCl
if day.running?
day
else
- DayEntry.from_xml(Task.get("daily/timer/#{day.id}")).first
+ DayEntry.new Task.get("daily/timer/#{day.id}")[:day_entries].first
end
end
end
diff --git a/lib/hcl/timesheet_resource.rb b/lib/hcl/timesheet_resource.rb
index 44d9ca0..cb6fce5 100644
--- a/lib/hcl/timesheet_resource.rb
+++ b/lib/hcl/timesheet_resource.rb
@@ -53,17 +53,14 @@ module HCl
end
def self.faraday
- @faraday ||= begin
- Faraday.new(
- "http#{ssl && 's'}://#{subdomain}.harvestapp.com"
- ) do |f|
- #f.headers['Accept'] = 'application/json'
- f.headers['Accept'] = 'application/xml'
- #f.request :json
- f.request :basic_auth, login, password
- #f.response :json, content_type: /\bjson$/
- f.adapter Faraday.default_adapter
- end
+ @faraday ||= Faraday.new(
+ "http#{ssl && 's'}://#{subdomain}.harvestapp.com"
+ ) do |f|
+ f.headers['Accept'] = 'application/json'
+ f.request :json
+ f.request :basic_auth, login, password
+ f.use HCl::YajlMiddleware, content_type: /\bjson$/
+ f.adapter Faraday.default_adapter
end
end
@@ -94,12 +91,5 @@ module HCl
def respond_to? method
(@data && @data.key?(method.to_sym)) || super
end
-
- def self.xml_to_hash elem
- elem.elements.map { |e| e.name }.inject({}) do |a, f|
- a[f.to_sym] = CGI.unescape_html(elem.elements[f].text || '') if elem.elements[f]
- a
- end
- end
end
end
diff --git a/lib/hcl/yajl_middleware.rb b/lib/hcl/yajl_middleware.rb
new file mode 100644
index 0000000..186c3d4
--- /dev/null
+++ b/lib/hcl/yajl_middleware.rb
@@ -0,0 +1,8 @@
+require 'faraday_middleware/response_middleware'
+require 'yajl'
+
+class HCl::YajlMiddleware < FaradayMiddleware::ResponseMiddleware
+ define_parser do |body|
+ Yajl::Parser.parse(body, symbolize_keys:true)
+ end
+end
diff --git a/test/day_entry_test.rb b/test/day_entry_test.rb
index 581b22b..1df4cba 100644
--- a/test/day_entry_test.rb
+++ b/test/day_entry_test.rb
@@ -19,38 +19,6 @@ class DayEntryTest < HCl::TestCase
assert_equal "Taco Town - Pizza Taco - Preparation (1:12)", entry.to_s
end
- def test_from_xml
- entries = HCl::DayEntry.from_xml(<<-EOD)
-<daily>
- <for_day type="date">Wed, 18 Oct 2006</for_day>
- <day_entries>
- <day_entry>
- <id type="integer">195168</id>
- <client>Iridesco</client>
- <project>Harvest</project>
- <task>Backend Programming</task>
- <hours type="float">2.06</hours>
- <notes>Test api support</notes>
- <timer_started_at type="datetime">
- Wed, 18 Oct 2006 09:53:06 -0000
- </timer_started_at>
- <created_at type="datetime">Wed, 18 Oct 2006 09:53:06 -0000</created_at>
- </day_entry>
- </day_entries>
-</daily>
- EOD
- assert_equal 1, entries.size
- {
- :project => 'Harvest',
- :client => 'Iridesco',
- :task => 'Backend Programming',
- :notes => 'Test api support',
- :hours => '2.06',
- }.each do |method, value|
- assert_equal value, entries.first.send(method)
- end
- end
-
def test_append_note
entry = HCl::DayEntry.new(:id => '1', :notes => 'yourmom.', :hours => '1.0')
HCl::DayEntry.stubs(:post)
diff --git a/test/task_test.rb b/test/task_test.rb
index f5c30b8..bbdce89 100644
--- a/test/task_test.rb
+++ b/test/task_test.rb
@@ -1,61 +1,20 @@
class TaskTest < HCl::TestCase
- DAILY_ENTRY = %{<daily>
- <for_day type="date">Wed, 18 Oct 2006</for_day>
- <day_entries>
- <day_entry>
- <id type="integer">195168</id>
- <client>Iridesco</client>
- <project>Harvest</project>
- <task>Backend Programming</task>
- <hours type="float">2.06</hours>
- <notes>Test api support</notes>
- <timer_started_at type="datetime">
- Wed, 18 Oct 2006 09:53:06 -0000
- </timer_started_at>
- <created_at type="datetime">Wed, 18 Oct 2006 09:53:06 -0000</created_at>
- </day_entry>
- </day_entries>
- </daily>}
-
- def test_add_task
- task = HCl::Task.new(id:456, project:HCl::Project.new(id:123))
- Date.expects(:today).returns('now')
- HCl::Task.expects(:post).with('daily/add', <<-EOT).returns(DAILY_ENTRY)
- <request>
- <notes>hi world</notes>
- <hours>0.5</hours>
- <project_id type="integer">123</project_id>
- <task_id type="integer">456</task_id>
- <spent_at type="date">now</spent_at>
- </request>
- EOT
- task.add note:'hi world', starting_time:0.5
- end
def test_cache_file
assert_equal "#{HCl::App::HCL_DIR}/cache/tasks.yml", HCl::Task.cache_file
end
- def test_cache_tasks
- HCl::Task.cache_tasks(REXML::Document.new(<<-EOD))
-<daily>
- <projects>
- <project>
- <name>Click and Type</name>
- <code></code>
- <id type="integer">3</id>
- <client>AFS</client>
- <tasks>
- <task>
- <name>Security support</name>
- <id type="integer">14</id>
- <billable type="boolean">true</billable>
- </task>
- </tasks>
- </project>
- </projects>
-</daily>
- EOD
+ def test_cache_tasks_hash
+ HCl::Task.cache_tasks_hash({ projects: [{
+ name: "Click and Type",
+ id: 3,
+ client: "AFS",
+ tasks: [{
+ name: "Security support",
+ id: 14,
+ billable: true
+ }]
+ }]})
assert_equal 1, HCl::Task.all.size
assert_equal 'Security support', HCl::Task.all.first.name
end