glsnip 0.0.2 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf475a86b786f39f02332a4ac5354b5850d97426f40996df210cc97423d383a8
4
- data.tar.gz: 118f452ce53a154e4553efc3f401b42d4ef70ecfd976d18218761d4977b8799c
3
+ metadata.gz: d2c6439d1e7452c83afae32942a7960e9fcc42ee6d04f1f55d0608f1dc106a3a
4
+ data.tar.gz: 9c4851465cd6b27d01dbf0fda784299f85b8b2ba1daf5468471c71e515b9d2c0
5
5
  SHA512:
6
- metadata.gz: b9cabb1c6f9ac74f3f8a00ed77ffcd18bbfe57ece4aa716ce349d5e4418879fdcc7a5fa6419a1b94f91b2e909bf855bb341a6c5c1d15b9ef902d7e21ee6afcbc
7
- data.tar.gz: 2582132c604d951929fb983774fd48c50b14e939486add278aab3d587b770ef8b23b6fdf0191cb1d6b2cf824af72addba3efd14d62d11a835ec95dffc3b927f3
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 'colorize'
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('-n', '--new FILE', 'Creates a new snippet for a user') do |file_name|
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
@@ -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(json)
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(json)
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(json)
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
@@ -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; end
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; end
62
+ def self.delete(id)
63
+ req = Glsnip::DeleteRequest.new(ENDPOINT + "/#{id}")
64
+ req.send
65
+ end
55
66
  end
56
67
  end
@@ -26,5 +26,5 @@
26
26
  # Module for namespacing application classes.
27
27
  module Glsnip
28
28
  # Program version number
29
- VERSION = '0.0.2'
29
+ VERSION = '0.1.0'
30
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glsnip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Davis