Module: Gist

Extended by:
Gist
Included in:
Gist
Defined in:
lib/gist.rb

Overview

It just gists.

Defined Under Namespace

Modules: Error Classes: ClipboardError

Constant Summary collapse

VERSION =
'4.1.3'
CLIPBOARD_COMMANDS =

A list of clipboard commands with copy and paste support.

{
  'xclip'   => 'xclip -o',
  'xsel -i'    => 'xsel -o',
  'pbcopy'  => 'pbpaste',
  'putclip' => 'getclip'
}
GITHUB_API_URL =
URI("https://api.github.com/")
GIT_IO_URL =
URI("http://git.io")
GITHUB_BASE_PATH =
""
GHE_BASE_PATH =
"/api/v3"
URL_ENV_NAME =
"GITHUB_URL"
USER_AGENT =
"gist/#{VERSION} (Net::HTTP, #{RUBY_DESCRIPTION})"

Instance Method Summary collapse

Instance Method Details

#api_urlObject

Get the API URL



352
353
354
# File 'lib/gist.rb', line 352

def api_url
  ENV.key?(URL_ENV_NAME) ? URI(ENV[URL_ENV_NAME]) : GITHUB_API_URL
end

#auth_token_fileObject



356
357
358
359
360
361
362
# File 'lib/gist.rb', line 356

def auth_token_file
  if ENV.key?(URL_ENV_NAME)
    File.expand_path "~/.gist.#{ENV[URL_ENV_NAME].gsub(/[^a-z.]/, '')}"
  else
    File.expand_path "~/.gist"
  end
end

#base_pathObject

Get the API base path



347
348
349
# File 'lib/gist.rb', line 347

def base_path
  ENV.key?(URL_ENV_NAME) ? GHE_BASE_PATH : GITHUB_BASE_PATH
end

#clipboard_command(action) ⇒ String

Get the command to use for the clipboard action.

Parameters:

  • action (Symbol)

    either :copy or :paste

Returns:

  • (String)

    the command to run

Raises:



306
307
308
309
310
311
312
313
314
315
# File 'lib/gist.rb', line 306

def clipboard_command(action)
  command = CLIPBOARD_COMMANDS.keys.detect do |cmd|
    which cmd
  end
  raise ClipboardError, <<-EOT unless command
Could not find copy command, tried:
  #{CLIPBOARD_COMMANDS.values.join(' || ')}
  EOT
  action == :copy ? command : CLIPBOARD_COMMANDS[command]
end

#copy(content) ⇒ Object

Copy a string to the clipboard.

Parameters:

  • content (String)

Raises:

  • (Gist::Error)

    if no clipboard integration could be found



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/gist.rb', line 260

def copy(content)
  IO.popen(clipboard_command(:copy), 'r+') { |clip| clip.print content }

  unless paste == content
    message = 'Copying to clipboard failed.'

    if ENV["TMUX"] && clipboard_command(:copy) == 'pbcopy'
      message << "\nIf you're running tmux on a mac, try http://robots.thoughtbot.com/post/19398560514/how-to-copy-and-paste-with-tmux-on-mac-os-x"
    end

    raise Error, message
  end
rescue Error => e
  raise ClipboardError, e.message + "\nAttempted to copy: #{content}"
end

#gist(content, options = {}) ⇒ Object

Upload a gist to gist.github.com

Parameters:

  • content (String)

    the code you’d like to gist

  • options (Hash) (defaults to: {})

    more detailed options, see the documentation for #multi_gist

See Also:



50
51
52
53
# File 'lib/gist.rb', line 50

def gist(content, options = {})
  filename = options[:filename] || "a.rb"
  multi_gist({filename => content}, options)
end

#http(url, request) ⇒ Net::HTTPResponse

Run an HTTP operation

Parameters:

  • The (URI::HTTP)

    URI to which to connect

  • The (Net::HTTPRequest)

    request to make

Returns:

  • (Net::HTTPResponse)


220
221
222
223
224
225
226
227
228
# File 'lib/gist.rb', line 220

def http(url, request)
  request['User-Agent'] = USER_AGENT

  http_connection(url).start do |http|
    http.request request
  end
rescue Timeout::Error
  raise "Could not connect to #{api_url}"
end

#http_connection(uri) ⇒ Net::HTTP

Return HTTP connection

Parameters:

  • The (URI::HTTP)

    URI to which to connect

Returns:

  • (Net::HTTP)


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/gist.rb', line 198

def http_connection(uri)
  env = ENV['http_proxy'] || ENV['HTTP_PROXY']
  connection = if env
                 proxy = URI(env)
                 Net::HTTP::Proxy(proxy.host, proxy.port).new(uri.host, uri.port)
               else
                 Net::HTTP.new(uri.host, uri.port)
               end
  if uri.scheme == "https"
    connection.use_ssl = true
    connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  connection.open_timeout = 10
  connection.read_timeout = 10
  connection
end

#legacy_private_gister?Boolean

Returns:

  • (Boolean)


364
365
366
367
# File 'lib/gist.rb', line 364

def legacy_private_gister?
  return unless which('git')
  `git config --global gist.private` =~ /\Ayes|1|true|on\z/i
end

#login!(credentials = {}) ⇒ Object

Log the user into gist.

This method asks the user for a username and password, and tries to obtain and OAuth2 access token, which is then stored in ~/.gist

Parameters:

  • credentials (Hash) (defaults to: {})

    login details

Options Hash (credentials):

  • :username (String)
  • :password (String)

Raises:

See Also:



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/gist.rb', line 149

def login!(credentials={})
  puts "Obtaining OAuth2 access_token from github."
  print "GitHub username: "
  username = credentials[:username] || $stdin.gets.strip
  print "GitHub password: "
  password = credentials[:password] || begin
    `stty -echo` rescue nil
    $stdin.gets.strip
  ensure
    `stty echo` rescue nil
  end
  puts ""

  request = Net::HTTP::Post.new("#{base_path}/authorizations")
  request.body = JSON.dump({
    :scopes => [:gist],
    :note => "The gist gem",
    :note_url => "https://github.com/ConradIrwin/gist"
  })
  request.content_type = 'application/json'
  request.basic_auth(username, password)

  response = http(api_url, request)

  if Net::HTTPUnauthorized === response && response['X-GitHub-OTP']
    print "2-factor auth code: "
    twofa_code = $stdin.gets.strip
    puts ""

    request['X-GitHub-OTP'] = twofa_code
    response = http(api_url, request)
  end

  if Net::HTTPCreated === response
    File.open(auth_token_file, 'w', 0600) do |f|
      f.write JSON.parse(response.body)['token']
    end
    puts "Success! #{ENV[URL_ENV_NAME] || "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 = {}) ⇒ String, Hash

Upload a gist to gist.github.com

Parameters:

  • files (Hash)

    the code you’d like to gist: filename => content

  • options (Hash) (defaults to: {})

    more detailed options

Options Hash (options):

  • :description (String)

    the description

  • :public (Boolean) — default: false

    is this gist public

  • :anonymous (Boolean) — default: false

    is this gist anonymous

  • :access_token (String) — default: `File.read("~/.gist")`

    The OAuth2 access token.

  • :update (String)

    the URL or id of a gist to update

  • :copy (Boolean) — default: false

    Copy resulting URL to clipboard, if successful.

  • :open (Boolean) — default: false

    Open the resulting URL in a browser.

  • :output (Symbol) — default: :all

    The type of return value you’d like: :html_url gives a String containing the url to the gist in a browser :short_url gives a String contianing a git.io url that redirects to html_url :javascript gives a String containing a script tag suitable for embedding the gist :all gives a Hash containing the parsed json response from the server

Returns:

  • (String, Hash)

    the return value as configured by options

Raises:

See Also:



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
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gist.rb', line 77

def multi_gist(files, options={})
  json = {}

  json[:description] = options[:description] if options[:description]
  json[:public] = !!options[: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 = options[:update].to_s.split("/").last
  if options[:anonymous]
    access_token = nil
  else
    access_token = (options[:access_token] || File.read(auth_token_file) rescue nil)
  end

  url = "#{base_path}/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(api_url, request)
    if Net::HTTPSuccess === response
      on_success(response.body, options)
    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 = {}) ⇒ Object

Called after an HTTP response to gist to perform post-processing.

Parameters:

  • body (String)

    the text body from the github api

  • options (Hash) (defaults to: {})

    more detailed options, see the documentation for #multi_gist



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/gist.rb', line 235

def on_success(body, options={})
  json = JSON.parse(body)

  output = case options[:output]
           when :javascript
             %Q{<script src="#{json['html_url']}.js"></script>}
           when :html_url
             json['html_url']
           when :short_url
             shorten(json['html_url'])
           else
             json
           end

  Gist.copy(output.to_s) if options[:copy]
  Gist.open(json['html_url']) if options[:open]

  output
end

#open(url) ⇒ Object

Open a URL in a browser.

This method was heavily inspired by defunkt’s Gist#open,

Parameters:

  • url (String)

Raises:

  • (RuntimeError)

    if no browser integration could be found

See Also:



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/gist.rb', line 324

def open(url)
  command = if ENV['BROWSER']
              ENV['BROWSER']
            elsif RUBY_PLATFORM =~ /darwin/
              'open'
            elsif RUBY_PLATFORM =~ /linux/
              %w(
                sensible-browser
                firefox
                firefox-bin
              ).detect do |cmd|
                which cmd
              end
            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

#pasteObject

Get a string from the clipboard.

Parameters:

  • content (String)

Raises:

  • (Gist::Error)

    if no clipboard integration could be found



280
281
282
# File 'lib/gist.rb', line 280

def paste
  `#{clipboard_command(:paste)}`
end

#shorten(url) ⇒ String

Convert long github urls into short git.io ones

Parameters:

  • url (String)

Returns:

  • (String)

    shortened url, or long url if shortening fails



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/gist.rb', line 127

def shorten(url)
  request = Net::HTTP::Post.new("/")
  request.set_form_data(:url => url)
  response = http(GIT_IO_URL, request)
  case response.code
  when "201"
    response['Location']
  else
    url
  end
end

#should_be_public?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


369
370
371
372
373
374
375
# File 'lib/gist.rb', line 369

def should_be_public?(options={})
  if options.key? :private
    !options[:private]
  else
    !Gist.legacy_private_gister?
  end
end

#which(cmd, path = ) ⇒ String

Find command from PATH environment.

Parameters:

  • cmd (String)

    command name to find

  • options (String)

    PATH environment variable

Returns:

  • (String)

    the command found



289
290
291
292
293
294
295
296
297
298
299
# File 'lib/gist.rb', line 289

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