@@ -1314,297 +1314,6 @@ rescue LoadError
13141314 require File . join File . dirname ( File . dirname ( __FILE__ ) ) , 'vendor' , 'json.rb'
13151315end
13161316
1317- begin
1318-
1319- # The netrc library (https://github.com/heroku/netrc) is reproduced below, along
1320- # with its copyright and license:
1321- # - https://raw.githubusercontent.com/heroku/netrc/262ef111/LICENSE.md
1322- # - https://raw.githubusercontent.com/heroku/netrc/262ef111/lib/netrc.rb
1323-
1324- # The MIT License (MIT)
1325- #
1326- # Copyright (c) 2011-2014 [CONTRIBUTORS.md](https://github.com/geemus/netrc/blob/master/CONTRIBUTORS.md)
1327- #
1328- # Permission is hereby granted, free of charge, to any person obtaining a copy of
1329- # this software and associated documentation files (the "Software"), to deal in
1330- # the Software without restriction, including without limitation the rights to
1331- # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
1332- # the Software, and to permit persons to whom the Software is furnished to do so,
1333- # subject to the following conditions:
1334- #
1335- # The above copyright notice and this permission notice shall be included in all
1336- # copies or substantial portions of the Software.
1337- #
1338- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1339- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
1340- # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
1341- # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
1342- # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1343- # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1344-
1345- require 'rbconfig'
1346- require 'io/console'
1347-
1348- class Netrc
1349- VERSION = "0.11.0"
1350-
1351- # see http://stackoverflow.com/questions/4871309/what-is-the-correct-way-to-detect-if-ruby-is-running-on-windows
1352- WINDOWS = RbConfig ::CONFIG [ "host_os" ] =~ /mswin|mingw|cygwin/
1353- CYGWIN = RbConfig ::CONFIG [ "host_os" ] =~ /cygwin/
1354-
1355- def self . default_path
1356- File . join ( ENV [ 'NETRC' ] || home_path , netrc_filename )
1357- end
1358-
1359- def self . home_path
1360- home = Dir . respond_to? ( :home ) ? Dir . home : ENV [ 'HOME' ]
1361-
1362- if WINDOWS && !CYGWIN
1363- home ||= File . join ( ENV [ 'HOMEDRIVE' ] , ENV [ 'HOMEPATH' ] ) if ENV [ 'HOMEDRIVE' ] && ENV [ 'HOMEPATH' ]
1364- home ||= ENV [ 'USERPROFILE' ]
1365- # XXX: old stuff; most likely unnecessary
1366- home = home . tr ( "\\ " , "/" ) unless home . nil?
1367- end
1368-
1369- ( home && File . readable? ( home ) ) ? home : Dir . pwd
1370- rescue ArgumentError
1371- return Dir . pwd
1372- end
1373-
1374- def self . netrc_filename
1375- WINDOWS && !CYGWIN ? "_netrc" : ".netrc"
1376- end
1377-
1378- def self . config
1379- @config ||= { }
1380- end
1381-
1382- def self . configure
1383- yield ( self . config ) if block_given?
1384- self . config
1385- end
1386-
1387- def self . check_permissions ( path )
1388- perm = File . stat ( path ) . mode & 0777
1389- if perm != 0600 && !( WINDOWS ) && !( Netrc . config [ :allow_permissive_netrc_file ] )
1390- raise Error , "Permission bits for '#{ path } ' should be 0600, but are " +perm . to_s ( 8 )
1391- end
1392- end
1393-
1394- # Reads path and parses it as a .netrc file. If path doesn't
1395- # exist, returns an empty object. Decrypt paths ending in .gpg.
1396- def self . read ( path = default_path )
1397- check_permissions ( path )
1398- data = if path =~ /\. gpg$/
1399- decrypted = if ENV [ 'GPG_AGENT_INFO' ]
1400- `gpg --batch --quiet --decrypt #{ path } `
1401- else
1402- print "Enter passphrase for #{ path } : "
1403- STDIN . noecho do
1404- `gpg --batch --passphrase-fd 0 --quiet --decrypt #{ path } `
1405- end
1406- end
1407- if $?. success?
1408- decrypted
1409- else
1410- raise Error . new ( "Decrypting #{ path } failed." ) unless $?. success?
1411- end
1412- else
1413- File . read ( path )
1414- end
1415- new ( path , parse ( lex ( data . lines . to_a ) ) )
1416- rescue Errno ::ENOENT
1417- new ( path , parse ( lex ( [ ] ) ) )
1418- end
1419-
1420- class TokenArray < Array
1421- def take
1422- if length < 1
1423- raise Error , "unexpected EOF"
1424- end
1425- shift
1426- end
1427-
1428- def readto
1429- l = [ ]
1430- while length > 0 && ! yield ( self [ 0 ] )
1431- l << shift
1432- end
1433- return l . join
1434- end
1435- end
1436-
1437- def self . lex ( lines )
1438- tokens = TokenArray . new
1439- for line in lines
1440- content , comment = line . split ( /(\s *#.*)/m )
1441- content . each_char do |char |
1442- case char
1443- when /\s /
1444- if tokens . last && tokens . last [ -1 ..-1 ] =~ /\s /
1445- tokens . last << char
1446- else
1447- tokens << char
1448- end
1449- else
1450- if tokens . last && tokens . last [ -1 ..-1 ] =~ /\S /
1451- tokens . last << char
1452- else
1453- tokens << char
1454- end
1455- end
1456- end
1457- if comment
1458- tokens << comment
1459- end
1460- end
1461- tokens
1462- end
1463-
1464- def self . skip? ( s )
1465- s =~ /^\s /
1466- end
1467-
1468-
1469-
1470- # Returns two values, a header and a list of items.
1471- # Each item is a tuple, containing some or all of:
1472- # - machine keyword (including trailing whitespace+comments)
1473- # - machine name
1474- # - login keyword (including surrounding whitespace+comments)
1475- # - login
1476- # - password keyword (including surrounding whitespace+comments)
1477- # - password
1478- # - trailing chars
1479- # This lets us change individual fields, then write out the file
1480- # with all its original formatting.
1481- def self . parse ( ts )
1482- cur , item = [ ] , [ ]
1483-
1484- unless ts . is_a? ( TokenArray )
1485- ts = TokenArray . new ( ts )
1486- end
1487-
1488- pre = ts . readto { |t | t == "machine" || t == "default" }
1489-
1490- while ts . length > 0
1491- if ts [ 0 ] == 'default'
1492- cur << ts . take . to_sym
1493- cur << ''
1494- else
1495- cur << ts . take + ts . readto { |t | ! skip? ( t ) }
1496- cur << ts . take
1497- end
1498-
1499- if ts . include? ( 'login' )
1500- cur << ts . readto { |t | t == "login" } + ts . take + ts . readto { |t | ! skip? ( t ) }
1501- cur << ts . take
1502- end
1503-
1504- if ts . include? ( 'password' )
1505- cur << ts . readto { |t | t == "password" } + ts . take + ts . readto { |t | ! skip? ( t ) }
1506- cur << ts . take
1507- end
1508-
1509- cur << ts . readto { |t | t == "machine" || t == "default" }
1510-
1511- item << cur
1512- cur = [ ]
1513- end
1514-
1515- [ pre , item ]
1516- end
1517-
1518- def initialize ( path , data )
1519- @new_item_prefix = ''
1520- @path = path
1521- @pre , @data = data
1522-
1523- if @data && @data . last && :default == @data . last [ 0 ]
1524- @default = @data . pop
1525- else
1526- @default = nil
1527- end
1528- end
1529-
1530- attr_accessor :new_item_prefix
1531-
1532- def []( k )
1533- if item = @data . detect { |datum | datum [ 1 ] == k }
1534- Entry . new ( item [ 3 ] , item [ 5 ] )
1535- elsif @default
1536- Entry . new ( @default [ 3 ] , @default [ 5 ] )
1537- end
1538- end
1539-
1540- def []=( k , info )
1541- if item = @data . detect { |datum | datum [ 1 ] == k }
1542- item [ 3 ] , item [ 5 ] = info
1543- else
1544- @data << new_item ( k , info [ 0 ] , info [ 1 ] )
1545- end
1546- end
1547-
1548- def length
1549- @data . length
1550- end
1551-
1552- def delete ( key )
1553- datum = nil
1554- for value in @data
1555- if value [ 1 ] == key
1556- datum = value
1557- break
1558- end
1559- end
1560- @data . delete ( datum )
1561- end
1562-
1563- def each ( &block )
1564- @data . each ( &block )
1565- end
1566-
1567- def new_item ( m , l , p )
1568- [ new_item_prefix +"machine " , m , "\n login " , l , "\n password " , p , "\n " ]
1569- end
1570-
1571- def save
1572- if @path =~ /\. gpg$/
1573- e = IO . popen ( "gpg -a --batch --default-recipient-self -e" , "r+" ) do |gpg |
1574- gpg . puts ( unparse )
1575- gpg . close_write
1576- gpg . read
1577- end
1578- raise Error . new ( "Encrypting #{ @path } failed." ) unless $?. success?
1579- File . open ( @path , 'w' , 0600 ) { |file | file . print ( e ) }
1580- else
1581- File . open ( @path , 'w' , 0600 ) { |file | file . print ( unparse ) }
1582- end
1583- end
1584-
1585- def unparse
1586- @pre + @data . map do |datum |
1587- datum = datum . join
1588- unless datum [ -1 ..-1 ] == "\n "
1589- datum << "\n "
1590- else
1591- datum
1592- end
1593- end . join
1594- end
1595-
1596- Entry = Struct . new ( :login , :password ) do
1597- alias to_ary to_a
1598- end
1599-
1600- end
1601-
1602- class Netrc ::Error < ::StandardError
1603- end
1604- rescue LoadError
1605- require File . join File . dirname ( File . dirname ( __FILE__ ) ) , 'vendor' , 'netrc.rb'
1606- end
1607-
16081317# It just gists.
16091318module Gist
16101319 extend self
@@ -1613,14 +1322,14 @@ module Gist
16131322
16141323 # A list of clipboard commands with copy and paste support.
16151324 CLIPBOARD_COMMANDS = {
1616- 'pbcopy' => 'pbpaste' ,
16171325 'xclip' => 'xclip -o' ,
16181326 'xsel -i' => 'xsel -o' ,
1619- 'putclip' => 'getclip' ,
1327+ 'pbcopy' => 'pbpaste' ,
1328+ 'putclip' => 'getclip'
16201329 }
16211330
16221331 GITHUB_API_URL = URI ( "https://api.github.com/" )
1623- GIT_IO_URL = URI ( "https ://git.io" )
1332+ GIT_IO_URL = URI ( "http ://git.io" )
16241333
16251334 GITHUB_BASE_PATH = ""
16261335 GHE_BASE_PATH = "/api/v3"
@@ -1641,7 +1350,7 @@ module Gist
16411350 module AuthTokenFile
16421351 def self . filename
16431352 if ENV . key? ( URL_ENV_NAME )
1644- File . expand_path "~/.gist.#{ ENV [ URL_ENV_NAME ] . gsub ( /:/ , '.' ) . gsub ( / [^a-z0-9 .]/, '' ) } "
1353+ File . expand_path "~/.gist.#{ ENV [ URL_ENV_NAME ] . gsub ( /[^a-z .]/ , '' ) } "
16451354 else
16461355 File . expand_path "~/.gist"
16471356 end
@@ -1662,7 +1371,7 @@ module Gist
16621371 #
16631372 # @return [String] string value of access token or `nil`, if not found
16641373 def auth_token
1665- @token ||= AuthTokenFile . read rescue nil || Netrc . read [ self . api_url . host ] [ 1 ]
1374+ @token ||= AuthTokenFile . read rescue nil
16661375 end
16671376
16681377 # Upload a gist to https://gist.github.com
@@ -1881,12 +1590,10 @@ module Gist
18811590 # @param [String] url
18821591 # @return [String] shortened url, or long url if shortening fails
18831592 def shorten ( url )
1884- request = Net ::HTTP ::Post . new ( "/create " )
1593+ request = Net ::HTTP ::Post . new ( "/" )
18851594 request . set_form_data ( :url => url )
18861595 response = http ( GIT_IO_URL , request )
18871596 case response . code
1888- when "200"
1889- URI . join ( GIT_IO_URL , response . body ) . to_s
18901597 when "201"
18911598 response [ 'Location' ]
18921599 else
@@ -1959,7 +1666,7 @@ module Gist
19591666
19601667 if Net ::HTTPCreated === response
19611668 AuthTokenFile . write JSON . parse ( response . body ) [ 'token' ]
1962- puts "Success! #{ ENV [ URL_ENV_NAME ] || "https://github.com/" } settings/tokens "
1669+ puts "Success! #{ ENV [ URL_ENV_NAME ] || "https://github.com/" } settings/applications "
19631670 return
19641671 elsif Net ::HTTPUnauthorized === response
19651672 puts "Error: #{ JSON . parse ( response . body ) [ 'message' ] } "
0 commit comments