base.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. require 'shopify_api/version'
  2. module ShopifyAPI
  3. class Base < ActiveResource::Base
  4. extend Countable
  5. self.include_root_in_json = false
  6. self.headers['User-Agent'] = ["ShopifyAPI/#{ShopifyAPI::VERSION}",
  7. "ActiveResource/#{ActiveResource::VERSION::STRING}",
  8. "Ruby/#{RUBY_VERSION}"].join(' ')
  9. def encode(options = {})
  10. same = dup
  11. same.attributes = {self.class.element_name => same.attributes} if self.class.format.extension == 'json'
  12. same.send("to_#{self.class.format.extension}", options)
  13. end
  14. def as_json(options = nil)
  15. root = options[:root] if options.try(:key?, :root)
  16. if include_root_in_json
  17. root = self.class.model_name.element if root == true
  18. { root => serializable_hash(options) }
  19. else
  20. serializable_hash(options)
  21. end
  22. end
  23. class << self
  24. def headers
  25. if defined?(@headers)
  26. @headers
  27. elsif superclass != Object && superclass.headers
  28. superclass.headers
  29. else
  30. @headers ||= {}
  31. end
  32. end
  33. def activate_session(session)
  34. self.site = session.site
  35. self.headers.merge!('X-Shopify-Access-Token' => session.token)
  36. end
  37. def clear_session
  38. self.site = nil
  39. self.headers.delete('X-Shopify-Access-Token')
  40. end
  41. end
  42. def persisted?
  43. !id.nil?
  44. end
  45. private
  46. def only_id
  47. encode(:only => :id, :include => [], :methods => [])
  48. end
  49. end
  50. end