Module: Pasting

Extended by:
Pasting
Included in:
Pasting
Defined in:
lib/pasting.rb,
lib/pasting/version.rb

Defined Under Namespace

Modules: AuthTokenFile, Error Classes: ClipboardError

Constant Summary collapse

VERSION =
"0.0.3"
PASTING_API_URL =
URI.encode("http://api.pasting.io/".strip)
PASTING_BASE_PATH =
"/"
USER_AGENT =
"pasting/#{VERSION} (Net::HTTP, #{RUBY_DESCRIPTION})"

Instance Method Summary collapse

Instance Method Details

#api_urlObject

Get the API URL



135
136
137
# File 'lib/pasting.rb', line 135

def api_url
  PASTING_API_URL
end

#auth_tokenString

auth token for authentication

Returns:

  • (String)

    string value of access token or ‘nil`, if not found



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

def auth_token
  @token ||= AuthTokenFile.read rescue nil
end

#base_pathObject

Get the API base path



130
131
132
# File 'lib/pasting.rb', line 130

def base_path
  PASTING_BASE_PATH
end

#loginObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pasting.rb', line 64

def 
  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']
    else
      AuthTokenFile.write(json['console_key'])
    end
  else
    puts "Fail to register"
  end
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



121
122
123
124
125
126
127
# File 'lib/pasting.rb', line 121

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

  output = "http://pasting.io/p/#{json["documentId"]}"

  output
end

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

Upload a paste to pasting.io

Parameters:

  • content (String)

    the code you’d like to paste

See Also:



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
# File 'lib/pasting.rb', line 88

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)
    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) ⇒ Object



54
55
56
57
58
59
60
61
62
# 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