1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- require 'shopify_api/version'
- module ShopifyAPI
- class Base < ActiveResource::Base
- extend Countable
- self.include_root_in_json = false
- self.headers['User-Agent'] = ["ShopifyAPI/#{ShopifyAPI::VERSION}",
- "ActiveResource/#{ActiveResource::VERSION::STRING}",
- "Ruby/#{RUBY_VERSION}"].join(' ')
- def encode(options = {})
- same = dup
- same.attributes = {self.class.element_name => same.attributes} if self.class.format.extension == 'json'
- same.send("to_#{self.class.format.extension}", options)
- end
- def as_json(options = nil)
- root = options[:root] if options.try(:key?, :root)
- if include_root_in_json
- root = self.class.model_name.element if root == true
- { root => serializable_hash(options) }
- else
- serializable_hash(options)
- end
- end
- class << self
- if ActiveResource::VERSION::MAJOR == 4 && ActiveResource::VERSION::PRE == 'threadsafe'
- def headers
- if _headers_defined?
- _headers
- elsif superclass != Object && superclass.headers
- superclass.headers
- else
- _headers ||= {}
- end
- end
- else
- def headers
- if defined?(@headers)
- @headers
- elsif superclass != Object && superclass.headers
- superclass.headers
- else
- @headers ||= {}
- end
- end
- end
- def activate_session(session)
- self.site = session.site
- self.headers.merge!('X-Shopify-Access-Token' => session.token)
- end
- def clear_session
- self.site = nil
- self.headers.delete('X-Shopify-Access-Token')
- end
- def init_prefix(resource)
- init_prefix_explicit(resource.to_s.pluralize, "#{resource}_id")
- end
- def init_prefix_explicit(resource_type, resource_id)
- self.prefix = "/admin/#{resource_type}/:#{resource_id}/"
- define_method resource_id.to_sym do
- @prefix_options[resource_id]
- end
- end
- end
- def persisted?
- !id.nil?
- end
- private
- def only_id
- encode(:only => :id, :include => [], :methods => [])
- end
- end
- end
|