glsnip 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/glsnip/base.rb +71 -7
- data/lib/glsnip/formatter.rb +11 -7
- data/lib/glsnip/snippets.rb +16 -5
- data/lib/glsnip/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2c6439d1e7452c83afae32942a7960e9fcc42ee6d04f1f55d0608f1dc106a3a
|
4
|
+
data.tar.gz: 9c4851465cd6b27d01dbf0fda784299f85b8b2ba1daf5468471c71e515b9d2c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed2f22f56885ec4c4c84156bc64e60ad73ff09318bf72023722d625f938be9272a45e14f5cd15ef3f28a75742cf91625cda598479c9c78755724ccc9bb751ea9
|
7
|
+
data.tar.gz: af9dc10b0a3a337c18a94f665e7ac8af8e815a5e27390011e2ebbc0dde5a9dcfa521aade2227a1be023b7429481069a1b515c98153973ab362cc9bc3f5a1c3a8
|
data/lib/glsnip/base.rb
CHANGED
@@ -27,7 +27,7 @@
|
|
27
27
|
|
28
28
|
require 'optparse'
|
29
29
|
require 'English'
|
30
|
-
require '
|
30
|
+
require 'json'
|
31
31
|
|
32
32
|
require 'glsnip/version'
|
33
33
|
require 'glsnip/snippets'
|
@@ -47,16 +47,16 @@ optparse = OptionParser.new do |opts|
|
|
47
47
|
gl_response = Glsnip::Snippets.list
|
48
48
|
|
49
49
|
if gl_response.success?
|
50
|
-
puts Glsnip::Formatter.list(gl_response.body)
|
50
|
+
puts Glsnip::Formatter.list(JSON.parse(gl_response.body))
|
51
51
|
else
|
52
|
-
puts Glsnip::Formatter.error(gl_response.body)
|
52
|
+
puts Glsnip::Formatter.error(JSON.parse(gl_response.body))
|
53
53
|
end
|
54
54
|
|
55
55
|
exit
|
56
56
|
end
|
57
57
|
|
58
|
-
opts.on('-
|
59
|
-
puts 'File does not exist' && exit unless File.exist?(file_name)
|
58
|
+
opts.on('-c', '--create FILE', 'Creates a new snippet given a file') do |file_name|
|
59
|
+
puts Glsnip::Formatter.error('File does not exist') && exit unless File.exist?(file_name)
|
60
60
|
|
61
61
|
print 'Enter a title for the snippet -> '
|
62
62
|
title = gets.chomp
|
@@ -72,9 +72,73 @@ optparse = OptionParser.new do |opts|
|
|
72
72
|
visibility: visibility)
|
73
73
|
|
74
74
|
if gl_response.success?
|
75
|
-
puts Glsnip::Formatter.create_success(gl_response.body)
|
75
|
+
puts Glsnip::Formatter.create_success(JSON.parse(gl_response.body))
|
76
76
|
else
|
77
|
-
puts Glsnip::Formatter.error(gl_response.body)
|
77
|
+
puts Glsnip::Formatter.error(JSON.parse(gl_response.body))
|
78
|
+
end
|
79
|
+
|
80
|
+
exit
|
81
|
+
end
|
82
|
+
|
83
|
+
opts.on('-r', '--read ID', 'Displays the contents of a snippet given an ID') do |snippet_id|
|
84
|
+
gl_response = Glsnip::Snippets.read_raw(snippet_id)
|
85
|
+
|
86
|
+
if gl_response.success?
|
87
|
+
puts gl_response.body
|
88
|
+
else
|
89
|
+
puts Glsnip::Formatter.error(JSON.parse(gl_response.body))
|
90
|
+
end
|
91
|
+
|
92
|
+
exit
|
93
|
+
end
|
94
|
+
|
95
|
+
opts.on('-u', '--update ID', 'Updates a snippet given an ID') do |snippet_id|
|
96
|
+
gl_read_response = Glsnip::Snippets.read(snippet_id)
|
97
|
+
puts Glsnip::Formatter.error(JSON.parse(gl_read_response.body)) && exit unless gl_read_response.success?
|
98
|
+
|
99
|
+
snippet_json = JSON.parse(gl_read_response.body)
|
100
|
+
|
101
|
+
update_params = {}
|
102
|
+
|
103
|
+
print "Current title: \"#{snippet_json['title']}\". Change? (y/n) -> "
|
104
|
+
if gets.chomp.casecmp?('y')
|
105
|
+
print 'Enter a title for the snippet -> '
|
106
|
+
update_params['title'] = gets.chomp
|
107
|
+
end
|
108
|
+
|
109
|
+
print "Current visibility: \"#{snippet_json['visibility']}\". Change? (y/n) -> "
|
110
|
+
if gets.chomp.casecmp?('y')
|
111
|
+
print 'Is this snippet private? (y/n) -> '
|
112
|
+
update_params['visibility'] = gets.chomp.casecmp?('y') ? 'private' : 'public'
|
113
|
+
end
|
114
|
+
|
115
|
+
print "Current file: \"#{snippet_json['file_name']}\". Change? (y/n) -> "
|
116
|
+
if gets.chomp.casecmp?('y')
|
117
|
+
print 'Path to file -> '
|
118
|
+
file_name = gets.chomp
|
119
|
+
puts Glsnip::Formatter.error('File does not exist') && exit unless File.exist?(file_name)
|
120
|
+
update_params['file_name'] = File.split(file_name).last
|
121
|
+
update_params['content'] = File.read(file_name)
|
122
|
+
end
|
123
|
+
|
124
|
+
gl_update_response = Glsnip::Snippets.update(snippet_id, update_params)
|
125
|
+
|
126
|
+
if gl_update_response.success?
|
127
|
+
puts Glsnip::Formatter.update_success(JSON.parse(gl_update_response.body))
|
128
|
+
else
|
129
|
+
puts Glsnip::Formatter.error(JSON.parse(gl_update_response.body))
|
130
|
+
end
|
131
|
+
|
132
|
+
exit
|
133
|
+
end
|
134
|
+
|
135
|
+
opts.on('-d', '--delete ID', 'Deletes a snippet given an ID') do |snippet_id|
|
136
|
+
gl_response = Glsnip::Snippets.delete(snippet_id)
|
137
|
+
|
138
|
+
if gl_response.success?
|
139
|
+
puts Glsnip::Formatter.delete_success(snippet_id)
|
140
|
+
else
|
141
|
+
puts Glsnip::Formatter.error(JSON.parse(gl_response.body))
|
78
142
|
end
|
79
143
|
|
80
144
|
exit
|
data/lib/glsnip/formatter.rb
CHANGED
@@ -18,7 +18,6 @@
|
|
18
18
|
# along with glsnip. If not, see <http://www.gnu.org/licenses/>.
|
19
19
|
|
20
20
|
require 'colorize'
|
21
|
-
require 'json'
|
22
21
|
|
23
22
|
module Glsnip
|
24
23
|
##
|
@@ -29,13 +28,11 @@ module Glsnip
|
|
29
28
|
#
|
30
29
|
# Formats execution results and prints to the terminal
|
31
30
|
class Formatter
|
32
|
-
def self.error(
|
33
|
-
body = JSON.parse(json)
|
31
|
+
def self.error(body)
|
34
32
|
"Failure: #{body['message']}".colorize(:red)
|
35
33
|
end
|
36
34
|
|
37
|
-
def self.list(
|
38
|
-
body = JSON.parse(json)
|
35
|
+
def self.list(body)
|
39
36
|
snippets = []
|
40
37
|
body.each do |snippet|
|
41
38
|
snippets.push "\"#{snippet['title']}\" (#{snippet['file_name']} | #{snippet['id']}): #{snippet['web_url']}".colorize(:light_blue)
|
@@ -43,9 +40,16 @@ module Glsnip
|
|
43
40
|
snippets
|
44
41
|
end
|
45
42
|
|
46
|
-
def self.create_success(
|
47
|
-
body = JSON.parse(json)
|
43
|
+
def self.create_success(body)
|
48
44
|
"Snippet \"#{body['title']}\" (#{body['file_name']} | #{body['id']}) successfully created: #{body['web_url']}".colorize(:green)
|
49
45
|
end
|
46
|
+
|
47
|
+
def self.update_success(body)
|
48
|
+
"Snippet #{body['id']} successfully updated: #{body['web_url']}".colorize(:green)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.delete_success(id)
|
52
|
+
"Snippet #{id} successfully deleted".colorize(:green)
|
53
|
+
end
|
50
54
|
end
|
51
55
|
end
|
data/lib/glsnip/snippets.rb
CHANGED
@@ -39,18 +39,29 @@ module Glsnip
|
|
39
39
|
req.send
|
40
40
|
end
|
41
41
|
|
42
|
-
def self.read
|
43
|
-
req = GetRequest.new(ENDPOINT)
|
42
|
+
def self.read(id)
|
43
|
+
req = Glsnip::GetRequest.new(ENDPOINT + "/#{id}")
|
44
|
+
req.send
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.read_raw(id)
|
48
|
+
req = Glsnip::GetRequest.new(ENDPOINT + "/#{id}/raw")
|
44
49
|
req.send
|
45
50
|
end
|
46
51
|
|
47
52
|
def self.create(params = {})
|
48
|
-
req = PostRequest.new(ENDPOINT, params)
|
53
|
+
req = Glsnip::PostRequest.new(ENDPOINT, params)
|
49
54
|
req.send
|
50
55
|
end
|
51
56
|
|
52
|
-
def self.update
|
57
|
+
def self.update(id, params = {})
|
58
|
+
req = Glsnip::PutRequest.new(ENDPOINT + "/#{id}", params)
|
59
|
+
req.send
|
60
|
+
end
|
53
61
|
|
54
|
-
def self.delete
|
62
|
+
def self.delete(id)
|
63
|
+
req = Glsnip::DeleteRequest.new(ENDPOINT + "/#{id}")
|
64
|
+
req.send
|
65
|
+
end
|
55
66
|
end
|
56
67
|
end
|
data/lib/glsnip/version.rb
CHANGED