Sha256: d5b15a8ce9c841c4eac3563d713ee92284f711b84f6dbf90a14a199ce354e0c9

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 KB

Contents

module Bitly
  module V3
    # OAuth consumer for authentication
    class OAuth
      attr_reader :access_token
      def initialize(consumer_token, consumer_secret)
        @consumer_token, @consumer_secret = consumer_token, consumer_secret
      end

      # Get the OAuth 2 client
      def client
        @client ||= ::OAuth2::Client.new(
          @consumer_token,
          @consumer_secret,
          :site => 'https://api-ssl.bitly.com',
          :token_url => '/oauth/access_token'
        )
      end

      # Get the url to redirect a user to, pass the url you want the user
      # to be redirected back to.
      def authorize_url(redirect_url, state=nil)
        params = {:redirect_uri => redirect_url}
        params[:state] = state if state
        client.auth_code.authorize_url(params).gsub(/api-ssl\./,'')
      end

      # Get the access token. You must pass the exact same redirect_url passed
      # to the authorize_url method
      def get_access_token_from_code(code,redirect_url)
        @access_token ||= client.auth_code.get_token(code, :redirect_uri => redirect_url, :parse => :query)
      end

      # If you already have a user token, this method gets the access token
      def get_access_token_from_token(token, params={})
        params = params.inject({}) { |options, (key, value)| options[key.to_s] = value; options }
        @access_token ||= ::OAuth2::AccessToken.new(client, token, params)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bitly-1.1.2 lib/bitly/v3/oauth.rb
bitly-1.1.1 lib/bitly/v3/oauth.rb
bitly-1.1.0 lib/bitly/v3/oauth.rb
bitly-1.0.2 lib/bitly/v3/oauth.rb
bitly-1.0.1 lib/bitly/v3/oauth.rb
bitly-1.0.0 lib/bitly/v3/oauth.rb
bitly-0.10.4 lib/bitly/v3/oauth.rb