module Pasting

Constants

PASTING_API_URL
PASTING_BASE_PATH
USER_AGENT
VERSION

Public Instance Methods

api_url() click to toggle source

Get the API URL

# File lib/pasting.rb, line 139
def api_url
  PASTING_API_URL
end
auth_token() click to toggle source

auth token for authentication

@return [String] string value of access token or ‘nil`, if not found

# File lib/pasting.rb, line 49
def auth_token
  @token ||= AuthTokenFile.read rescue nil
end
base_path() click to toggle source

Get the API base path

# File lib/pasting.rb, line 134
def base_path
  PASTING_BASE_PATH
end
login() click to toggle source

Create token to pasting @see pasting.io/

# File lib/pasting.rb, line 66
def login
  print "Pasting username: "
  username =  $stdin.gets.strip
  print "Pasting password: "
  password = $stdin.gets.strip
  body = {:username => username, :pwd => password}
  response = pasting_post('/createConsoleKey', body)
  if Net::HTTPSuccess === response
    json = JSON.parse(response.body)
    if json['st'] == 'error'
      puts json['msg']
    elsif json['st'] == 'ok'
      AuthTokenFile.write(json['console_key'])
      puts "Login Success"
    end
  else
    puts "Fail to register"
  end
end
on_success(body, options={}) click to toggle source

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

@param [String] body the text body from the github api @param [Hash] options more detailed options, see

the documentation for {multi_gist}
# File lib/pasting.rb, line 124
def on_success(body, options={})
  json = JSON.parse(body)
  output = "http://pasting.io/p/#{json["documentId"]}"
  if options[:private] 
    output = output + "/#{json["hash"]}"
  end
  output
end
paste(content, options = {}) click to toggle source

Upload a paste to pasting.io

@param [String] content the code you’d like to paste

@see pasting.io/

# File lib/pasting.rb, line 91
def paste(content, options = {})
  if options[:private]
    prv = 1
  else
    prv = 0
  end 
  body = {:consoleKey => auth_token, :text => content, :protected => prv }
  uri = URI.parse(api_url)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new("/createFromConsole")
  request.add_field('Content-Type', 'application/json')
  request.body = body.to_json
  response = http.request(request)

  if Net::HTTPSuccess === response
    messsage = on_success(response.body, options)
    puts "#{messsage}"
    begin
      IO.popen('pbcopy', 'r+') { |clip| clip.print messsage }
    rescue 
      puts
    end
  else
    puts "Fail to register, please check user and password"
  end
end
pasting_post(post_uri, body) click to toggle source
# File lib/pasting.rb, line 54
def pasting_post(post_uri, body)
  uri = URI.parse(api_url)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(post_uri)
  request.add_field('Content-Type', 'application/json')
  request.body = body.to_json
  response = http.request(request)
  response
end