cliff 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Sebastian Cohnen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # What's this about?
2
+ Cliff is a CLI-Client for [friendpaste.com][fp]. You can paste stuff to Friendpaste by piping it to cliff or fetch contents of a existing paste. It's not yet finished but should work fine. Currently only getting existing and creating new snippets is supported. Updates, Diffs, Versioning, etc. will soon follow!
3
+
4
+ And btw: [friendpaste.com][fp] is powered by [CouchDB][couch] :)
5
+
6
+ # Usage
7
+
8
+ cliff [--help | --list-languages | --refresh]
9
+
10
+ `--help` should be pretty self-explanatory. `--list-languages` prints a list of all available languages for syntax highlighting from friendpaste. `--refresh` fetches a new list from friendpaste.com and stores it in `$HOME/.friendpaste_languages`.
11
+
12
+ cliff [FILE] [LANGUAGE]
13
+ cliff [SOME_EXISTING_SNIPPET_ID]
14
+
15
+ Upload stuff from stdin
16
+
17
+ echo "Hello world!" | cliff
18
+
19
+ Upload contents from file
20
+
21
+ cliff < file.txt
22
+ cliff myrubyscript.rb rb
23
+
24
+ Fetch a paste
25
+
26
+ cliff SOME_SNIPPET_ID > output.txt
27
+
28
+ # Notes
29
+ When creating a new snippet, the complete URL is copied to your clipboard for direct usage on IRC and co.
30
+
31
+ This CLI-Tool is heavily inspired by http://github.com/defunkt/gist.
32
+
33
+
34
+ [fp]: http://friendpaste.com/
35
+ [couch]: http://couchdb.apache.org/
data/bin/cliff ADDED
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "cliff"
5
+ require "trollop"
6
+
7
+ # Define some command-line options
8
+ opts = Trollop.options do
9
+ version "v0.2 (by) Sebastian Cohnen, 2010"
10
+ banner <<-BANNER
11
+ Cliff is a CLI-Client for friendpaste.com. It's not yet finished but
12
+ should work. Currently only getting existing and creating new snippets
13
+ is supported. Updates, Diffs, Versioning, etc. will soon follow!
14
+
15
+ http://github.com/tisba/cliff
16
+
17
+ Example usage:
18
+ # echo "Hello world!" | cliff
19
+ # cliff < file.txt
20
+ # cliff someexistingfile.txt
21
+ # cliff SOME_SNIPPET_ID > output.txt
22
+
23
+ Note:
24
+ When creating a new snippet, the complete URL is copied to your
25
+ clipboard for direct usage on IRC and co.
26
+
27
+ Cliff is heavily inspired by the CLI-Clients for gist :-)
28
+
29
+ Options:
30
+ BANNER
31
+ opt :list_languages, "List all available languages for syntax highlighting on friendpaste", :default => false
32
+ opt :refresh, "Refresh available languages", :default => false
33
+ opt :language, "Specify the language for highlighting purposes on fp", :default => "text"
34
+
35
+ if ARGV.empty? && STDIN.tty?
36
+ educate
37
+ exit
38
+ end
39
+ end
40
+
41
+ # List all available languages
42
+ if opts[:list_languages]
43
+ Cliff.languages.each { |lang| puts lang.join(': ') }
44
+ exit
45
+ end
46
+
47
+ # Refresh available languages
48
+ if opts[:refresh]
49
+ Cliff::Helpers.refresh_languages_cache
50
+ exit
51
+ end
52
+
53
+ # Let's rock!
54
+
55
+ # Let's see if our first param could be an existing file...
56
+ if ARGV.first and File.exists?(ARGV.first)
57
+ input = File.read(ARGV.first)
58
+ opts = {"language" => ARGV[1]} if Cliff::Helpers.is_language?(ARGV[1])
59
+ puts Cliff.create(input, opts).snippet_id
60
+ exit
61
+ end
62
+
63
+ if STDIN.tty?
64
+ puts Cliff.get(ARGV.first).snippet
65
+ else
66
+ opts = {"language" => ARGV.first} if Cliff::Helpers.is_language?(ARGV.first)
67
+ puts Cliff.create(STDIN.read, opts).snippet_id
68
+ end
data/lib/cliff.rb ADDED
@@ -0,0 +1,121 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'open-uri'
4
+ require 'net/http'
5
+
6
+ begin
7
+ require 'yajl/json'
8
+ rescue LoadError
9
+ require 'json'
10
+ end
11
+
12
+ class Cliff
13
+
14
+ class <<self
15
+ def languages
16
+ @languages ||= Helpers.load_languages_from_cache
17
+ end
18
+
19
+ def create(content, params)
20
+ snippet = Snippet.new(content, params)
21
+ snippet.save
22
+ Helpers.copy(snippet.url)
23
+ return snippet
24
+ end
25
+
26
+ def get(doc_id)
27
+ headers = {"Accept" => "application/json"}
28
+ raw = open("#{FP_URL}/#{doc_id}", headers).read
29
+ Snippet.new(JSON.parse(raw))
30
+ end
31
+
32
+ def fp_url
33
+ "http://www.friendpaste.com/"
34
+ end
35
+ end
36
+
37
+ class Helpers
38
+ class << self
39
+ def is_language?(opt)
40
+ Cliff.languages.flatten.include?(opt)
41
+ end
42
+
43
+ def cache_file_name
44
+ File.join(ENV['HOME'], ".friendpaste_languages")
45
+ end
46
+
47
+ def refresh_languages_cache
48
+ languages = fetch_languages
49
+ File.open(cache_file_name, 'w+') {|f| f.write(languages) }
50
+ JSON.parse(languages)
51
+ end
52
+
53
+ def load_languages_from_cache
54
+ if File.exists?(cache_file_name)
55
+ JSON.parse(File.read(cache_file_name))
56
+ else
57
+ refresh_languages_cache
58
+ end
59
+ end
60
+
61
+ def fetch_languages
62
+ open("#{fp_url}/_all_languages").read
63
+ end
64
+
65
+ # Copy content to clipboard
66
+ def copy(content)
67
+ case RUBY_PLATFORM
68
+ when /darwin/
69
+ return content if `which pbcopy`.strip == ''
70
+ IO.popen('pbcopy', 'r+') { |clip| clip.print content }
71
+ when /linux/
72
+ return content if `which xclip 2> /dev/null`.strip == ''
73
+ IO.popen('xclip', 'r+') { |clip| clip.print content }
74
+ when /i386-cygwin/
75
+ return content if `which putclip`.strip == ''
76
+ IO.popen('putclip', 'r+') { |clip| clip.print content }
77
+ end
78
+
79
+ content
80
+ end
81
+ end
82
+ end
83
+
84
+ # Class for representing a snippet
85
+ class Snippet
86
+ attr_reader :snippet_id
87
+
88
+ def initialize(data, params = {})
89
+ data = {"snippet" => data} unless data.kind_of? Hash
90
+
91
+ @data = { "title" => "",
92
+ "language" => "text"
93
+ }.merge(data).merge(params)
94
+ end
95
+
96
+ def self.create(data)
97
+ snippet = new(data)
98
+ snippet.save
99
+ end
100
+
101
+ def url
102
+ "#{Cliff::fp_url}#{@snippet_id}"
103
+ end
104
+
105
+ def method_missing(meth, *args, &blk)
106
+ super unless @data.keys.include?(meth.to_s)
107
+ @data[meth.to_s]
108
+ end
109
+
110
+ def save
111
+ uri = URI.parse(Cliff::fp_url)
112
+ headers = {"Content-Type" => "application/json", "Accept" => "application/json"}
113
+ res = Net::HTTP.new(uri.host, uri.port).post(uri.path, @data.to_json, headers)
114
+ if res.kind_of? Net::HTTPSuccess
115
+ @snippet_id = JSON.parse(res.body)["id"]
116
+ else
117
+ puts "Something went wrong #{res.code.inspect}"
118
+ end
119
+ end
120
+ end
121
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ require 'cliffg'
7
+
8
+ class Test::Unit::TestCase
9
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestCliff < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cliff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Cohnen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-13 00:00:00 +01:00
13
+ default_executable: cliff
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: trollop
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Cliff is a CLI client written in ruby for friendpaste.com
36
+ email: sebastian.cohnen@gmx.net
37
+ executables:
38
+ - cliff
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.md
44
+ files:
45
+ - README.md
46
+ - lib/cliff.rb
47
+ - LICENSE
48
+ has_rdoc: true
49
+ homepage: http://github.com/tisba/cliff
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: CLI client in ruby for friendpaste.com
76
+ test_files:
77
+ - test/helper.rb
78
+ - test/test_cliff.rb