Sha256: 519e56ee9f38f52bb0753703ea978d0a467794c363e7f4df4f1e8905b70fc379

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

#!/usr/bin/env ruby
# The command line Restfully client

$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'

require 'restfully'
require 'optparse'
require 'logger'
require 'pp'

@options = {}
option_parser = OptionParser.new do |opts|
  opts.banner = "Usage: restfully base_uri [root_path] [options]"

  opts.on("-u=", "--username=", "Sets the username") do |u|
    @options['username'] = u
  end
  opts.on("-p=", "--password=", "Sets the user password") do |p|
    @options['password'] = p
  end
  opts.on("-c=", "--config=", "Sets the various options based on a custom YAML configuration file") do |v|
    @options['configuration_file'] = v
  end
  opts.on("-v", "--verbose", "Run verbosely") do |v|
    @options['verbose'] = v
  end
  opts.on("--log=", "Outputs log messages to the given file. Defaults to stdout") do |v|
    @options['log'] = v
  end
  
  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
  
end

option_parser.parse!

@base_uri = ARGV.shift
@root_path = ARGV.shift || "/"

if (config_filename = @options.delete('configuration_file')) && File.exists?(File.expand_path(config_filename))
  config = YAML.load_file(File.expand_path(config_filename))
  @base_uri = config.delete('base_uri') || @base_uri
  @root_path = config.delete('root_path') || @root_path
  @options.merge!(config)
end

unless @base_uri
  $stderr.puts option_parser.help
  exit(-1)
else
  if (log_file=@options.delete('log'))
    @logger = Logger.new(File.expand_path(log_file))
  else
    @logger = Logger.new(STDOUT)
  end
  if @options.delete('verbose')
    @logger.level = Logger::DEBUG
  else
    @logger.level = Logger::WARN
  end

  def session
    @session ||= Restfully::Session.new(@base_uri, @options.merge('root_path' => @root_path, 'logger' => @logger))
  end
  def root 
    @root ||= Restfully::Resource.new(session.root_path, session).load
  end
  
  root # preloads
  
  require 'irb'
  require 'irb/completion'
  
  ARGV.clear
  IRB.start
  exit!
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
crohr-restfully-0.2.1 bin/restfully
crohr-restfully-0.2.2 bin/restfully
restfully-0.2.2 bin/restfully
restfully-0.2.1 bin/restfully