Class: FortyTwo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fortytwo/client.rb

Constant Summary collapse

API_HOST =
'https://api.intra.42.fr'
REQUEST_CLASSES =
[FortyTwo::Endpoint::User,
FortyTwo::Endpoint::Users,
FortyTwo::Endpoint::UserSessions]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
# File 'lib/fortytwo/client.rb', line 17

def initialize(options = nil)
  @configuration = nil
  define_request_methods

  unless options.nil?
    @configuration = Configuration.new(options)
    check_api_keys
  end
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



15
16
17
# File 'lib/fortytwo/client.rb', line 15

def configuration
  @configuration
end

Instance Method Details

#check_api_keysObject



33
34
35
36
37
38
39
# File 'lib/fortytwo/client.rb', line 33

def check_api_keys
  if configuration.nil? || configuration.invalid?
    @configuration = nil
  else
    @configuration.freeze
  end
end

#configure {|@configuration| ... } ⇒ Object

Yields:



27
28
29
30
31
# File 'lib/fortytwo/client.rb', line 27

def configure
  @configuration = Configuration.new
  yield(@configuration)
  check_api_keys
end

#tokenObject



41
42
43
44
45
46
47
48
49
# File 'lib/fortytwo/client.rb', line 41

def token
  return @token if instance_variable_defined?(:@token)

  check_api_keys
  api_key = @configuration.api_key
  api_secret = @configuration.api_secret
  client = OAuth2::Client.new(api_key, api_secret, site: API_HOST)
  client.client_credentials.get_token
end