class ShopifyAPI::ApiVersion

Constants

API_PREFIX
HANDLE_FORMAT
LOOKUP_MODES
UNSTABLE_AS_DATE
UNSTABLE_HANDLE

Attributes

versions[R]
display_name[R]
handle[R]
latest_supported[R]
supported[R]
verified[R]

Public Class Methods

add_to_known_versions(version) click to toggle source
# File lib/shopify_api/api_version.rb, line 63
def add_to_known_versions(version)
  @versions[version.handle] = version
end
clear_defined_versions() click to toggle source
# File lib/shopify_api/api_version.rb, line 71
def clear_defined_versions
  warn(
    '[DEPRECATED] ShopifyAPI::ApiVersion.clear_defined_versions is deprecated and will be ' \
      'removed in a future version. Use `clear_known_versions` instead.'
  )
  clear_known_versions
end
clear_known_versions() click to toggle source
# File lib/shopify_api/api_version.rb, line 67
def clear_known_versions
  @versions = {}
end
coerce_to_version(version_or_handle) click to toggle source
# File lib/shopify_api/api_version.rb, line 41
def coerce_to_version(version_or_handle)
  warn(
    '[DEPRECATED] ShopifyAPI::ApiVersion.coerce_to_version be removed in a future version. ' \
      'Use `find_version` instead.'
  )
  find_version(version_or_handle)
end
define_known_versions() click to toggle source
# File lib/shopify_api/api_version.rb, line 55
def define_known_versions
  warn(
    '[DEPRECATED] ShopifyAPI::ApiVersion.define_known_versions is deprecated and will be ' \
      'removed in a future version. Use `fetch_known_versions` instead.'
  )
  fetch_known_versions
end
fetch_known_versions() click to toggle source
# File lib/shopify_api/api_version.rb, line 49
def fetch_known_versions
  @versions = Meta.admin_versions.map do |version|
    [version.handle, ApiVersion.new(version.attributes.merge(verified: version.persisted?))]
  end.to_h
end
find_version(version_or_handle) click to toggle source
# File lib/shopify_api/api_version.rb, line 27
def find_version(version_or_handle)
  raise ArgumentError, "NullVersion is not a valid version or version handle." if version_or_handle == NullVersion
  return version_or_handle if version_or_handle.is_a?(ApiVersion)
  handle = version_or_handle.to_s
  @versions ||= {}
  @versions.fetch(handle) do
    if @version_lookup_mode == :raise_on_unknown
      raise UnknownVersion, unknown_version_error_message(handle)
    else
      add_to_known_versions(ApiVersion.new(handle: handle))
    end
  end
end
latest_stable_version() click to toggle source
# File lib/shopify_api/api_version.rb, line 79
def latest_stable_version
  warn(
    '[DEPRECATED] ShopifyAPI::ApiVersion.latest_stable_version is deprecated and will be ' \
      'removed in a future version.'
  )
  versions.values.find(&:latest_supported?)
end
new(attributes) click to toggle source
# File lib/shopify_api/api_version.rb, line 106
def initialize(attributes)
  attributes = ActiveSupport::HashWithIndifferentAccess.new(attributes)
  @handle = attributes[:handle].to_s
  @display_name = attributes.fetch(:display_name, attributes[:handle].to_s)
  @supported = attributes.fetch(:supported, false)
  @latest_supported = attributes.fetch(:latest_supported, false)
  @verified = attributes.fetch(:verified, false)
end
version_lookup_mode() click to toggle source
# File lib/shopify_api/api_version.rb, line 17
def version_lookup_mode
  @version_lookup_mode ||= :define_on_unknown
end
version_lookup_mode=(mode) click to toggle source
# File lib/shopify_api/api_version.rb, line 21
def version_lookup_mode=(mode)
  raise ArgumentError, "Mode must be one of #{LOOKUP_MODES}" unless LOOKUP_MODES.include?(mode)
  sanitize_known_versions if mode == :raise_on_unknown
  @version_lookup_mode = mode
end

Private Class Methods

sanitize_known_versions() click to toggle source
# File lib/shopify_api/api_version.rb, line 89
def sanitize_known_versions
  return if @versions.nil?
  @versions = @versions.keys.map do |handle|
    next unless @versions[handle].verified?
    [handle, @versions[handle]]
  end.compact.to_h
end
unknown_version_error_message(handle) click to toggle source
# File lib/shopify_api/api_version.rb, line 97
def unknown_version_error_message(handle)
  msg = "ApiVersion.version_lookup_mode is set to `:raise_on_unknown`. \n"
  return msg + "No versions defined. You must call `ApiVersion.fetch_known_versions` first." if @versions.empty?
  msg + "`#{handle}` is not in the defined version set. Available versions: #{@versions.keys}"
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/shopify_api/api_version.rb, line 131
def <=>(other)
  handle_as_date <=> other.handle_as_date
end
==(other) click to toggle source
# File lib/shopify_api/api_version.rb, line 135
def ==(other)
  other.class == self.class && handle == other.handle
end
construct_api_path(path) click to toggle source
# File lib/shopify_api/api_version.rb, line 143
def construct_api_path(path)
  "#{API_PREFIX}#{handle}/#{path}"
end
construct_graphql_path() click to toggle source
# File lib/shopify_api/api_version.rb, line 147
def construct_graphql_path
  construct_api_path('graphql.json')
end
handle_as_date() click to toggle source
# File lib/shopify_api/api_version.rb, line 171
def handle_as_date
  return UNSTABLE_AS_DATE if unstable?
  year, month, day = handle.split('-')
  Time.utc(year, month, day)
end
hash() click to toggle source
# File lib/shopify_api/api_version.rb, line 139
def hash
  handle.hash
end
latest_supported?() click to toggle source
# File lib/shopify_api/api_version.rb, line 119
def latest_supported?
  latest_supported
end
name() click to toggle source
# File lib/shopify_api/api_version.rb, line 151
def name
  warn(
    '[DEPRECATED] ShopifyAPI::ApiVersion#name is deprecated and will be removed in a future version. ' \
      'Use `handle` instead.'
  )
  handle
end
stable?() click to toggle source
# File lib/shopify_api/api_version.rb, line 159
def stable?
  warn(
    '[DEPRECATED] ShopifyAPI::ApiVersion#stable? is deprecated and will be removed in a future version. ' \
      'Use `supported?` instead.'
  )
  supported?
end
supported?() click to toggle source
# File lib/shopify_api/api_version.rb, line 123
def supported?
  supported
end
to_s() click to toggle source
# File lib/shopify_api/api_version.rb, line 115
def to_s
  handle
end
unstable?() click to toggle source
# File lib/shopify_api/api_version.rb, line 167
def unstable?
  handle == UNSTABLE_HANDLE
end
verified?() click to toggle source
# File lib/shopify_api/api_version.rb, line 127
def verified?
  verified
end