Class: PixivApi::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/pixiv_api/authentication.rb

Defined Under Namespace

Classes: InvalidAuthError

Instance Method Summary collapse

Constructor Details

#initialize(id:, password:) ⇒ Authentication

Returns a new instance of Authentication.

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pixiv_api/authentication.rb', line 9

def initialize(id:, password:)
  connection = Faraday.new(default_options) do |conn|
    # Set default middleware
    conn.request :multipart
    conn.request :url_encoded
    conn.adapter Faraday.default_adapter
  end

  @response = connection.post(
    URI(configuration[:token_url]).path,
    client_id: configuration[:client_id],
    client_secret: configuration[:client_secret],
    grant_type: 'password',
    username: id,
    password: password
  )

  raise InvalidAuthError, body['errors'].to_s unless success?
end

Instance Method Details

#bodyObject



29
30
31
# File 'lib/pixiv_api/authentication.rb', line 29

def body
  @body ||= JSON.parse(@response.body).freeze
end

#success?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/pixiv_api/authentication.rb', line 41

def success?
  @response.success?
end

#to_clientObject



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

def to_client
  PixivApi::Client.new(
    access_token: body['access_token'],
    refresh_token: body['refresh_token'],
    expires_at: Time.now.to_i + body['expires_in']
  )
end