Sha256: be34b38a8da84269e7f3da8614409486cbc6a3c96ab2c018346d6912485cf6ac

Contents?: true

Size: 1.93 KB

Versions: 7

Compression:

Stored size: 1.93 KB

Contents

require 'addressable/uri'
require 'simple_oauth'
require 'twitter/version'

module Twitter
  class Client
    attr_accessor :access_token, :access_token_secret, :consumer_key, :consumer_secret
    alias_method :oauth_token, :access_token
    alias_method :oauth_token=, :access_token=
    alias_method :oauth_token_secret, :access_token_secret
    alias_method :oauth_token_secret=, :access_token_secret=

    # Initializes a new Client object
    #
    # @param options [Hash]
    # @return [Twitter::Client]
    def initialize(options = {})
      options.each do |key, value|
        send(:"#{key}=", value)
      end
      yield self if block_given?
      validate_credential_type!
    end

    # @return [Boolean]
    def user_token?
      !!(access_token && access_token_secret)
    end

    # @return [String]
    def user_agent
      @user_agent ||= "Twitter Ruby Gem #{Twitter::Version}"
    end

    # @return [Hash]
    def credentials
      {
        :consumer_key    => consumer_key,
        :consumer_secret => consumer_secret,
        :token           => access_token,
        :token_secret    => access_token_secret,
      }
    end

    # @return [Boolean]
    def credentials?
      credentials.values.all?
    end

  private

    # Ensures that all credentials set during configuration are of a
    # valid type. Valid types are String and Symbol.
    #
    # @raise [Twitter::Error::ConfigurationError] Error is raised when
    #   supplied twitter credentials are not a String or Symbol.
    def validate_credential_type!
      credentials.each do |credential, value|
        next if value.nil?
        fail(Error::ConfigurationError, "Invalid #{credential} specified: #{value.inspect} must be a string or symbol.") unless value.is_a?(String) || value.is_a?(Symbol)
      end
    end

    def oauth_auth_header(method, uri, params = {})
      uri = Addressable::URI.parse(uri)
      SimpleOAuth::Header.new(method, uri, params, credentials)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
twitter-5.5.1 lib/twitter/client.rb
twitter-5.5.0 lib/twitter/client.rb
twitter-5.4.1 lib/twitter/client.rb
twitter-5.4.0 lib/twitter/client.rb
twitter-5.3.1 lib/twitter/client.rb
twitter-5.3.0 lib/twitter/client.rb
twitter-5.2.0 lib/twitter/client.rb