Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Last active March 9, 2018 10:30

Revisions

  1. alphaKAI revised this gist Jan 30, 2014. 1 changed file with 72 additions and 54 deletions.
    126 changes: 72 additions & 54 deletions pocket_lib.rb
    Original file line number Diff line number Diff line change
    @@ -1,60 +1,78 @@
    #encoding:utf-8
    require "net/https"
    require "open-uri"
    require "uri"
    require "json"
    require "pp"

    class PocketRuby
    def get_code(ck)
    header={
    "Content-Type"=> "application/json; charset=UTF-8",
    "X-Accept"=> " application/json"
    }

    body={
    "consumer_key" => ck.to_s,
    "redirect_uri" => "localhost"
    }
    uri=URI.parse("https://getpocket.com/v3/oauth/request")

    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)
    pp res
    }
    return res
    end
    def get_atoken(ck,code)
    header={
    "Content-Type"=> "application/json; charset=UTF-8",
    "X-Accept"=> " application/json"
    }
    body={
    "consumer_key" => ck.to_s,
    "code"=>code.to_s
    }
    uri=URI.parse("https://getpocket.com/v3/oauth/authorize")

    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(
    p htp.request(request).body)
    pp res
    }
    return res
    end
    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
    ck="***"#consumer_key
    res=PocketRuby.new.get_code(ck)["code"]
    puts res
    PocketRuby.new.get_atoken(ck,res)

    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")
  2. alphaKAI created this gist Jun 29, 2013.
    60 changes: 60 additions & 0 deletions pocket_lib.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    require "net/https"
    require "uri"
    require "json"
    require "pp"
    class PocketRuby
    def get_code(ck)
    header={
    "Content-Type"=> "application/json; charset=UTF-8",
    "X-Accept"=> " application/json"
    }

    body={
    "consumer_key" => ck.to_s,
    "redirect_uri" => "localhost"
    }
    uri=URI.parse("https://getpocket.com/v3/oauth/request")

    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)
    pp res
    }
    return res
    end
    def get_atoken(ck,code)
    header={
    "Content-Type"=> "application/json; charset=UTF-8",
    "X-Accept"=> " application/json"
    }
    body={
    "consumer_key" => ck.to_s,
    "code"=>code.to_s
    }
    uri=URI.parse("https://getpocket.com/v3/oauth/authorize")

    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(
    p htp.request(request).body)
    pp res
    }
    return res
    end
    end
    ck="***"#consumer_key
    res=PocketRuby.new.get_code(ck)["code"]
    puts res
    PocketRuby.new.get_atoken(ck,res)