Module: Pasting
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
-
#api_url ⇒ Object
Get the API URL.
-
#auth_token ⇒ String
auth token for authentication.
-
#base_path ⇒ Object
Get the API base path.
- #login ⇒ Object
-
#on_success(body, options = {}) ⇒ Object
Called after an HTTP response to gist to perform post-processing.
-
#paste(content, options = {}) ⇒ Object
Upload a paste to pasting.io.
- #pasting_post(post_uri, body) ⇒ Object
Instance Method Details
#api_url ⇒ Object
Get the API URL
135 136 137 |
# File 'lib/pasting.rb', line 135 def api_url PASTING_API_URL end |
#auth_token ⇒ String
auth token for authentication
50 51 52 |
# File 'lib/pasting.rb', line 50 def auth_token @token ||= AuthTokenFile.read rescue nil end |
#base_path ⇒ Object
Get the API base path
130 131 132 |
# File 'lib/pasting.rb', line 130 def base_path PASTING_BASE_PATH end |
#login ⇒ Object
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 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'] 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.
121 122 123 124 125 126 127 |
# File 'lib/pasting.rb', line 121 def on_success(body, ={}) json = JSON.parse(body) output = "http://pasting.io/p/#{json["documentId"]}" output end |
#paste(content, options = {}) ⇒ Object
Upload a paste to pasting.io
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, = {}) if [: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 |