Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Last active March 9, 2018 10:30
Show Gist options
  • Save alphaKAI/5891205 to your computer and use it in GitHub Desktop.
Save alphaKAI/5891205 to your computer and use it in GitHub Desktop.
Pocket API Rapper Written in Ruby.
#encoding:utf-8
require "net/https"
require "open-uri"
require "uri"
require "json"
require "pp"
class PocketRuby
def initialize
@header = {
"Content-Type"=> "application/json; charset=UTF-8",
"X-Accept"=> " application/json"
}
end
def post(header, body, url)
uri = URI.parse(url)
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
res = ""
https.start{|htp|
request = Net::HTTP::Post.new(uri.path)
request["X-Accept"] = header["X-Accept"]
request.set_form_data(body,"&")
res = JSON.parse(htp.request(request).body)
}
return res
end
def get_code(consumer_key)
uri = "https://getpocket.com/v3/oauth/request"
header = @header
body = {
"consumer_key" => consumer_key.to_s,
"redirect_uri" => "localhost"
}
return post(header, body, uri)
end
def get_access_token(consumer_key, code)
uri = "https://getpocket.com/v3/oauth/authorize"
header = @header
body = {
"consumer_key" => consumer_key,
"code"=> code
}
return post(header, body, uri)
end
def add_article(consumer_key, access_token, url)
uri = "https://getpocket.com/v3/add"
header = @header
body = {
"url" => url,
"time" => Time.now.to_i.to_s,
"consumer_key" => consumer_key,
"access_token" => access_token
}
return post(header, body, uri)
end
end
consumer_key = "REPLACE YOUR CONSUMER KEY"
#initialize
code = "";access_token = "";username = ""
pr = PocketRuby.new
code = pr.get_code(consumer_key)["code"]
#if Linux => remove "start"
system("start firefox https://getpocket.com/auth/authorize?request_token=#{code}")
puts "PLEASE ACCEPT AND PUSH ENTER"
STDIN.gets
res = pr.get_access_token(consumer_key, code)
access_token = res["access_token"]
username = res["username"]
pr.add_article(consumer_key, access_token, "URL")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment