Sha256: af8325398ab227d724ed21cff5061f656f7d1ec2e7cd9c0a1ad502ece78acb1d

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

require 'flyrb'
require 'open-uri'
require 'net/http'

Flyrb.equip(:clipboard)

module Flyrb
  module Gist
    def gist(content = nil, filename = nil)
      content  ||= Clipboard.read if Clipboard.available?
      filename ||= "fly.rb"

      url = URI.parse('http://gist.github.com/gists')
      req = Net::HTTP.post_form(url, data(filename, "rb", content))
      
      gist_url = req['Location']
      Clipboard.write(gist_url) if Clipboard.available?
      open(gist_url)
    end
    
    private
      def open(url)
        case Platform::IMPL
        when :macosx
          Kernel.system("open #{url}")
        when :mswin
          pastie_url = url.chop if url[-1].chr == "\000"
          Kernel.system("start #{url}")
        end
      end
      
      def data(filename, ext, content)
        {
          :'file_ext[gistfile1]'      => ext,
          :'file_name[gistfile1]'     => filename,
          :'file_contents[gistfile1]' => content
        }.merge(auth)
      end
      
      def auth
        user  = `git config --global github.user`.strip
        token = `git config --global github.token`.strip

        user.empty? ? {} : { :login => user, :token => token }
      end
  end
end

class Object
  include Flyrb::Gist
end if Object.const_defined? :IRB

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flyrb-1.0.0.c lib/flyrb/gist.rb
flyrb-1.0.0.b lib/flyrb/gist.rb