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



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

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



58
59
60
61
# File 'lib/shopify_api/resources/base.rb', line 58

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

.init_prefix(resource) ⇒ Object



63
64
65
# File 'lib/shopify_api/resources/base.rb', line 63

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

.init_prefix_explicit(resource_type, resource_id) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/shopify_api/resources/base.rb', line 67

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



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

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



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

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



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

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

#persisted?Boolean



76
77
78
# File 'lib/shopify_api/resources/base.rb', line 76

def persisted?
  !id.nil?
end