Sha256: 38cb9950cd5c1f954a405fb423b2bd648df52b2778c522e2b5d43ff2b8702434

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module Boson::Commands::WebCore #:nodoc:
  def self.config
    descriptions = {
      :install=>"Installs a library by url. Library should then be loaded with load_library.",
      :browser=>"Opens urls in a browser", :get=>"Gets the body of a url" }
    commands = descriptions.inject({}) {|h,(k,v)| h[k.to_s] = {:description=>v}; h}
    commands['install'][:options] = {:name=>:string, :force=>:boolean}
    {:library_file=>File.expand_path(__FILE__), :commands=>commands}
  end

  def get(url)
    %w{uri net/http}.each {|e| require e }
    Net::HTTP.get(URI.parse(url))
  rescue
    raise "Error opening #{url}"
  end

  def install(url, options={})
    options[:name] ||= strip_name_from_url(url)
    return puts("Please give a library name with this url.") unless options[:name]
    filename = File.join Boson.repo.commands_dir, "#{options[:name]}.rb"
    return puts("Library name #{options[:name]} already exists. Try a different name.") if File.exists?(filename) && !options[:force]
    File.open(filename, 'w') {|f| f.write get(url) }
    puts "Saved to #{filename}."
  end

  # non-mac users should override this with the launchy gem
  def browser(*urls)
    system('open', *urls)
  end

  private
  def strip_name_from_url(url)
    url[/\/([^\/.]+)(\.[a-z]+)?$/, 1]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
boson-0.0.1 lib/boson/commands/web_core.rb