class PixivApi::Authentication

Public Class Methods

new(id:, password:) click to toggle source
# 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

Public Instance Methods

body() click to toggle source
# File lib/pixiv_api/authentication.rb, line 29
def body
  @body ||= JSON.parse(@response.body).freeze
end
success?() click to toggle source
# File lib/pixiv_api/authentication.rb, line 41
def success?
  @response.success?
end
to_client() click to toggle source
# 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

Private Instance Methods

configuration() click to toggle source
# File lib/pixiv_api/authentication.rb, line 47
def configuration
  PixivApi.configuration.tap(&:require_keys!)
end
default_options() click to toggle source
# File lib/pixiv_api/authentication.rb, line 51
def default_options
  {
    headers: {
      'Host' => URI(configuration[:token_url]).host,
    },
    url: configuration[:site],
  }
end