require 'oauth' require 'forwardable' module Sredder WRIKE_OAUTH_OPTIONS = { :site => 'https://www.wrike.com', :authorize_path => '/rest/auth/authorize', :access_token_path => '/rest/auth/access_token', :request_token_path => '/rest/auth/request_token' } class WrikeAuth extend Forwardable attr_accessor :sredderc attr_writer :consumer def_delegators :sredderc, :token, :token=, :secret, :secret= def initialize(sredderc = Sredderc.new) @sredderc = sredderc @sredderc.load end def run_oauth_procedure @request_token = consumer.get_request_token puts 'Sredder is about to open your browser to complete the oauth protocol.' puts 'Please return to the terminal after you authenticate with Wrike.' Util.wait_for_input `open #{@request_token.authorize_url}` Util.wait_for_input store_tokens(@request_token.get_access_token) end def authorized? !!token && !!secret end def oauth_access_token if @access_token || authorized? @access_token ||= OAuth::AccessToken.new(consumer, secret, token) end end private def consumer @consumer ||= OAuth::Consumer.new(key, oauth_secret, WRIKE_OAUTH_OPTIONS) end def key '119705a437503d196da9eeab675808a4' end def oauth_secret '4216face9995a5d6' end def store_tokens(access_token) @access_token = access_token self.secret = access_token.token self.token = access_token.secret sredderc.save end end end