base.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. require 'shopify_api/version'
  2. module ShopifyAPI
  3. class Base < ActiveResource::Base
  4. class InvalidSessionError < StandardError; end
  5. extend Countable
  6. self.timeout = 90
  7. self.include_root_in_json = false
  8. self.headers['User-Agent'] = ["ShopifyAPI/#{ShopifyAPI::VERSION}",
  9. "ActiveResource/#{ActiveResource::VERSION::STRING}",
  10. "Ruby/#{RUBY_VERSION}"].join(' ')
  11. self.collection_parser = ShopifyAPI::PaginatedCollection
  12. def encode(options = {})
  13. same = dup
  14. same.attributes = {self.class.element_name => same.attributes} if self.class.format.extension == 'json'
  15. same.send("to_#{self.class.format.extension}", options)
  16. end
  17. def as_json(options = nil)
  18. root = options[:root] if options.try(:key?, :root)
  19. if include_root_in_json
  20. root = self.class.model_name.element if root == true
  21. { root => serializable_hash(options) }
  22. else
  23. serializable_hash(options)
  24. end
  25. end
  26. class << self
  27. threadsafe_attribute(:_api_version)
  28. def headers
  29. if _headers_defined?
  30. _headers
  31. elsif superclass != Object && superclass.headers
  32. superclass.headers
  33. else
  34. _headers ||= {}
  35. end
  36. end
  37. def activate_session(session)
  38. raise InvalidSessionError.new("Session cannot be nil") if session.nil?
  39. self.site = session.site
  40. self.headers.merge!('X-Shopify-Access-Token' => session.token)
  41. self.api_version = session.api_version
  42. end
  43. def clear_session
  44. self.site = nil
  45. self.password = nil
  46. self.user = nil
  47. self.api_version = nil
  48. self.headers.delete('X-Shopify-Access-Token')
  49. end
  50. def api_version
  51. if _api_version_defined?
  52. _api_version
  53. elsif superclass != Object && superclass.site
  54. superclass.api_version.dup.freeze
  55. else
  56. ApiVersion::NullVersion
  57. end
  58. end
  59. def api_version=(version)
  60. self._api_version = ApiVersion::NullVersion.matches?(version) ? ApiVersion::NullVersion : ApiVersion.find_version(version)
  61. end
  62. def prefix(options = {})
  63. api_version.construct_api_path(resource_prefix(options))
  64. end
  65. def prefix_source
  66. ''
  67. end
  68. def resource_prefix(_options = {})
  69. ''
  70. end
  71. # Sets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>).
  72. # Default value is <tt>site.path</tt>.
  73. def resource_prefix=(value)
  74. @prefix_parameters = nil
  75. resource_prefix_call = value.gsub(/:\w+/) { |key| "\#{URI.parser.escape options[#{key}].to_s}" }
  76. silence_warnings do
  77. # Redefine the new methods.
  78. instance_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
  79. def prefix_source() "#{value}" end
  80. def resource_prefix(options={}) "#{resource_prefix_call}" end
  81. RUBY_EVAL
  82. end
  83. rescue => e
  84. logger&.error("Couldn't set prefix: #{e}\n #{code}")
  85. raise
  86. end
  87. def prefix=(value)
  88. if value.start_with?('/admin')
  89. raise ArgumentError, "'#{value}' can no longer start /admin/. Change to using resource_prefix="
  90. end
  91. warn(
  92. '[DEPRECATED] ShopifyAPI::Base#prefix= is deprecated and will be removed in a future version. ' \
  93. 'Use `self.resource_prefix=` instead.'
  94. )
  95. self.resource_prefix = value
  96. end
  97. alias_method :set_prefix, :prefix=
  98. def init_prefix(resource)
  99. init_prefix_explicit(resource.to_s.pluralize, "#{resource}_id")
  100. end
  101. def init_prefix_explicit(resource_type, resource_id)
  102. self.resource_prefix = "#{resource_type}/:#{resource_id}/"
  103. define_method resource_id.to_sym do
  104. @prefix_options[resource_id]
  105. end
  106. end
  107. def early_july_pagination?
  108. !!early_july_pagination
  109. end
  110. def version_validation!(minimum_version)
  111. available_in_version = ShopifyAPI::ApiVersion.find_version(minimum_version)
  112. unless ShopifyAPI::Base.api_version >= available_in_version
  113. raise NotImplementedError, "The minimum supported version is #{minimum_version}."
  114. end
  115. end
  116. private
  117. attr_accessor :early_july_pagination
  118. def early_july_pagination_release!
  119. self.early_july_pagination = true
  120. end
  121. end
  122. def persisted?
  123. !id.nil?
  124. end
  125. private
  126. def only_id
  127. encode(:only => :id, :include => [], :methods => [])
  128. end
  129. end
  130. end