Module: Jist
Overview
It just gists.
Defined Under Namespace
Modules: Error
Constant Summary collapse
- VERSION =
'1.2.0'
- CLIPBOARD_COMMANDS =
A list of clipboard commands with copy and paste support.
{ 'xclip' => 'xclip -o', 'xsel' => 'xsel -o', 'pbcopy' => 'pbpaste', 'putclip' => 'getclip' }
Instance Method Summary collapse
-
#clipboard_command(action) ⇒ String
Get the command to use for the clipboard action.
-
#copy(content) ⇒ Object
Copy a string to the clipboard.
-
#gist(content, options = {}) ⇒ Hash
Upload a gist to gist.github.com.
-
#http(request) ⇒ Net::HTTPResponse
Run an HTTP operation against api.github.com.
-
#http_connection ⇒ Net::HTTP
Return HTTP connection.
-
#login! ⇒ Object
Log the user into jist.
-
#multi_gist(files, options = {}) ⇒ Hash
Upload a gist to gist.github.com.
-
#on_success(body, options = {}) ⇒ Hash
Called after an HTTP response to gist to perform post-processing.
-
#open(url) ⇒ Object
Open a URL in a browser.
-
#paste ⇒ Object
Get a string from the clipboard.
-
#shorten(url) ⇒ String
Convert long github urls into shotr git.io ones.
-
#which(cmd, path = ) ⇒ String
Find command from PATH environment.
Instance Method Details
#clipboard_command(action) ⇒ String
Get the command to use for the clipboard action.
256 257 258 259 260 261 262 |
# File 'lib/jist.rb', line 256 def clipboard_command(action) command = CLIPBOARD_COMMANDS.keys.detect do |cmd| which cmd end raise "Could not find copy command, tried: #{CLIPBOARD_COMMANDS}" unless command action == :copy ? command : CLIPBOARD_COMMANDS[command] end |
#copy(content) ⇒ Object
Copy a string to the clipboard.
This method was heavily inspired by defunkt’s Gist#copy,
221 222 223 224 |
# File 'lib/jist.rb', line 221 def copy(content) IO.popen(clipboard_command(:copy), 'r+') { |clip| clip.print content } raise "Copying to clipboard failed" unless paste == content end |
#gist(content, options = {}) ⇒ Hash
Upload a gist to gist.github.com
41 42 43 44 |
# File 'lib/jist.rb', line 41 def gist(content, = {}) filename = [:filename] || "a.rb" multi_gist({filename => content}, ) end |
#http(request) ⇒ Net::HTTPResponse
Run an HTTP operation against api.github.com
190 191 192 193 194 195 196 |
# File 'lib/jist.rb', line 190 def http(request) http_connection().start do |http| http.request request end rescue Timeout::Error raise "Could not connect to https://api.github.com/" end |
#http_connection ⇒ Net::HTTP
Return HTTP connection
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/jist.rb', line 170 def http_connection env = ENV['http_proxy'] || ENV['HTTP_PROXY'] connection = if env uri = URI(env) proxy_host, proxy_port = uri.host, uri.port Net::HTTP::Proxy(proxy_host, proxy_port).new("api.github.com", 443) else Net::HTTP.new("api.github.com", 443) end connection.use_ssl = true connection.verify_mode = OpenSSL::SSL::VERIFY_NONE connection.open_timeout = 10 connection.read_timeout = 10 connection end |
#login! ⇒ Object
Log the user into jist.
This method asks the user for a username and password, and tries to obtain and OAuth2 access token, which is then stored in ~/.jist
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/jist.rb', line 131 def login! puts "Obtaining OAuth2 access_token from github." print "Github username: " username = $stdin.gets.strip print "Github password: " password = begin `stty -echo` rescue nil $stdin.gets.strip ensure `stty echo` rescue nil end puts "" request = Net::HTTP::Post.new("/authorizations") request.body = JSON.dump({ :scopes => [:gist], :note => "The jist gem", :note_url => "https://github.com/ConradIrwin/jist" }) request.content_type = 'application/json' request.basic_auth(username, password) response = http(request) if Net::HTTPCreated === response File.open(File.("~/.jist"), 'w') do |f| f.write JSON.parse(response.body)['token'] end puts "Success! https://github.com/settings/applications" else raise "Got #{response.class} from gist: #{response.body}" end rescue => e raise e.extend Error end |
#multi_gist(files, options = {}) ⇒ Hash
Upload a gist to gist.github.com
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/jist.rb', line 64 def multi_gist(files, ={}) json = {} json[:description] = [:description] if [:description] json[:public] = !![:public] json[:files] = {} files.each_pair do |(name, content)| raise "Cannot gist empty files" if content.to_s.strip == "" json[:files][File.basename(name)] = {:content => content} end existing_gist = [:update].to_s.split("/").last if [:anonymous] access_token = nil else access_token = ([:access_token] || File.read(File.("~/.jist")) rescue nil) end url = "/gists" url << "/" << CGI.escape(existing_gist) if existing_gist.to_s != '' url << "?access_token=" << CGI.escape(access_token) if access_token.to_s != '' request = Net::HTTP::Post.new(url) request.body = JSON.dump(json) request.content_type = 'application/json' retried = false begin response = http(request) if Net::HTTPSuccess === response on_success(response.body, ) else raise "Got #{response.class} from gist: #{response.body}" end rescue => e raise if retried retried = true retry end rescue => e raise e.extend Error end |
#on_success(body, options = {}) ⇒ Hash
Called after an HTTP response to gist to perform post-processing.
204 205 206 207 208 209 210 211 212 |
# File 'lib/jist.rb', line 204 def on_success(body, ={}) json = JSON.parse(body) json['html_url'] = shorten(json['html_url']) if [:shorten] Jist.copy(json['html_url']) if [:copy] Jist.open(json['html_url']) if [:open] json end |
#open(url) ⇒ Object
Open a URL in a browser.
This method was heavily inspired by defunkt’s Gist#open,
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/jist.rb', line 271 def open(url) command = if ENV['BROWSER'] ENV['BROWSER'] elsif RUBY_PLATFORM =~ /darwin/ 'open' elsif RUBY_PLATFORM =~ /linux/ 'sensible-browser' elsif ENV['OS'] == 'Windows_NT' || RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw|wince/i 'start ""' else raise "Could not work out how to use a browser." end `#{command} #{url}` end |
#paste ⇒ Object
Get a string from the clipboard.
230 231 232 |
# File 'lib/jist.rb', line 230 def paste `#{clipboard_command(:paste)}` end |
#shorten(url) ⇒ String
Convert long github urls into shotr git.io ones
114 115 116 117 118 119 120 121 122 |
# File 'lib/jist.rb', line 114 def shorten(url) response = Net::HTTP.post_form(URI("http://git.io/"), :url => url) case response.code when "201" response['Location'] else url end end |
#which(cmd, path = ) ⇒ String
Find command from PATH environment.
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/jist.rb', line 239 def which(cmd, path=ENV['PATH']) if RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|bccwin|cygwin/ path.split(File::PATH_SEPARATOR).each {|dir| f = File.join(dir, cmd+".exe") return f if File.executable?(f) && !File.directory?(f) } nil else return system("which #{cmd} > /dev/null 2>&1") end end |