diff options
| author | Zack Hobson | 2013-12-22 17:07:49 -0800 | 
|---|---|---|
| committer | Zack Hobson | 2013-12-22 17:07:49 -0800 | 
| commit | 0e3581420a697c40ae91c2ad92884cd8e1eebe03 (patch) | |
| tree | f8228ad368de8e86691c28f7e9e7f3d2c749125c /lib | |
| parent | 2f6c16ddba55c516b28fc98302c7659bfc06ef3b (diff) | |
| download | hcl-0e3581420a697c40ae91c2ad92884cd8e1eebe03.tar.bz2 | |
remove xml processing code
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/hcl.rb | 1 | ||||
| -rw-r--r-- | lib/hcl/day_entry.rb | 18 | ||||
| -rw-r--r-- | lib/hcl/task.rb | 39 | ||||
| -rw-r--r-- | lib/hcl/timesheet_resource.rb | 26 | ||||
| -rw-r--r-- | lib/hcl/yajl_middleware.rb | 8 | 
5 files changed, 31 insertions, 61 deletions
| @@ -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 | 
