Class: Imgurapi::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/imgurapi/session.rb

Constant Summary collapse

HOST =
'https://api.imgur.com'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Session

Creates the session instance that handles all the API calls to Imgur access_token is optional

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
# File 'lib/imgurapi/session.rb', line 7

def initialize(options)
  required_arguments = %i(client_id client_secret refresh_token)
  raise ArgumentError unless required_arguments.all? { |key| options.keys.include? key }

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

Class Method Details

.instance(options) ⇒ Object

Singleton initializer to allow access_token reuse



19
20
21
# File 'lib/imgurapi/session.rb', line 19

def self.instance(options)
  @@session ||= new(options)
end

Instance Method Details

#access_token=(access_token) ⇒ Object



33
34
35
36
37
38
# File 'lib/imgurapi/session.rb', line 33

def access_token=(access_token)
  @access_token = access_token

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

#connectionObject



48
49
50
51
52
53
# File 'lib/imgurapi/session.rb', line 48

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

#paramsObject



40
41
42
43
44
45
46
# File 'lib/imgurapi/session.rb', line 40

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

#use_ssl?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/imgurapi/session.rb', line 29

def use_ssl?
  @use_ssl
end