From f0eee83fcdf3047674db3310f2847704021e54e1 Mon Sep 17 00:00:00 2001 From: Zack Hobson Date: Fri, 27 Dec 2013 10:15:53 -0800 Subject: console command --- CHANGELOG | 3 ++- Gemfile.lock | 1 + README.markdown | 19 +++++++++++++++++++ hcl.gemspec | 1 + lib/hcl.rb | 1 + lib/hcl/commands.rb | 6 ++++++ lib/hcl/console.rb | 19 +++++++++++++++++++ 7 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 lib/hcl/console.rb diff --git a/CHANGELOG b/CHANGELOG index 7a7d747..3e091c7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,7 +2,8 @@ == next version -* addded `config` command to display current credentials +* added `config` command to display current credentials +* added `console` command for exploring the Harvest API == v0.4.9 2013-12-21 diff --git a/Gemfile.lock b/Gemfile.lock index 362ea92..fe0c913 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,6 +6,7 @@ PATH escape_utils faraday highline + pry trollop yajl-ruby diff --git a/README.markdown b/README.markdown index e898cb9..6c185aa 100644 --- a/README.markdown +++ b/README.markdown @@ -137,6 +137,25 @@ Adding something like the above to your bashrc will enable a new command, `myhcl`. When using `myhcl` you can use different credentials and aliases, while `hcl` will continue to function with your original configuration. +### Interactive console + +An interactive Ruby console is provided to allow you to use the fairly +powerful Harvest API client built into HCl, since not all of its +features are exposed via the command line. The current {HCl::App} +instance is available as `hcl`. + +It's also possible to issue HCl commands directly (as Ruby methods), or +to query specific JSON end points and have the results pretty-printed: + + hcl console + bin/hcl> show "yesterday" + # => prints yesterday's timesheet, note the quotes! + bin/hcl> Net.get('daily') + # => displays a pretty-printed version of the JSON output + +Note that unlike the commands themselves, the HCl internals may change without +notice. + ### Date Formats Dates can be expressed in a variety of ways. See the [Chronic documentation][cd] diff --git a/hcl.gemspec b/hcl.gemspec index 7dd0fd8..cf1c615 100644 --- a/hcl.gemspec +++ b/hcl.gemspec @@ -21,6 +21,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'faraday' s.add_runtime_dependency 'yajl-ruby' s.add_runtime_dependency 'escape_utils' + s.add_runtime_dependency 'pry' 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 5d6f108..b041538 100644 --- a/lib/hcl.rb +++ b/lib/hcl.rb @@ -3,6 +3,7 @@ module HCl autoload :App, 'hcl/app' autoload :Net, 'hcl/net' autoload :Commands, 'hcl/commands' + autoload :Console, 'hcl/console' autoload :TimesheetResource, 'hcl/timesheet_resource' autoload :Utility, 'hcl/utility' autoload :Project, 'hcl/project' diff --git a/lib/hcl/commands.rb b/lib/hcl/commands.rb index 98daff5..3e96077 100644 --- a/lib/hcl/commands.rb +++ b/lib/hcl/commands.rb @@ -1,5 +1,6 @@ require 'chronic' require 'highline' +require 'pry' module HCl module Commands @@ -10,6 +11,11 @@ module HCl Net.config_hash.merge(password:'***').map {|k,v| "#{k}: #{v}" }.join("\n") end + def console + Console.new(self) + nil + end + def tasks project_code=nil tasks = Task.all if tasks.empty? # cache tasks diff --git a/lib/hcl/console.rb b/lib/hcl/console.rb new file mode 100644 index 0000000..da0f7c5 --- /dev/null +++ b/lib/hcl/console.rb @@ -0,0 +1,19 @@ +require 'pp' + +module HCl + class Console + attr_reader :hcl + def initialize app + @hcl = app + binding.pry quiet: true, + prompt:[->(a,b,c){"#{$PROGRAM_NAME}> "}], + print:->(io, *p){ pp p } + end + + Commands.instance_methods.each do |command| + define_method command do |*args| + puts @hcl.send(command, *args) + end + end + end +end -- cgit v1.2.3