Class: ShopifyAPI::Base

Inherits:
ActiveResource::Base show all
Extended by:
Countable
Defined in:
lib/shopify_api/resources/base.rb

Defined Under Namespace

Classes: InvalidSessionError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Countable

count

Methods inherited from ActiveResource::Base

delete, headers

Class Method Details

.activate_session(session) ⇒ Object



53
54
55
56
57
# File 'lib/shopify_api/resources/base.rb', line 53

def activate_session(session)
  raise InvalidSessionError.new("Session cannot be nil") if session.nil?
  self.site = session.site
  self.headers.merge!('X-Shopify-Access-Token' => session.token)
end

.clear_sessionObject



59
60
61
62
63
64
# File 'lib/shopify_api/resources/base.rb', line 59

def clear_session
  self.site = nil
  self.password = nil
  self.user = nil
  self.headers.delete('X-Shopify-Access-Token')
end

.init_prefix(resource) ⇒ Object



66
67
68
# File 'lib/shopify_api/resources/base.rb', line 66

def init_prefix(resource)
  init_prefix_explicit(resource.to_s.pluralize, "#{resource}_id")
end

.init_prefix_explicit(resource_type, resource_id) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/shopify_api/resources/base.rb', line 70

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

Instance Method Details

#as_json(options = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/shopify_api/resources/base.rb', line 20

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

#encode(options = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/shopify_api/resources/base.rb', line 13

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

#headersObject



32
33
34
35
36
37
38
39
40
# File 'lib/shopify_api/resources/base.rb', line 32

def headers
  if _headers_defined?
    _headers
  elsif superclass != Object && superclass.headers
    superclass.headers
  else
    _headers ||= {}
  end
end

#persisted?Boolean



79
80
81
# File 'lib/shopify_api/resources/base.rb', line 79

def persisted?
  !id.nil?
end