Sha256: da1c4ec8ad7371654d93b869d2eed523f213f6258927d01931deb7a0601da88a

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 KB

Contents

require 'cgi'
require 'net/http'
require 'uri'

module Pickpocket
  module Authentication
    class Oauth
      attr_reader :token_handler

      def initialize
        @token_handler = TokenHandler.new
      end

      def request_authorization
        uri      = URI(Pickpocket.config.pocket_oauth_request_url)
        response = Net::HTTP.post_form(uri, {
            consumer_key: Pickpocket.config.consumer_key,
            redirect_uri: Pickpocket.config.pocket_homepage
        })

        response_token = CGI::parse(response.body)['code'][0]
        auth_url       = URI(Pickpocket.config.pocket_user_authorize_url)
        auth_url.query = "request_token=#{response_token}&redirect_uri=#{Pickpocket.config.pocket_homepage}"

        Launchy.open(auth_url.to_s)
        token_handler.save_oauth(response_token)
      end

      def authorize
        response_token = token_handler.read_oauth
        uri            = URI(Pickpocket.config.pocket_oauth_authorize_url)

        response = Net::HTTP.post_form(uri, {
            consumer_key: Pickpocket.config.consumer_key,
            code:         response_token
        })

        response_token = CGI::parse(response.body)['access_token'][0]
        token_handler.save_auth(response_token)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pick-pocket-0.2.4 lib/pickpocket/authentication/oauth.rb
pick-pocket-0.2.3 lib/pickpocket/authentication/oauth.rb
pick-pocket-0.2.2 lib/pickpocket/authentication/oauth.rb
pick-pocket-0.2.1 lib/pickpocket/authentication/oauth.rb
pick-pocket-0.2.0 lib/pickpocket/authentication/oauth.rb
pick-pocket-0.1.2 lib/pickpocket/authentication/oauth.rb
pick-pocket-0.1.1 lib/pickpocket/authentication/oauth.rb
pick-pocket-0.1.0 lib/pickpocket/authentication/oauth.rb
pick-pocket-0.0.3 lib/pickpocket/authentication/oauth.rb