diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/hcl.rb | 1 | ||||
| -rw-r--r-- | lib/hcl/app.rb | 4 | ||||
| -rw-r--r-- | lib/hcl/commands.rb | 2 | ||||
| -rw-r--r-- | lib/hcl/day_entry.rb | 6 | ||||
| -rw-r--r-- | lib/hcl/harvest_middleware.rb | 2 | ||||
| -rw-r--r-- | lib/hcl/net.rb | 55 | ||||
| -rw-r--r-- | lib/hcl/task.rb | 4 | ||||
| -rw-r--r-- | lib/hcl/timesheet_resource.rb | 57 | 
8 files changed, 65 insertions, 66 deletions
| @@ -1,6 +1,7 @@  module HCl    autoload :VERSION, 'hcl/version'    autoload :App, 'hcl/app' +  autoload :Net, 'hcl/net'    autoload :Commands, 'hcl/commands'    autoload :TimesheetResource, 'hcl/timesheet_resource'    autoload :Utility, 'hcl/utility' diff --git a/lib/hcl/app.rb b/lib/hcl/app.rb index 671575c..1f641d5 100644 --- a/lib/hcl/app.rb +++ b/lib/hcl/app.rb @@ -140,7 +140,7 @@ EOM          if has_security_command?            load_password config          end -        TimesheetResource.configure config +        Net.configure config        else          request_config        end @@ -153,7 +153,7 @@ EOM        config['password'] = ask("Password: ") { |q| q.echo = false }.to_s        config['subdomain'] = ask("Subdomain: ").to_s        config['ssl'] = /^y/.match(ask("Use SSL? (y/n): ").downcase) -      TimesheetResource.configure config +      Net.configure config        write_config config      end diff --git a/lib/hcl/commands.rb b/lib/hcl/commands.rb index e9fd847..cf8ddc5 100644 --- a/lib/hcl/commands.rb +++ b/lib/hcl/commands.rb @@ -7,7 +7,7 @@ module HCl      # Display a sanitized view of your auth credentials.      def config -      TimesheetResource.config_hash.merge(password:'***'). +      Net.config_hash.merge(password:'***').          map {|k,v| "#{k}: #{v}" }.join("\n")      end diff --git a/lib/hcl/day_entry.rb b/lib/hcl/day_entry.rb index 6441f1d..645bb3d 100644 --- a/lib/hcl/day_entry.rb +++ b/lib/hcl/day_entry.rb @@ -6,7 +6,7 @@ module HCl      # defaults to today.      def self.all date = nil        url = date.nil? ? 'daily' : "daily/#{date.strftime '%j/%Y'}" -      doc = get url +      doc = Net.get url        Task.cache_tasks_hash doc        doc[:day_entries].map {|e| new e}      end @@ -37,7 +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}", notes:notes, hours:hours +      Net.post "daily/update/#{id}", notes:notes, hours:hours      end      def self.with_timer date=nil @@ -63,7 +63,7 @@ module HCl      end      def toggle -      DayEntry.get("daily/timer/#{id}") +      Net.get("daily/timer/#{id}")        self      end diff --git a/lib/hcl/harvest_middleware.rb b/lib/hcl/harvest_middleware.rb index 47e790d..4d742ed 100644 --- a/lib/hcl/harvest_middleware.rb +++ b/lib/hcl/harvest_middleware.rb @@ -18,7 +18,7 @@ class HCl::HarvestMiddleware < FaradayMiddleware::ResponseMiddleware        case env[:status]        when 200..299          begin  -          env[:body] = unescape(MultiJson.load(env[:body].chomp, symbolize_keys:true)) +          env[:body] = unescape(MultiJson.load(env[:body], symbolize_keys:true))          rescue MultiJson::LoadError            env[:body]          end diff --git a/lib/hcl/net.rb b/lib/hcl/net.rb new file mode 100644 index 0000000..2339f1d --- /dev/null +++ b/lib/hcl/net.rb @@ -0,0 +1,55 @@ +require 'faraday_middleware' + +module HCl +  module Net +    # configuration accessors +    CONFIG_VARS = [ :login, :password, :subdomain, :ssl ].freeze +    CONFIG_VARS.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 self.configure opts = nil +      if opts +        self.login = opts['login'] +        self.password = opts['password'] +        self.subdomain = opts['subdomain'] +        self.ssl = opts['ssl'] +      end +    end + +    def self.config_hash +      CONFIG_VARS.inject({}) {|c,k| c.update(k => send(k)) } +    end + +    def self.faraday +      @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::HarvestMiddleware, content_type: /\bjson\b/ +        f.adapter Faraday.default_adapter +      end +    end + +    def self.get action +      faraday.get(action).body +    end + +    def self.post action, data +      faraday.post(action, data).body +    end + +    def self.delete action +      faraday.delete(action).body +    end +  end +end diff --git a/lib/hcl/task.rb b/lib/hcl/task.rb index 7678655..e1d5b58 100644 --- a/lib/hcl/task.rb +++ b/lib/hcl/task.rb @@ -50,7 +50,7 @@ module HCl      def add opts        notes = opts[:note]        starting_time = opts[:starting_time] || 0 -      DayEntry.new Task.post("daily/add", { +      DayEntry.new Net.post("daily/add", {          notes: notes,          hours: starting_time,          project_id: project.id, @@ -64,7 +64,7 @@ module HCl        if day.running?          day        else -        DayEntry.new Task.get("daily/timer/#{day.id}") +        DayEntry.new Net.get("daily/timer/#{day.id}")        end      end    end diff --git a/lib/hcl/timesheet_resource.rb b/lib/hcl/timesheet_resource.rb index 518ebf8..28e5378 100644 --- a/lib/hcl/timesheet_resource.rb +++ b/lib/hcl/timesheet_resource.rb @@ -1,66 +1,9 @@ -require 'net/http' -require 'net/https' -require 'cgi' -require 'faraday_middleware' -  module HCl    class TimesheetResource - -    def self.configure opts = nil -      if opts -        self.login = opts['login'] -        self.password = opts['password'] -        self.subdomain = opts['subdomain'] -        self.ssl = opts['ssl'] -      end -    end - -    # configuration accessors -    CONFIG_VARS = [ :login, :password, :subdomain, :ssl ].freeze -    CONFIG_VARS.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 - -    # @return [Hash] -    def self.config_hash -      CONFIG_VARS.inject({}) {|c,k| c.update(k => TimesheetResource.send(k)) } -    end - -    def self.faraday -      @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::HarvestMiddleware, content_type: /\bjson\b/ -        f.adapter Faraday.default_adapter -      end -    end -      def initialize params        @data = params      end -    def self.get action -      faraday.get(action).body -    end - -    def self.post action, data -      faraday.post(action, data).body -    end - -    def self.delete action -      faraday.delete(action).body -    end -      def id        @data[:id]      end | 
