Class: ShopifyAPI::ApiVersion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/shopify_api/api_version.rb

Defined Under Namespace

Classes: ApiVersionNotSetError, NullVersion, UnknownVersion

Constant Summary collapse

UNSTABLE_HANDLE =
'unstable'
HANDLE_FORMAT =
/((\d{4}-\d{2})|#{UNSTABLE_HANDLE})/.freeze
UNSTABLE_AS_DATE =
Time.utc(3000, 1, 1)
API_PREFIX =
'/admin/api/'
LOOKUP_MODES =
[:raise_on_unknown, :define_on_unknown].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ ApiVersion

Returns a new instance of ApiVersion.


105
106
107
108
109
110
111
112
# File 'lib/shopify_api/api_version.rb', line 105

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

Class Attribute Details

.versionsObject (readonly)

Returns the value of attribute versions


15
16
17
# File 'lib/shopify_api/api_version.rb', line 15

def versions
  @versions
end

Instance Attribute Details

#display_nameObject (readonly)

Returns the value of attribute display_name


103
104
105
# File 'lib/shopify_api/api_version.rb', line 103

def display_name
  @display_name
end

#handleObject (readonly)

Returns the value of attribute handle


103
104
105
# File 'lib/shopify_api/api_version.rb', line 103

def handle
  @handle
end

#latest_supportedObject (readonly)

Returns the value of attribute latest_supported


103
104
105
# File 'lib/shopify_api/api_version.rb', line 103

def latest_supported
  @latest_supported
end

#supportedObject (readonly)

Returns the value of attribute supported


103
104
105
# File 'lib/shopify_api/api_version.rb', line 103

def supported
  @supported
end

#verifiedObject (readonly)

Returns the value of attribute verified


103
104
105
# File 'lib/shopify_api/api_version.rb', line 103

def verified
  @verified
end

Class Method Details

.add_to_known_versions(version) ⇒ Object


63
64
65
# File 'lib/shopify_api/api_version.rb', line 63

def add_to_known_versions(version)
  @versions[version.handle] = version
end

.clear_defined_versionsObject


71
72
73
74
75
76
77
# 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_versionsObject


67
68
69
# File 'lib/shopify_api/api_version.rb', line 67

def clear_known_versions
  @versions = {}
end

.coerce_to_version(version_or_handle) ⇒ Object


41
42
43
44
45
46
47
# 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_versionsObject


55
56
57
58
59
60
61
# 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_versionsObject


49
50
51
52
53
# 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) ⇒ Object

Raises:

  • (ArgumentError)

27
28
29
30
31
32
33
34
35
36
37
38
39
# 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_versionObject


79
80
81
82
83
84
# 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

.version_lookup_modeObject


17
18
19
# File 'lib/shopify_api/api_version.rb', line 17

def version_lookup_mode
  @version_lookup_mode ||= :define_on_unknown
end

.version_lookup_mode=(mode) ⇒ Object

Raises:

  • (ArgumentError)

21
22
23
24
25
# 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

Instance Method Details

#<=>(other) ⇒ Object


130
131
132
# File 'lib/shopify_api/api_version.rb', line 130

def <=>(other)
  handle_as_date <=> other.handle_as_date
end

#==(other) ⇒ Object


134
135
136
# File 'lib/shopify_api/api_version.rb', line 134

def ==(other)
  other.class == self.class && handle == other.handle
end

#construct_api_path(path) ⇒ Object


142
143
144
# File 'lib/shopify_api/api_version.rb', line 142

def construct_api_path(path)
  "#{API_PREFIX}#{handle}/#{path}"
end

#construct_graphql_pathObject


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

def construct_graphql_path
  construct_api_path('graphql.json')
end

#handle_as_dateObject


170
171
172
173
174
# File 'lib/shopify_api/api_version.rb', line 170

def handle_as_date
  return UNSTABLE_AS_DATE if unstable?
  year, month, day = handle.split('-')
  Time.utc(year, month, day)
end

#hashObject


138
139
140
# File 'lib/shopify_api/api_version.rb', line 138

def hash
  handle.hash
end

#latest_supported?Boolean

Returns:

  • (Boolean)

118
119
120
# File 'lib/shopify_api/api_version.rb', line 118

def latest_supported?
  latest_supported
end

#nameObject


150
151
152
153
154
155
156
# File 'lib/shopify_api/api_version.rb', line 150

def name
  warn(
    '[DEPRECATED] ShopifyAPI::ApiVersion#name is deprecated and will be removed in a future version. ' \
      'Use `handle` instead.'
  )
  handle
end

#stable?Boolean

Returns:

  • (Boolean)

158
159
160
161
162
163
164
# File 'lib/shopify_api/api_version.rb', line 158

def stable?
  warn(
    '[DEPRECATED] ShopifyAPI::ApiVersion#stable? is deprecated and will be removed in a future version. ' \
      'Use `supported?` instead.'
  )
  supported?
end

#supported?Boolean

Returns:

  • (Boolean)

122
123
124
# File 'lib/shopify_api/api_version.rb', line 122

def supported?
  supported
end

#to_sObject


114
115
116
# File 'lib/shopify_api/api_version.rb', line 114

def to_s
  handle
end

#unstable?Boolean

Returns:

  • (Boolean)

166
167
168
# File 'lib/shopify_api/api_version.rb', line 166

def unstable?
  handle == UNSTABLE_HANDLE
end

#verified?Boolean

Returns:

  • (Boolean)

126
127
128
# File 'lib/shopify_api/api_version.rb', line 126

def verified?
  verified
end