pastie-api 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
+
6
+ require 'pastie'
7
+ require 'pastie/console'
8
+
9
+ unless ARGV.empty?
10
+ ARGV.each do |f|
11
+ path = File.expand_path(f)
12
+ if File.exists?(path)
13
+ p = Pastie.create(File.read(path))
14
+ unless p.nil?
15
+ puts p.link.green
16
+ else
17
+ puts "Error: Cant create paste at this moment.".red
18
+ end
19
+ else
20
+ puts "Error: \"#{path}\" does not exist!".red
21
+ end
22
+ end
23
+ else
24
+ puts "Error: Input file(s) required".red
25
+ end
26
+
@@ -0,0 +1,6 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ require 'pastie/request'
5
+ require 'pastie/paste'
6
+ require 'pastie/pastie'
@@ -0,0 +1,11 @@
1
+ class String
2
+ def red; colorize(self, "\e[1m\e[31m"); end
3
+ def green; colorize(self, "\e[1m\e[32m"); end
4
+ def dark_green; colorize(self, "\e[32m"); end
5
+ def yellow; colorize(self, "\e[1m\e[33m"); end
6
+ def blue; colorize(self, "\e[1m\e[34m"); end
7
+ def dark_blue; colorize(self, "\e[34m"); end
8
+ def purple; colorize(self, "\e[1m\e[35m"); end
9
+ def magenta; colorize(self, "\e[1m\e[36m"); end
10
+ def colorize(text, color_code) ; "#{color_code}#{text}\e[0m" ; end
11
+ end
@@ -0,0 +1,33 @@
1
+ module Pastie
2
+ class Paste
3
+ attr_reader :id, :key
4
+ attr_reader :content
5
+
6
+ # Initializes a pastie from server html response
7
+ def initialize(html)
8
+ url = html.scan(/http:\/\/pastie.org\/([\d]{1,}).txt(\?key=([A-Za-z\d]{1,}))?/)
9
+ unless url.nil?
10
+ url.flatten!
11
+ @id = url[0] ; @key = url[2]
12
+ @content = Request.fetch(raw_link)
13
+ else
14
+ raise 'Invalid html!'
15
+ end
16
+ end
17
+
18
+ # Returns direct link to paste contents
19
+ def raw_link
20
+ BASE_URL + "/#{@id}.txt" + (private? ? "?key=#{@key}" : '')
21
+ end
22
+
23
+ # Return direct link to paste page
24
+ def link
25
+ BASE_URL + (private? ? "/private/#{@key}" : "/#{@id}")
26
+ end
27
+
28
+ # Returns true if paste is private
29
+ def private?
30
+ !@key.nil?
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,28 @@
1
+ module Pastie
2
+ BASE_URL = 'http://pastie.org'
3
+
4
+ # Gets existing paste
5
+ def self.get(id)
6
+ id = id.to_s
7
+ path = id.match(/^[\d]{1,}$/) ? "/#{id}" : "/private/#{id}"
8
+ html = Request.fetch(BASE_URL + path)
9
+ return html.nil? ? nil : Paste.new(html)
10
+ end
11
+
12
+ # Creates a new paste
13
+ def self.create(content, private=true)
14
+ params = {
15
+ "paste[body]" => content.to_s,
16
+ "paste[authorization]" => "burger",
17
+ "paste[restricted]" => "1"
18
+ }
19
+ params["paste[restricted]"] = "0" unless private
20
+ resp = Net::HTTP.post_form(URI.parse(BASE_URL + "/pastes"), params)
21
+ if resp.kind_of?(Net::HTTPFound)
22
+ paste_url = resp.body.scan(/<a href="(.*)">/)[0][0]
23
+ Paste.new(Request.fetch(paste_url))
24
+ else
25
+ raise "Cannot create paste!"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,12 @@
1
+ module Pastie
2
+ class Request
3
+ # Returns response body of specified link
4
+ def self.fetch(link)
5
+ url = URI.parse(link)
6
+ resp = Net::HTTP.start(url.host, url.port) do |http|
7
+ http.get(url.path + "?" + url.query.to_s)
8
+ end
9
+ return resp.body if resp.kind_of?(Net::HTTPOK)
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pastie-api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Dan Sosedoff
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-28 00:00:00 -05:00
18
+ default_executable: pastie
19
+ dependencies: []
20
+
21
+ description: Pastie.org simplified API with terminal access
22
+ email: dan.sosedoff@gmail.com
23
+ executables:
24
+ - pastie
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - bin/pastie
31
+ - lib/pastie.rb
32
+ - lib/pastie/pastie.rb
33
+ - lib/pastie/request.rb
34
+ - lib/pastie/paste.rb
35
+ - lib/pastie/console.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/sosedoff/pastie
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Pastie.org simplified API with terminal access
70
+ test_files: []
71
+