class ShopifyAPI::Auth::Session

Attributes

access_token[RW]
associated_user[RW]
associated_user_scope[RW]
expires[RW]
id[R]
scope[RW]
shop[RW]
shopify_session_id[RW]
state[RW]

Public Class Methods

deserialize(str) click to toggle source
# File lib/shopify_api/auth/session.rb, line 119
def deserialize(str)
  Oj.load(str)
end
from(shop:, access_token_response:) click to toggle source
# File lib/shopify_api/auth/session.rb, line 93
def from(shop:, access_token_response:)
  is_online = access_token_response.online_token?

  if is_online
    associated_user = T.must(access_token_response.associated_user)
    expires = Time.now + access_token_response.expires_in.to_i
    associated_user_scope = access_token_response.associated_user_scope
    id = "#{shop}_#{associated_user.id}"
  else
    id = "offline_#{shop}"
  end

  new(
    id: id,
    shop: shop,
    access_token: access_token_response.access_token,
    scope: access_token_response.scope,
    is_online: is_online,
    associated_user_scope: associated_user_scope,
    associated_user: associated_user,
    expires: expires,
    shopify_session_id: access_token_response.session,
  )
end
new(shop:, id: nil, state: nil, access_token: "", scope: [], associated_user_scope: nil, expires: nil, is_online: nil, associated_user: nil, shopify_session_id: nil) click to toggle source
# File lib/shopify_api/auth/session.rb, line 57
def initialize(shop:, id: nil, state: nil, access_token: "", scope: [], associated_user_scope: nil, expires: nil,
  is_online: nil, associated_user: nil, shopify_session_id: nil)
  @id = T.let(id || SecureRandom.uuid, String)
  @shop = shop
  @state = state
  @access_token = access_token
  @scope = T.let(AuthScopes.new(scope), AuthScopes)
  @associated_user_scope = T.let(
    associated_user_scope.nil? ? nil : AuthScopes.new(associated_user_scope), T.nilable(AuthScopes)
  )
  @expires = expires
  @associated_user = associated_user
  @is_online = T.let(is_online || !associated_user.nil?, T::Boolean)
  @shopify_session_id = shopify_session_id
end
temp(shop:, access_token:) { |temp_session| ... } click to toggle source
# File lib/shopify_api/auth/session.rb, line 80
def temp(shop:, access_token:, &blk)
  original_session = Context.active_session
  temp_session = Session.new(shop: shop, access_token: access_token)

  begin
    Context.activate_session(temp_session)
    yield temp_session
  ensure
    Context.activate_session(original_session)
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/shopify_api/auth/session.rb, line 142
def ==(other)
  if other

    id == other.id &&
      shop == other.shop &&
      state == other.state &&
      scope == other.scope &&
      associated_user_scope == other.associated_user_scope &&
      (!(expires.nil? ^ other.expires.nil?) && (expires.nil? || expires.to_i == other.expires.to_i)) &&
      online? == other.online? &&
      associated_user == other.associated_user &&
      shopify_session_id == other.shopify_session_id

  else
    false
  end
end
Also aliased as: eql?
copy_attributes_from(other) click to toggle source
# File lib/shopify_api/auth/session.rb, line 125
def copy_attributes_from(other)
  JSON.parse(other.serialize).keys.each do |key|
    next if key.include?("^")

    variable_name = "@#{key}"
    instance_variable_set(variable_name, other.instance_variable_get(variable_name))
  end
  self
end
eql?(other)
Alias for: ==
expired?() click to toggle source
# File lib/shopify_api/auth/session.rb, line 39
def expired?
  @expires ? @expires < Time.now : false
end
online?() click to toggle source
# File lib/shopify_api/auth/session.rb, line 34
def online?
  @is_online
end
serialize() click to toggle source
# File lib/shopify_api/auth/session.rb, line 136
def serialize
  Oj.dump(self)
end