Sha256: d2adbb36b7a3ee23f4f36de51759a3e9beb23a0e7cff4f7c6504820fe8160ddd

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Imgur
  class Session
    HOST = 'https://api.imgur.com'

    # Creates the session instance that handles all the API calls to Imgur
    # access_token is optional
    def initialize(options)
      required_arguments = %i(client_id client_secret refresh_token)
      raise ArgumentError if required_arguments & options.keys != required_arguments

      @client_id = options[:client_id]
      @client_secret = options[:client_secret]
      @access_token = options[:access_token]
      @refresh_token = options[:refresh_token]
    end

    # Singleton initializer to allow access_token reuse
    def self.instance(options)
      @@session ||= new(options)
    end

    %w(Account Image).each do |clazz|
      define_method clazz.downcase do
        Imgur::Api.const_get(clazz).new(self)
      end
    end

    def access_token=(access_token)
      @access_token = access_token

      # Forces to create a new connection with new header
      @connection = nil
    end

    def params
      {
        refresh_token: @refresh_token,
        client_id:     @client_id,
        client_secret: @client_secret
      }
    end

    def connection
      @connection ||= Faraday.new(
        HOST,
        headers: {'Authorization' => 'Bearer ' << @access_token}
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
imgurapi-2.0.2 lib/imgur/session.rb