basics_edit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/basics_edit +215 -0
  2. metadata +65 -0
data/bin/basics_edit ADDED
@@ -0,0 +1,215 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'open-uri'
4
+ require 'trollop'
5
+ require 'json'
6
+ require 'rainbow'
7
+ require 'openssl'
8
+ require 'uri'
9
+ require 'net/http'
10
+ require 'base64'
11
+ require 'tempfile'
12
+ require 'redcarpet'
13
+ require 'console_renderer'
14
+
15
+ class Be
16
+
17
+ CATEGORIES = JSON.parse(open("http://basics.adit.io/categories").read).map { |cat| cat["name"] }
18
+
19
+ attr_accessor :opts
20
+
21
+ def initialize
22
+ @opts = Trollop::options do
23
+ opt :list, "See a list of available categories."
24
+ opt :edit, "Edit a tip"
25
+ opt :delete, "Delete a tip"
26
+ opt :new, "Create a new tip (default if no option is chosen)"
27
+
28
+ opt :id, "Specify a tip id to edit or delete", :type => String
29
+ opt :category, "Specify category (for new).", :type => String
30
+ opt :title, "Specify title (for new).", :type => String
31
+ opt :text, "Specify text on the command line (or use --file) (for new).", :type => String
32
+ opt :file, "Specify a file containing your text (or use --text) (for new).", :type => String
33
+ end
34
+
35
+ list if opts[:list]
36
+ edit if opts[:edit]
37
+ delete if opts[:delete]
38
+ new
39
+ end
40
+
41
+ def log(x)
42
+ puts x.color(:blue)
43
+ end
44
+
45
+ def err(x)
46
+ puts x.color(:red)
47
+ end
48
+
49
+ def check_cat(cat, x="")
50
+ unless CATEGORIES.include? cat
51
+ err "PLEASE ENTER A VALID CATEGORY. AVAILABLE CATEGORIES:\n#{CATEGORIES.inspect}"
52
+ puts x
53
+ exit
54
+ end
55
+ end
56
+
57
+ def list
58
+ puts "AVAILABLE CATEGORIES:\n#{CATEGORIES.inspect}"
59
+ exit
60
+ end
61
+
62
+ def get_tip id
63
+ if !id
64
+ err "PLEASE SPECIFY A TIP ID TO EDIT OR DELETE."
65
+ exit
66
+ end
67
+ JSON.parse(open("http://basics.adit.io/show/" + opts[:id]).read)
68
+ end
69
+
70
+ def delete
71
+ tip = get_tip opts[:id]
72
+ preview(tip["category"], tip["title"], tip["text"], "YOU ARE DELETING:")
73
+
74
+ log "REALLY DELETE THIS? (y/n):"
75
+ res = STDIN.readline.chomp
76
+ if res =~ /^y/
77
+ log "DELETING."
78
+ res = Net::HTTP.post_form(URI.parse("http://basics.adit.io/delete/"), :id => opts[:id].to_i, :auth => auth)
79
+ puts res.body
80
+ end
81
+ exit
82
+ end
83
+
84
+ def edit
85
+ tip = get_tip opts[:id]
86
+ category, title, text = live_edit(tip["category"], tip["title"], tip["text"])
87
+ preview_and_push(category, title, text, tip["id"])
88
+ exit
89
+ end
90
+
91
+ def live_edit(category, title, text)
92
+ tmp = Tempfile.new("newb")
93
+
94
+ s=<<-EOF
95
+ >CATEGORY (FROM #{CATEGORIES.join(" | ")}):
96
+ #{category}
97
+
98
+ >TITLE
99
+ #{title}
100
+
101
+ >TEXT
102
+ #{text}
103
+ EOF
104
+
105
+ tmp.write(s)
106
+ tmp.flush
107
+ `mvim -f #{tmp.path}`
108
+
109
+ edited = File.open(tmp.path).readlines
110
+
111
+ category = ""
112
+ title = ""
113
+ text = ""
114
+
115
+ stage = nil
116
+ edited.each do |line|
117
+ stage = "category" if line =~ /^>CATEGORY/
118
+ stage = "title" if line =~ /^>TITLE/
119
+ stage = "text" if line =~ /^>TEXT/
120
+ next if line =~ /^>(TEXT|TITLE|CATEGORY)/
121
+
122
+ case stage
123
+ when "category"
124
+ category += line
125
+ when "title"
126
+ title += line
127
+ when "text"
128
+ text += line
129
+ else
130
+ err "ERROR WHILE LIVE EDITING: INVALID CATEGORY. POSSIBLE CAUSE: BLANK LINE ON TOP OF FILE."
131
+ exit
132
+ end
133
+ end
134
+ tmp.close
135
+ category.gsub!("\n", "")
136
+ title.gsub!("\n", "")
137
+ check_cat category, text
138
+ [category, title, text.chomp]
139
+ end
140
+
141
+ def preview(category, title, text, msg="WHAT YOU'VE GOT:")
142
+ m = Redcarpet::Markdown.new(ConsoleRenderer)
143
+ puts
144
+ log msg
145
+ puts
146
+ log "CATEGORY: "
147
+ puts category
148
+ puts
149
+ log "TITLE:"
150
+ puts title
151
+ puts
152
+ log "TEXT:"
153
+ puts m.render(text)
154
+ puts
155
+ end
156
+
157
+ def preview_and_push(category, title, text, id=-1)
158
+ preview(category, title, text)
159
+ log "LOOKS GOOD? (y/n):"
160
+ res = STDIN.readline.chomp
161
+ if res.downcase =~ /^y/
162
+ push(category, title, text, id)
163
+ else
164
+ new_category, new_title, new_text = live_edit(category, title, text)
165
+ preview_and_push(new_category, new_title, new_text, id)
166
+ end
167
+ end
168
+
169
+ def push(category, title, text, id)
170
+ log "Cool! Pushing tip."
171
+
172
+ log "Username?"
173
+ name = STDIN.readline.chomp
174
+
175
+ log "Password?"
176
+ password = STDIN.readline.chomp
177
+
178
+ res = Net::HTTP.post_form(URI.parse("http://basics.adit.io/create/"), :category=>category, :title => title, :text => text, :name => name, :password => password, :id => id)
179
+ puts res.body
180
+ end
181
+
182
+ def new
183
+ if opts[:category]
184
+ cat = opts[:category]
185
+ check_cat cat
186
+ else
187
+ puts "Please enter a category (#{CATEGORIES.join(" | ")}):"
188
+ cat = STDIN.readline.chomp
189
+ check_cat cat
190
+ end
191
+
192
+ if opts[:title]
193
+ title = opts[:title]
194
+ else
195
+ puts "Please enter a title:"
196
+ title = STDIN.readline.chomp
197
+ end
198
+
199
+ if opts[:text]
200
+ text = opts[:text]
201
+ elsif opts[:file]
202
+ text = File.open(opts[:file]).read
203
+ else
204
+ puts "Please enter your content (markdown allowed) or a file name (Ctrl-D when done):"
205
+ text = STDIN.read.chomp
206
+ if File.exist?(text)
207
+ log "reading file."
208
+ text = File.open(text).read
209
+ end
210
+ end
211
+ preview_and_push cat, title, text
212
+ end
213
+ end
214
+
215
+ Be.new
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: basics_edit
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Aditya Bhargava
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-04-28 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Edit basics
22
+ email: bluemangroupie@gmail.com
23
+ executables:
24
+ - basics_edit
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - bin/basics_edit
31
+ homepage:
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.25
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Edit basics
64
+ test_files: []
65
+