Class: ShopifyAPI::Context

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/shopify_api/context.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject (readonly)

Returns the value of attribute api_key.


104
105
106
# File 'lib/shopify_api/context.rb', line 104

def api_key
  @api_key
end

.api_secret_keyObject (readonly)

Returns the value of attribute api_secret_key.


104
105
106
# File 'lib/shopify_api/context.rb', line 104

def api_secret_key
  @api_secret_key
end

.api_versionObject (readonly)

Returns the value of attribute api_version.


104
105
106
# File 'lib/shopify_api/context.rb', line 104

def api_version
  @api_version
end

.host_nameObject (readonly)

Returns the value of attribute host_name.


104
105
106
# File 'lib/shopify_api/context.rb', line 104

def host_name
  @host_name
end

.loggerObject (readonly)

Returns the value of attribute logger.


113
114
115
# File 'lib/shopify_api/context.rb', line 113

def logger
  @logger
end

.private_shopObject (readonly)

Returns the value of attribute private_shop.


121
122
123
# File 'lib/shopify_api/context.rb', line 121

def private_shop
  @private_shop
end

.scopeObject (readonly)

Returns the value of attribute scope.


107
108
109
# File 'lib/shopify_api/context.rb', line 107

def scope
  @scope
end

.session_storageObject (readonly)

Returns the value of attribute session_storage.


110
111
112
# File 'lib/shopify_api/context.rb', line 110

def session_storage
  @session_storage
end

.user_agent_prefixObject (readonly)

Returns the value of attribute user_agent_prefix.


121
122
123
# File 'lib/shopify_api/context.rb', line 121

def user_agent_prefix
  @user_agent_prefix
end

Class Method Details

.activate_session(session) ⇒ Object


141
142
143
# File 'lib/shopify_api/context.rb', line 141

def activate_session(session)
  @active_session.value = session
end

.active_sessionObject


134
135
136
137
138
# File 'lib/shopify_api/context.rb', line 134

def active_session
  return @active_session.value unless @active_session.value.nil?

  private? ? Utils::SessionUtils.load_current_session : nil
end

.deactivate_sessionObject


146
147
148
# File 'lib/shopify_api/context.rb', line 146

def deactivate_session
  @active_session.value = nil
end

.embedded?Boolean

Returns:

  • (Boolean)

124
125
126
# File 'lib/shopify_api/context.rb', line 124

def embedded?
  @is_embedded
end

.load_rest_resources(api_version:) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/shopify_api/context.rb', line 76

def load_rest_resources(api_version:)
  # Unload any previous instances - mostly useful for tests where we need to reset the version
  @rest_resource_loader&.unload

  # No resources for the unstable version
  return if api_version == "unstable"

  version_folder_name = api_version.gsub("-", "_")
  path = "#{__dir__}/rest/resources/#{version_folder_name}"

  unless Dir.exist?(path)
    unless @notified_missing_resources_folder.key?(api_version)
      @logger.warn("Cannot autoload REST resources for API version '#{version_folder_name}', folder is missing")
      @notified_missing_resources_folder[api_version] = true
    end

    return
  end

  @rest_resource_loader = T.let(Zeitwerk::Loader.new, T.nilable(Zeitwerk::Loader))
  T.must(@rest_resource_loader).enable_reloading
  T.must(@rest_resource_loader).ignore("#{__dir__}/rest/resources")
  T.must(@rest_resource_loader).setup
  T.must(@rest_resource_loader).push_dir(path, namespace: ShopifyAPI)
  T.must(@rest_resource_loader).reload
end

.private?Boolean

Returns:

  • (Boolean)

116
117
118
# File 'lib/shopify_api/context.rb', line 116

def private?
  @is_private
end

.setup(api_key:, api_secret_key:, api_version:, host_name:, scope:, is_private:, is_embedded:, session_storage:, logger: Logger.new($stdout), private_shop: nil, user_agent_prefix: nil) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/shopify_api/context.rb', line 42

def setup(
  api_key:,
  api_secret_key:,
  api_version:,
  host_name:,
  scope:,
  is_private:,
  is_embedded:,
  session_storage:,
  logger: Logger.new($stdout),
  private_shop: nil,
  user_agent_prefix: nil
)
  unless ShopifyAPI::AdminVersions::SUPPORTED_ADMIN_VERSIONS.include?(api_version)
    raise Errors::UnsupportedVersionError,
      "Invalid vession #{api_version}, supported versions: #{ShopifyAPI::AdminVersions::SUPPORTED_ADMIN_VERSIONS}"
  end

  @api_key = api_key
  @api_secret_key = api_secret_key
  @api_version = api_version
  @host_name = host_name
  @is_private = is_private
  @scope = Auth::AuthScopes.new(scope)
  @is_embedded = is_embedded
  @session_storage = session_storage
  @logger = logger
  @private_shop = private_shop
  @user_agent_prefix = user_agent_prefix

  load_rest_resources(api_version: api_version)
end

.setup?Boolean

Returns:

  • (Boolean)

129
130
131
# File 'lib/shopify_api/context.rb', line 129

def setup?
  !(api_key.empty? || api_secret_key.empty? || host_name.empty?)
end