snipplr 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/bin/snipplr ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: UTF-8
3
+ require 'interface/bash_api'
4
+ begin
5
+ BashAPI.new.process
6
+ rescue =>msg # ommiting the exception catches them all
7
+ FileUtils.mkdir(ENV['HOME']+'/.snipplr/') if !File.exists?(ENV['HOME']+'/.snipplr/')
8
+ File.open(ENV['HOME']+'/.snipplr/errors','w+').puts msg
9
+ end
10
+
@@ -0,0 +1,104 @@
1
+ # encoding: UTF-8
2
+ require "model/snipplr_entry"
3
+ require 'rubygems'
4
+ require 'optiflag'
5
+ require 'fileutils'
6
+
7
+ HashRegexp=/\:\w*\s*\=\>\s*\'(\w|\s|\-)*\'/
8
+
9
+ META_DESC=<<END
10
+ The meta data of a new entry in the following form:
11
+ ":title=>'Entry title',tags:=>'C Ruby',:lang=>'c-plus-plus',:content=>'x=1'".
12
+ The content key is optional and will be piped in if not present (by using the '<' opertaor).
13
+ Lang is the language name found in Snipplr’s URLs, For example “javascript” or “c-plus-plus”.
14
+ END
15
+
16
+ class Hash
17
+ def keys_to_sym!
18
+ each_pair { |key,value| self.[]=(key.to_sym,value) }
19
+ end
20
+ end
21
+
22
+ module Snipplr extend OptiFlagSet
23
+ optional_flag 'key' do# it will be possible to place an api_key file in user home instead
24
+ description 'API key (if not provided it will be taken from /home/./snipplr/api_key).'
25
+ end
26
+ optional_flag 'd' do # delete
27
+ description 'Delete an entry with an id number: snipplr -d 123.'
28
+ end
29
+ optional_flag 'g' do# get
30
+ description 'Get the content of an entry and print it out: snipplr -g 123.'
31
+ end
32
+
33
+ optional_flag 'meta' do
34
+ description META_DESC
35
+ validates_against do |f,errors|
36
+ begin
37
+ raise SyntaxError.new if f.value.split(',').find{|he|!HashRegexp.match(he)}
38
+ hash=eval('{'+f.value+'}')
39
+ missing_value=[:title ,:tags ,:language].find{|sym| !hash.has_key?(sym)}
40
+ errors<< "You must pass #{missing_value} value in the meta values" if missing_value
41
+ rescue SyntaxError => e
42
+ errors<< 'meta tag is not well formated'
43
+ end
44
+ end
45
+ end
46
+
47
+ character_flag :c do
48
+ description "Create a new entry: snipplr -c -meta (see the meta description)."
49
+ validates_against do |f,errors|
50
+ errors<< 'you must pass in the meta data values of the new entry' if !ARGV.flags.meta
51
+ end
52
+ end
53
+
54
+ character_flag :l do# list
55
+ description 'List all the existing entries (the format is id and title): snipplr -l.'
56
+ end
57
+ and_process!
58
+ end
59
+
60
+ class BashAPI
61
+ attr_reader :entry
62
+
63
+ def initialize
64
+ @flagsToActions={
65
+ :d => proc{@entry.delete(@f.d)} , :g=>proc{puts @entry.get_content(@f.g).source},
66
+ :l=>proc{@entry.find_all_entries.each{|e| puts e.eid+' '+e.title}},
67
+ :c=>proc{post_request}
68
+ }
69
+ end
70
+
71
+ def process
72
+ @f = ARGV.flags
73
+ FileUtils.mkdir(ENV['HOME']+'/.snipplr') if !File.exist?(ENV['HOME']+'/.snipplr')
74
+ key=(@f.key ? @f.key : (IO.read(ENV['HOME']+'/.snipplr/api_key')).chomp!)
75
+ @entry=SnipplrEntry.new(key)
76
+ process_calls
77
+ end
78
+
79
+ private
80
+ def get_content
81
+ Timeout.timeout(3,Exception.new("No content was piped in (timed out after 3 sec)")) do
82
+ content=''
83
+ $stdin.each {|line| content<< line}
84
+ return content
85
+ end
86
+ end
87
+
88
+ def post_request
89
+ @meta[:content]=get_content if !@meta[:content]
90
+ @entry.post_entry(@meta)
91
+ end
92
+
93
+ def proc(&proc)
94
+ proc
95
+ end
96
+
97
+ def process_calls
98
+ @meta=eval('{'+@f.meta+'}') if @f.meta
99
+ @flagsToActions.each_pair { |flag,act| act.call if @f.send(flag) }
100
+ puts 'Use the -h option for help' if !@f.values.find { |val| val}
101
+ end
102
+
103
+
104
+ end
@@ -0,0 +1,18 @@
1
+ require 'xmlrpc/client'
2
+ NOTVALID=0
3
+ class Server
4
+
5
+ def self.open_session(key)
6
+ server = XMLRPC::Client.new( "snipplr.com", "/xml-rpc.php")
7
+ puts "#{} not valid API Key" and return if server.call("user.checkkey", key)==NOTVALID
8
+ begin
9
+ yield server
10
+ rescue XMLRPC::FaultException => err
11
+ if err.faultString =~ /No snippets found/
12
+ puts "No snippets yet!"
13
+ else
14
+ puts "Error: " + err.faultCode.to_s + ", " + err.faultString
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,44 @@
1
+ require 'ostruct'
2
+ require 'model/server'
3
+ require "cgi"
4
+
5
+
6
+ class SnipplrEntry
7
+
8
+ def initialize(key)
9
+ raise Exception.new('nil isn\'t a legal key value!') if !key
10
+ @key=key
11
+ end
12
+
13
+ def get_content(id)
14
+ Server.open_session(@key) do |server|
15
+ entry=OpenStruct.new(server.call("snippet.get", id))
16
+ entry.source=CGI::unescapeHTML(entry.source)
17
+ return entry
18
+ end
19
+ end
20
+
21
+ def post_entry(info)
22
+ Server.open_session(@key) do |server|
23
+ server.call("snippet.post", @key, info[:title], info[:content], info[:tags],info[:language])
24
+ end
25
+ end
26
+
27
+ def delete(id)
28
+ Server.open_session(@key) do |server|
29
+ server.call("snippet.delete", @key, id)
30
+ end
31
+ end
32
+
33
+ def find_all_entries
34
+ entries=[]
35
+ Server.open_session(@key) do |server|
36
+ hashes = server.call("snippet.list", @key)
37
+ hashes.each do |row|
38
+ row[:eid]=row['id']# we must use eid since id collides with Object.id
39
+ entries << OpenStruct.new(row)
40
+ end
41
+ end
42
+ return entries
43
+ end
44
+ end
data/lib/snipplr.rb ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/ruby
2
+ require 'interface/bash_api'
3
+ if __FILE__ == $0 # we want to excute this only when directly used
4
+ BashAPI.new.process
5
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snipplr
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 8
9
+ version: 0.0.8
10
+ platform: ruby
11
+ authors:
12
+ - Ronen Narkis
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-01 00:00:00 +02:00
18
+ default_executable: snipplr
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: optiflag
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 7
31
+ version: "0.7"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Snipplr gem is intended to be used as command line api to publish delete and view code entires in snipplr.com
35
+ email: narkisr@gmail.com
36
+ executables:
37
+ - snipplr
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - lib/model/snipplr_entry.rb
44
+ - lib/model/server.rb
45
+ - lib/snipplr.rb
46
+ - lib/interface/bash_api.rb
47
+ - bin/snipplr
48
+ has_rdoc: true
49
+ homepage: http://github.com/narkisr/snipplr-ruby-gem
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.7
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Command line interface for snipplr
80
+ test_files: []
81
+