api_version.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # frozen_string_literal: true
  2. module ShopifyAPI
  3. class ApiVersion
  4. class UnknownVersion < StandardError; end
  5. class InvalidVersion < StandardError; end
  6. extend DefinedVersions
  7. include Comparable
  8. def self.coerce_to_version(version_or_name)
  9. return version_or_name if version_or_name.is_a?(ApiVersion)
  10. @versions ||= {}
  11. @versions.fetch(version_or_name.to_s) do
  12. raise UnknownVersion, "#{version_or_name} is not in the defined version set: #{@versions.keys.join(', ')}"
  13. end
  14. end
  15. def self.define_version(version)
  16. @versions ||= {}
  17. @versions[version.name] = version
  18. end
  19. def self.clear_defined_versions
  20. @versions = {}
  21. end
  22. def self.latest_stable_version
  23. @versions.values.select(&:stable?).sort.last
  24. end
  25. def to_s
  26. @version_name
  27. end
  28. alias_method :name, :to_s
  29. def inspect
  30. @version_name
  31. end
  32. def ==(other)
  33. other.class == self.class && to_s == other.to_s
  34. end
  35. def hash
  36. @version_name.hash
  37. end
  38. def <=>(other)
  39. numeric_version <=> other.numeric_version
  40. end
  41. def stable?
  42. false
  43. end
  44. def construct_api_path(_path)
  45. raise NotImplementedError
  46. end
  47. def construct_graphql_path
  48. raise NotImplementedError
  49. end
  50. protected
  51. attr_reader :numeric_version
  52. class Unstable < ApiVersion
  53. API_PREFIX = '/admin/api/unstable/'
  54. def initialize
  55. @version_name = "unstable"
  56. @url = API_PREFIX
  57. @numeric_version = 9_000_00
  58. end
  59. def construct_api_path(path)
  60. "#{@url}#{path}"
  61. end
  62. def construct_graphql_path
  63. construct_api_path("graphql.json")
  64. end
  65. end
  66. class Release < ApiVersion
  67. FORMAT = /^\d{4}-\d{2}$/.freeze
  68. API_PREFIX = '/admin/api/'
  69. def initialize(version_number)
  70. raise InvalidVersion, version_number unless version_number.match(FORMAT)
  71. @version_name = version_number
  72. @url = "#{API_PREFIX}#{version_number}/"
  73. @numeric_version = version_number.tr('-', '').to_i
  74. end
  75. def stable?
  76. true
  77. end
  78. def construct_api_path(path)
  79. "#{@url}#{path}"
  80. end
  81. def construct_graphql_path
  82. construct_api_path('graphql.json')
  83. end
  84. end
  85. end
  86. end