class IronBank::Authentications::Cookie

Get a cookie to enable authenticated calls to Zuora through Session.

Constants

ONE_HOUR
TEN_MINUTES

Attributes

base_url[R]
client_id[R]
client_secret[R]
expires_at[R]
zsession[R]

Public Class Methods

new(client_id:, client_secret:, base_url:) click to toggle source
# File lib/iron_bank/authentications/cookie.rb, line 11
def initialize(client_id:, client_secret:, base_url:)
  @client_id     = client_id
  @client_secret = client_secret
  @base_url      = base_url
  fetch_cookie
end

Public Instance Methods

expired?() click to toggle source
# File lib/iron_bank/authentications/cookie.rb, line 18
def expired?
  # Ten minutes as a margin of error in order to renew token in time
  expires_at < Time.now + TEN_MINUTES
end
header() click to toggle source
# File lib/iron_bank/authentications/cookie.rb, line 23
def header
  { "Cookie" => use }
end

Private Instance Methods

authenticate() click to toggle source
# File lib/iron_bank/authentications/cookie.rb, line 49
def authenticate
  connection.post("v1/connections", {})
end
authentication_headers() click to toggle source
# File lib/iron_bank/authentications/cookie.rb, line 72
def authentication_headers
  {
    apiaccesskeyid:     client_id,
    apisecretaccesskey: client_secret
  }
end
connection() click to toggle source
# File lib/iron_bank/authentications/cookie.rb, line 53
def connection
  @connection ||= Faraday.new(**faraday_config) do |conn|
    IronBank.configuration.middlewares.each do |klass, options|
      conn.use klass, options
    end

    conn.request  :url_encoded
    conn.response :json
    conn.adapter  Faraday.default_adapter
  end
end
faraday_config() click to toggle source
# File lib/iron_bank/authentications/cookie.rb, line 65
def faraday_config
  {
    url:     base_url,
    headers: authentication_headers
  }
end
fetch_zsession() click to toggle source
# File lib/iron_bank/authentications/cookie.rb, line 45
def fetch_zsession
  /ZSession=([^;]+)/.match(cookie)[0]
end
use() click to toggle source
# File lib/iron_bank/authentications/cookie.rb, line 32
def use
  refetch_cookie if expired?
  zsession
end