Class: PastrIt

Inherits:
Object
  • Object
show all
Defined in:
lib/pastr_it.rb

Constant Summary collapse

VERSION =
'1.0.2'
REALM =
'Pastr Registered User'
PastrHome =
"http://pastr.it"
PastrNew =
"%s/%s" % [PastrHome, :new]
PastrEdit =
"%s/%s" % [PastrHome, :edit]
PastrNote =
"%s/%s" % [PastrHome, :annotate]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ PastrIt

Returns a new instance of PastrIt.



10
11
12
13
14
# File 'lib/pastr_it.rb', line 10

def initialize(args = nil)
  @network, @username = "Freenode", ENV["USER"]
  @args = args || ARGV
  parse_args
end

Instance Attribute Details

#annotate_idObject

Returns the value of attribute annotate_id.



9
10
11
# File 'lib/pastr_it.rb', line 9

def annotate_id
  @annotate_id
end

#channelObject

Returns the value of attribute channel.



9
10
11
# File 'lib/pastr_it.rb', line 9

def channel
  @channel
end

#filenameObject

Returns the value of attribute filename.



9
10
11
# File 'lib/pastr_it.rb', line 9

def filename
  @filename
end

#languageObject

Returns the value of attribute language.



9
10
11
# File 'lib/pastr_it.rb', line 9

def language
  @language
end

#networkObject

Returns the value of attribute network.



9
10
11
# File 'lib/pastr_it.rb', line 9

def network
  @network
end

#no_bodyObject

Returns the value of attribute no_body.



9
10
11
# File 'lib/pastr_it.rb', line 9

def no_body
  @no_body
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/pastr_it.rb', line 9

def password
  @password
end

#pastr_idObject

Returns the value of attribute pastr_id.



9
10
11
# File 'lib/pastr_it.rb', line 9

def pastr_id
  @pastr_id
end

#titleObject

Returns the value of attribute title.



9
10
11
# File 'lib/pastr_it.rb', line 9

def title
  @title
end

#usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/pastr_it.rb', line 9

def username
  @username
end

Class Method Details

.pastr_it(args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pastr_it.rb', line 16

def self.pastr_it(args)
  me = PastrIt.new(args)
  me.parse_args
  if me.annotate_id
    me.annotate_it
  elsif me.pastr_id
    me.edit_it
  else
    me.pastr_it
  end
end

Instance Method Details

#annotate_itObject



78
79
80
81
82
83
84
# File 'lib/pastr_it.rb', line 78

def annotate_it
  aid = "annotation_#{@annotate_id}"
  form = {"#{aid}[paste_body]" => paste_body}
  form["#{aid}[title]"] = title    if title
  form["language"]      = language if language
  puts http_request(:form => form, :url => "%s/%s" % [PastrNote, @annotate_id]).content
end

#edit_itObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/pastr_it.rb', line 86

def edit_it
  pid = "pastr_#{@pastr_id}"
  form = {}
  form["#{pid}[network]"]     = network    if network
  form["#{pid}[channel]"]     = channel.sub(/^_/,"#") if channel
  form["#{pid}[title]"]       = title      if title
  form["language"]            = language   if language
  form["#{pid}[paste_body]"]  = paste_body unless no_body
  puts http_request(:form => form, :url => "%s/%s" % [PastrEdit, @pastr_id]).content
end

#parse_argsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pastr_it.rb', line 28

def parse_args
  return @opts if @opts
  @opts = OptionParser.new
  @opts.banner = "\nUsage: pastr-it [options] FILE"

  @opts.separator "--- Common Pastr Options"
  @opts.separator " To paste from STDIN (instead of a file), leave off FILE, and you'll need to auth with a ~/.netrc"
  @opts.on("-u", "--username USERNAME", "Your username (Default: #{@username})") { |foo| @username = foo }
  @opts.on("-c", "--channel CHANNEL", "IRC Channel for this Pastr (Default: same as username)") { |foo| @channel = foo }
  @opts.separator "\tWhen using your username as the channel argument, the paste will be private"
  @opts.on("-n", "--network NETWORK", "IRC Network for this Pastr (Default: #{network})") { |foo| @network = foo }
  @opts.on("-l", "--language LANGUAGE", "Language to use for syntax highlighting") { |foo| @language = foo }
  @opts.on("-t", "--title TITLE", "Title of this paste (Default: Filename or 'Pastr by #{username}')") { |foo| @title = foo }

  @opts.separator "--- Editing"
  @opts.on("-e", "--edit PASTR_ID", "ID Of paste to edit (instead of making a new paste)") { |foo| @pastr_id = foo }
  @opts.on("-N", "--no-body", "Just edit metadata (channel, language, etc), not the paste body") { |foo| @no_body = true }

  @opts.separator "--- Annotate"
  @opts.on("-a", "--annotate PASTR_ID", "ID Of paste to annotate (incompatible with -e/--edit.  -n/--network, and -c/--channel ignored.)") { |foo| @annotate_id = foo }

  @opts.separator "--- Informational"
  @opts.on("-L", "--list", "List supported languages") { |foo| @list_langs = true }
  @opts.on("-v", "--version", "Print pastr version") { |foo| @version_only = true }
  @opts.on_tail("-h", "--help", "Show this message") do
    puts @opts
    exit 
  end   
  @opts.parse!(@args)

  if @list_langs
    puts http_request(:url => "http://pastr.it/languages", :auth => false).content
    exit
  elsif @version_only
    puts "pastr-it for http://pastr.it - Version: #{PastrIt::VERSION}"
    exit
  end

  @filename = ARGV.shift if ARGV.size == 1 and File.file?(ARGV.first)
  unless @paste_id
    @title ||= File.basename(@filename) if @filename
    @channel ||= @username
  end
  if @filename.nil? and STDIN.isatty
    $stderr.puts "No Input on STDIN and no filename given, what am I supposed to paste?" 
    puts @opts
    exit 1
  end unless @pastr_id and @no_body
end

#pastr_itObject



97
98
99
100
101
102
# File 'lib/pastr_it.rb', line 97

def pastr_it
  form = {'network' => network, 'channel' => channel.sub(/^_/,"#"), 'paste_body' => paste_body}
  form["title"]    = title    if title
  form["language"] = language if language
  puts http_request(:form => form).content
end