narkisr-snipplr 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/snipplr ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/ruby
2
+ require 'interface/bash_api'
3
+ begin
4
+ BashAPI.new.process
5
+ rescue =>msg # ommiting the exception catches them all
6
+ FileUtils.mkdir(ENV['HOME']+'/.snipplr/') if !File.exists?(ENV['HOME']+'/.snipplr/')
7
+ File.open(ENV['HOME']+'/.snipplr/errors','w+').puts msg
8
+ end
@@ -0,0 +1,94 @@
1
+ require "model/snipplr_entry"
2
+ require 'rubygems'
3
+ require 'optiflag'
4
+ require 'fileutils'
5
+ META_DESC=<<END
6
+
7
+ The meta data of a new entry in the following form:
8
+ ":title=>'Entry title',tags:=>'C Ruby',:lang=>'c-plus-plus',:content=>'x=1'".
9
+ The content key is optional and will be piped in if not present (by using the '<' opertaor).
10
+ Lang is the language name found in Snipplr’s URLs, For example “javascript” or “c-plus-plus”.
11
+ END
12
+
13
+ class Hash
14
+ def keys_to_sym!
15
+ each_pair { |key,value| self.[]=(key.to_sym,value) }
16
+ end
17
+ end
18
+
19
+ module Snipplr extend OptiFlagSet
20
+ optional_flag 'key' do# it will be possible to place an api_key file in user home instead
21
+ description 'API key (if not provided it will be taken from /home/./snipplr/api_key).'
22
+ end
23
+ optional_flag 'd' do # delete
24
+ description 'Delete an entry with an id number: snipplr -d 123.'
25
+ end
26
+ optional_flag 'g' do# get
27
+ description 'Get the content of an entry and print it out: snipplr -g 123.'
28
+ end
29
+
30
+ optional_flag 'meta' do
31
+ description META_DESC
32
+ validates_against do |f,errors|
33
+ begin
34
+ (hash=eval('{'+f.value+'}'))
35
+ hash.keys_to_sym!# the user may insert strings and not symbols
36
+ missing_value=[:title ,:tags ,:language].find{|sym| !hash.has_key?(sym)}
37
+ errors<< "You must pass #{missing_value} value in the meta values" if missing_value
38
+ rescue SyntaxError => e
39
+ errors<< 'meta tag is not well formated'
40
+ end
41
+ end
42
+ end
43
+
44
+ character_flag :c do
45
+ description "Create a new entry: snipplr -c -meta (see the meta description)."
46
+ validates_against do |f,errors|
47
+ errors<< 'you must pass in the meta data values of the new entry' if !ARGV.flags.meta
48
+ end
49
+ end
50
+
51
+ character_flag :l do# list
52
+ description 'List all the existing entries (the format is id and title): snipplr -l.'
53
+ end
54
+ and_process!
55
+ end
56
+
57
+ class BashAPI
58
+ attr_reader :entry
59
+ def process
60
+ @f = ARGV.flags
61
+ FileUtils.mkdir(ENV['HOME']+'/.snipplr') if !File.exist?(ENV['HOME']+'/.snipplr')
62
+ key=(@f.key ? @f.key : (IO.read(ENV['HOME']+'/.snipplr/api_key')).chomp!)
63
+ @entry=SnipplrEntry.new(key)
64
+ @entry.find_all_entries.each{|e| puts e.eid+' '+e.title} if @f.l
65
+ @meta=eval('{'+@f.meta+'}') if @f.meta
66
+ delete_entry if @f.d
67
+ post_request if @f.c
68
+ print_entry if @f.g
69
+ puts 'Use the -h option for help' if !@f.values.find { |val| val}
70
+ end
71
+
72
+ private
73
+ def get_content
74
+ Timeout.timeout(3,Exception.new("No content was piped in (timed out after 3 sec)")) do
75
+ content=''
76
+ $stdin.each {|line| content<< line}
77
+ return content
78
+ end
79
+ end
80
+
81
+ def post_request
82
+ @meta[:content]=get_content if !@meta[:content]
83
+ @entry.post_entry(@meta)
84
+ end
85
+
86
+ def print_entry
87
+ puts @entry.get_content(@f.g).source
88
+ end
89
+
90
+ def delete_entry
91
+ @entry.delete(@f.d)
92
+ end
93
+
94
+ 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,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: narkisr-snipplr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ronen Narkis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-05 00:00:00 -07:00
13
+ default_executable: snipplr
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: optiflag
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.6.5
23
+ version:
24
+ description: Snipplr gem is intended to be used as command line api to publish delete and view code entires in snipplr.com
25
+ email: narkisr@gmail.com
26
+ executables:
27
+ - snipplr
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - lib/model/snipplr_entry.rb
34
+ - lib/model/server.rb
35
+ - lib/snipplr.rb
36
+ - lib/interface/bash_api.rb
37
+ has_rdoc: false
38
+ homepage: http://github.com/narkisr/snipplr-ruby-gem
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Command line interface for snipplr
63
+ test_files: []
64
+