Sha256: 9cc361e0e28a370829e01cc5a63afe989c233caa29697693d7c8046abc5e94a9

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sredder-0.0.4 lib/sredder/wrike_auth.rb