Skip to content

Commit

Permalink
Cleanup, email sharing option, rdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
sosedoff committed Jan 8, 2011
1 parent 4742337 commit 50cc1ce
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 61 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
36 changes: 0 additions & 36 deletions README

This file was deleted.

45 changes: 45 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions TODO

This file was deleted.

16 changes: 16 additions & 0 deletions bin/pastie
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 24 additions & 9 deletions lib/pastie-api/console.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 2 additions & 6 deletions lib/pastie-api/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions lib/pastie-api/pastie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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?
Expand All @@ -37,5 +37,4 @@ def self.parser_id(language=nil)
end
@parser[language.to_s] || @parser[:plain]
end

end
54 changes: 54 additions & 0 deletions lib/pastie-api/share.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions pastie.gemspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit 50cc1ce

Please sign in to comment.