base.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, "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.headers.delete('X-Shopify-Access-Token')
  48. end
  49. def api_version
  50. if _api_version_defined?
  51. _api_version
  52. elsif superclass != Object && superclass.site
  53. superclass.api_version.dup.freeze
  54. else
  55. ApiVersion::NullVersion
  56. end
  57. end
  58. def api_version=(version)
  59. self._api_version = ApiVersion::NullVersion.matches?(version) ? ApiVersion::NullVersion : ApiVersion.find_version(version)
  60. end
  61. def prefix(options = {})
  62. api_version.construct_api_path(resource_prefix(options))
  63. end
  64. def prefix_source
  65. ''
  66. end
  67. def resource_prefix(_options = {})
  68. ''
  69. end
  70. # Sets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>).
  71. # Default value is <tt>site.path</tt>.
  72. def resource_prefix=(value)
  73. @prefix_parameters = nil
  74. resource_prefix_call = value.gsub(/:\w+/) { |key| "\#{URI::DEFAULT_PARSER.escape options[#{key}].to_s}" }
  75. silence_warnings do
  76. # Redefine the new methods.
  77. instance_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
  78. def prefix_source() "#{value}" end
  79. def resource_prefix(options={}) "#{resource_prefix_call}" end
  80. RUBY_EVAL
  81. end
  82. rescue => e
  83. logger&.error("Couldn't set prefix: #{e}\n #{code}")
  84. raise
  85. end
  86. def prefix=(value)
  87. if value.start_with?('/admin')
  88. raise ArgumentError, "'#{value}' can no longer start /admin/. Change to using resource_prefix="
  89. end
  90. warn(
  91. '[DEPRECATED] ShopifyAPI::Base#prefix= is deprecated and will be removed in a future version. ' \
  92. 'Use `self.resource_prefix=` instead.'
  93. )
  94. self.resource_prefix = value
  95. end
  96. alias_method :set_prefix, :prefix=
  97. def init_prefix(resource)
  98. init_prefix_explicit(resource.to_s.pluralize, "#{resource}_id")
  99. end
  100. def init_prefix_explicit(resource_type, resource_id)
  101. self.resource_prefix = "#{resource_type}/:#{resource_id}/"
  102. define_method resource_id.to_sym do
  103. @prefix_options[resource_id]
  104. end
  105. end
  106. def early_july_pagination?
  107. !!early_july_pagination
  108. end
  109. def version_validation!(minimum_version)
  110. available_in_version = ShopifyAPI::ApiVersion.find_version(minimum_version)
  111. unless ShopifyAPI::Base.api_version >= available_in_version
  112. raise NotImplementedError, "The minimum supported version is #{minimum_version}."
  113. end
  114. end
  115. private
  116. attr_accessor :early_july_pagination
  117. def early_july_pagination_release!
  118. self.early_july_pagination = true
  119. end
  120. end
  121. def persisted?
  122. !id.nil?
  123. end
  124. private
  125. def only_id
  126. encode(:only => :id, :include => [], :methods => [])
  127. end
  128. end
  129. end