Skip to content
Browse files

Cleanup, email sharing option, rdoc

  • Loading branch information...
1 parent 4742337 commit 50cc1ce5746859ac8dd4cfb563b60e6b75cebd33 @sosedoff committed Jan 8, 2011
Showing with 151 additions and 61 deletions.
  1. +1 −0 .gitignore
  2. +0 −36 README
  3. +45 −0 README.rdoc
  4. +0 −3 TODO
  5. +16 −0 bin/pastie
  6. +24 −9 lib/pastie-api/console.rb
  7. +2 −6 lib/pastie-api/history.rb
  8. +2 −3 lib/pastie-api/pastie.rb
  9. +54 −0 lib/pastie-api/share.rb
  10. +7 −4 pastie.gemspec
View
1 .gitignore
@@ -0,0 +1 @@
+.DS_Store
View
36 README
@@ -1,36 +0,0 @@
-== About Pastie
-
-Pastie gem is a simplified API to communicate with Pastie.org website.
-It is based on HTML processing since pastie.org does not provide any API access.
-This gem comes as a library and executable file.
-All pastes are private by default.
-History available from version 0.2.0.
-
-== Usage
-
-require 'rubygems'
-require 'pastie-api'
-
-# Create a new private paste
-p = Pastie.create('Test string')
-
-# Create a new public paste
-p = Pastie.create('Hello!', false)
-
-# View paste details
-puts "Paste ID: #{p.id}"
-puts "Paste Key: #{p.key}"
-puts "URL: #{p.link}"
-puts "Raw link: #{p.raw_link}"
-
-# Find existing paste
-p = Pastie.get(1234567) # find by paste's ID
-p = Pastie.get('abcdefabcdef') # find by paste's private code
-
-== Command-line Usage
-
-Usage: pastie [options] file1 file2 ... fileN
- -i, --info Display this information.
- -p, --public Paste files as public.
- -h, --history Show pastes history.
- -c, --clear Clear your pastes history.
View
45 README.rdoc
@@ -0,0 +1,45 @@
+= Pastie - Simple API wrapper to communicate with Pastie.org website
+
+Pastie gem is a simplified API to communicate with Pastie.org website.
+It is based on HTML processing since pastie.org does not provide any API access.
+This gem comes as a library and executable file.
+All pastes are private by default.
+History tracking feature is available from version 0.2.0.
+
+== Installation
+
+ gem install pastie-api
+
+== Usage: API
+
+ require 'rubygems'
+ require 'pastie-api'
+
+ # Create a new private paste
+ p = Pastie.create('Test string')
+
+ # Create a new public paste
+ p = Pastie.create('Hello!', false)
+
+ # View paste details
+ puts "Paste ID: #{p.id}"
+ puts "Paste Key: #{p.key}"
+ puts "URL: #{p.link}"
+ puts "Raw link: #{p.raw_link}"
+
+ # Find existing paste
+ p = Pastie.get(1234567) # find by paste's ID
+ p = Pastie.get('abcdefabcdef') # find by paste's private code
+
+== Usage: Terminal
+
+ Usage: pastie [options] file1 file2 ... fileN
+ -i, --info Display this information.
+ -p, --public Paste files as public.
+ -h, --history Show pastes history.
+ -c, --clear Clear your pastes history.
+
+== Authors & Contributors
+
+Dan Sosedoff, author, github.com/sosedoff
+Barry Hess, contributor, github.com/bjhess
View
3 TODO
@@ -1,3 +0,0 @@
-== TODO
-
-All good for now :)
View
16 bin/pastie
@@ -3,30 +3,41 @@
lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
+require 'rubygems'
+require 'pony'
require 'optparse'
+require 'yaml'
require 'pastie-api'
require 'pastie-api/console'
require 'pastie-api/history'
+require 'pastie-api/share'
+
+include Pastie::Console
+include Pastie::Share
history = Pastie::History.new
options = {:private => true}
+links = []
optparse = OptionParser.new do |opts|
opts.banner = "Usage: pastie [options] file1 file2 ... fileN"
opts.on('-i', '--info', 'Display this information.') { puts opts ; exit }
opts.on('-p', '--public', 'Paste files as public.') { options[:private] = false }
+ opts.on('-s', '--share EMAIL', 'Share pastes with email') { |v| options[:share] = v }
opts.on('-h', '--history', 'Show pastes history.') { history.print ; exit }
opts.on('-c', '--clear', 'Clear your pastes history.') { history.flush ; history.save ; exit }
end
begin
optparse.parse!
+
unless ARGV.empty?
ARGV.each do |f|
path = File.expand_path(f)
if File.exists?(path)
p = Pastie.create(File.read(path), options[:private])
unless p.nil?
+ links << p.link
history.add(p.link)
puts p.link.green
else
@@ -37,6 +48,11 @@ begin
puts "Error: \"#{path}\" does not exist!".red
end
end
+
+ ARGV.clear
+ if options[:share] && !links.empty?
+ share(links, options[:share])
+ end
else
puts optparse
end
View
33 lib/pastie-api/console.rb
@@ -1,11 +1,26 @@
+module Pastie
+ module Console
+ # Prompt for input
+ def ask(title, opts={})
+ print("#{title}#{opts.key?(:default) ? " [#{opts[:default]}]" : ''}: ") ; result = gets.strip
+ result = opts[:default] if result.empty? && opts.key?(:default)
+ result = result.scan(/[a-z\d\_\-]{1,}/i) if opts.key?(:array)
+ result.empty? && opts.key?(:required) ? ask(title, opts) : result
+ end
+ end
+end
+
class String
- def red; colorize(self, "\e[1m\e[31m"); end
- def green; colorize(self, "\e[1m\e[32m"); end
- def dark_green; colorize(self, "\e[32m"); end
- def yellow; colorize(self, "\e[1m\e[33m"); end
- def blue; colorize(self, "\e[1m\e[34m"); end
- def dark_blue; colorize(self, "\e[34m"); end
- def purple; colorize(self, "\e[1m\e[35m"); end
- def magenta; colorize(self, "\e[1m\e[36m"); end
- def colorize(text, color_code) ; "#{color_code}#{text}\e[0m" ; end
+ def colorize(text, color_code)
+ "#{color_code}#{text}\e[0m"
+ end
+
+ def red; colorize(self, "\e[1m\e[31m"); end
+ def green; colorize(self, "\e[1m\e[32m"); end
+ def dark_green; colorize(self, "\e[32m"); end
+ def yellow; colorize(self, "\e[1m\e[33m"); end
+ def blue; colorize(self, "\e[1m\e[34m"); end
+ def dark_blue; colorize(self, "\e[34m"); end
+ def purple; colorize(self, "\e[1m\e[35m"); end
+ def magenta; colorize(self, "\e[1m\e[36m"); end
end
View
8 lib/pastie-api/history.rb
@@ -12,13 +12,9 @@ def initialize
# Load history
def load
if File.exists?(@path) && File.readable?(@path)
- File.open(@path).readlines("\r\n").each do |l|
- @links << l.strip
- end
+ @links = File.open(@path).readlines("\r\n").collect { |l| l.strip }
else
- if File.writable?(File.dirname(@path))
- File.open(@path, 'w').close
- end
+ File.open(@path, 'w').close if File.writable?(File.dirname(@path))
end
end
View
5 lib/pastie-api/pastie.rb
@@ -14,9 +14,8 @@ def self.create(content, private=true, language=nil)
params = {
"paste[body]" => content.to_s,
"paste[authorization]" => "burger",
- "paste[restricted]" => "1"
+ "paste[restricted]" => private ? "1" : "0"
}
- params["paste[restricted]"] = "0" unless private
params["paste[parser_id]"] = parser_id(language)
resp = Net::HTTP.post_form(URI.parse(BASE_URL + "/pastes"), params)
if resp.kind_of?(Net::HTTPFound)
@@ -27,6 +26,7 @@ def self.create(content, private=true, language=nil)
end
end
+ # Get language ID by its name
def self.parser_id(language=nil)
return "6" if language.nil? # Default to plain text
if @parser.nil?
@@ -37,5 +37,4 @@ def self.parser_id(language=nil)
end
@parser[language.to_s] || @parser[:plain]
end
-
end
View
54 lib/pastie-api/share.rb
@@ -0,0 +1,54 @@
+module Pastie
+ module Share
+ # Email configuration file location
+ def email_file
+ "#{ENV['HOME']}/.pastie_email"
+ end
+
+ # Load configuration
+ def email_config
+ YAML.load(File.read(email_file))
+ end
+
+ # Configure local email sharer
+ def configure_email
+ config = {
+ :username => ask('Gmail', :required => true),
+ :password => ask('Password', :required => true)
+ }
+ File.open(email_file, 'w') { |f| f.write(YAML.dump(config)) }
+ end
+
+ # Share link
+ def share(links, email)
+ unless File.exists?(email_file)
+ configure_email
+ end
+
+ config = email_config
+
+ begin
+ Pony.mail(
+ :from => config[:username],
+ :to => email,
+ :subject => "Pastie.org - New paste!",
+ :body => links.join("\r\n"),
+ :via => :smtp,
+ :via_options => {
+ :address => 'smtp.gmail.com',
+ :port => '587',
+ :enable_starttls_auto => true,
+ :user_name => config[:username],
+ :password => config[:password],
+ :authentication => :plain,
+ :domain => "localhost.localdomain"
+ }
+ )
+ rescue Net::SMTPAuthenticationError
+ puts "Invalid username or password!".red
+ rescue Exception => ex
+ puts "Uncaught exception: #{ex.inspect}".red
+ end
+ end
+ end
+end
View
11 pastie.gemspec
@@ -1,27 +1,30 @@
Gem::Specification.new do |s|
s.name = "pastie-api"
- s.version = "0.2.1"
- s.date = "2010-10-19"
+ s.version = "0.2.2"
+ s.date = "2011-01-08"
s.description = "Simple API and shell access to Pastie.org"
s.summary = "Simple Pastie.org API"
s.authors = ["Dan Sosedoff"]
s.email = "dan.sosedoff@gmail.com"
s.homepage = "http://github.com/sosedoff/pastie"
- # = MANIFEST =
s.files = %w[
+ .gitignore,
+ README.rdoc,
bin/pastie
lib/pastie-api.rb
lib/pastie-api/pastie.rb
lib/pastie-api/request.rb
lib/pastie-api/paste.rb
lib/pastie-api/console.rb
lib/pastie-api/history.rb
+ lib/pastie-api/share.rb
]
- # = MANIFEST =
s.executables = ["pastie"]
s.default_executable = "pastie"
+
+ s.add_dependency('mail','>1.0')
s.has_rdoc = true
s.rubygems_version = '1.3.7'

0 comments on commit 50cc1ce

Please sign in to comment.
Something went wrong with that request. Please try again.