base.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. def encode(options = {})
  12. same = dup
  13. same.attributes = {self.class.element_name => same.attributes} if self.class.format.extension == 'json'
  14. same.send("to_#{self.class.format.extension}", options)
  15. end
  16. def as_json(options = nil)
  17. root = options[:root] if options.try(:key?, :root)
  18. if include_root_in_json
  19. root = self.class.model_name.element if root == true
  20. { root => serializable_hash(options) }
  21. else
  22. serializable_hash(options)
  23. end
  24. end
  25. class << self
  26. threadsafe_attribute(:_api_version)
  27. def headers
  28. if _headers_defined?
  29. _headers
  30. elsif superclass != Object && superclass.headers
  31. superclass.headers
  32. else
  33. _headers ||= {}
  34. end
  35. end
  36. def activate_session(session)
  37. raise InvalidSessionError.new("Session cannot be nil") if session.nil?
  38. self.site = session.site
  39. self.headers.merge!('X-Shopify-Access-Token' => session.token)
  40. self.api_version = session.api_version
  41. end
  42. def clear_session
  43. self.site = nil
  44. self.password = nil
  45. self.user = nil
  46. self.api_version = 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. end
  55. end
  56. def api_version=(version)
  57. self._api_version = version.nil? ? nil : ApiVersion.coerce_to_version(version)
  58. end
  59. def prefix(options = {})
  60. api_version.construct_api_path(resource_prefix(options))
  61. end
  62. def prefix_source
  63. ''
  64. end
  65. def resource_prefix(_options = {})
  66. ''
  67. end
  68. # Sets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>).
  69. # Default value is <tt>site.path</tt>.
  70. def resource_prefix=(value)
  71. @prefix_parameters = nil
  72. resource_prefix_call = value.gsub(/:\w+/) { |key| "\#{URI.parser.escape options[#{key}].to_s}" }
  73. silence_warnings do
  74. # Redefine the new methods.
  75. instance_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
  76. def prefix_source() "#{value}" end
  77. def resource_prefix(options={}) "#{resource_prefix_call}" end
  78. RUBY_EVAL
  79. end
  80. rescue => e
  81. logger&.error("Couldn't set prefix: #{e}\n #{code}")
  82. raise
  83. end
  84. def prefix=(value)
  85. if value.start_with?('/admin')
  86. raise ArgumentError, "'#{value}' can no longer start /admin/. Change to using resource_prefix="
  87. end
  88. warn(
  89. '[DEPRECATED] ShopifyAPI::Base#prefix= is deprecated and will be removed in a future version. ' \
  90. 'Use `self.resource_prefix=` instead.'
  91. )
  92. self.resource_prefix = value
  93. end
  94. alias_method :set_prefix, :prefix=
  95. def init_prefix(resource)
  96. init_prefix_explicit(resource.to_s.pluralize, "#{resource}_id")
  97. end
  98. def init_prefix_explicit(resource_type, resource_id)
  99. self.resource_prefix = "#{resource_type}/:#{resource_id}/"
  100. define_method resource_id.to_sym do
  101. @prefix_options[resource_id]
  102. end
  103. end
  104. end
  105. def persisted?
  106. !id.nil?
  107. end
  108. private
  109. def only_id
  110. encode(:only => :id, :include => [], :methods => [])
  111. end
  112. end
  113. end