Class: ShopifyAPI::Base

Inherits:
ActiveResource::Base
  • Object
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

Class Method Details

.activate_session(session) ⇒ Object


45
46
47
48
49
50
# File 'lib/shopify_api/resources/base.rb', line 45

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

.api_versionObject


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

def api_version
  if _api_version_defined?
    _api_version
  elsif superclass != Object && superclass.site
    superclass.api_version.dup.freeze
  else
    ApiVersion::NullVersion
  end
end

.api_version=(version) ⇒ Object


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

def api_version=(version)
  self._api_version = if ApiVersion::NullVersion.matches?(version)
    ApiVersion::NullVersion
  else
    ApiVersion.find_version(version)
  end
end

.clear_sessionObject


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

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

.early_july_pagination?Boolean

Returns:

  • (Boolean)

134
135
136
# File 'lib/shopify_api/resources/base.rb', line 134

def early_july_pagination?
  !!early_july_pagination
end

.headersObject


35
36
37
38
39
40
41
42
43
# File 'lib/shopify_api/resources/base.rb', line 35

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

.init_prefix(resource) ⇒ Object


122
123
124
# File 'lib/shopify_api/resources/base.rb', line 122

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

.init_prefix_explicit(resource_type, resource_id) ⇒ Object


126
127
128
129
130
131
132
# File 'lib/shopify_api/resources/base.rb', line 126

def init_prefix_explicit(resource_type, resource_id)
  self.resource_prefix = "#{resource_type}/:#{resource_id}/"

  define_method(resource_id.to_sym) do
    @prefix_options[resource_id]
  end
end

.prefix(options = {}) ⇒ Object


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

def prefix(options = {})
  api_version.construct_api_path(resource_prefix(options))
end

.prefix=(value) ⇒ Object Also known as: set_prefix


108
109
110
111
112
113
114
115
116
117
118
# File 'lib/shopify_api/resources/base.rb', line 108

def prefix=(value)
  if value.start_with?('/admin')
    raise ArgumentError, "'#{value}' can no longer start /admin/. Change to using resource_prefix="
  end

  warn(
    '[DEPRECATED] ShopifyAPI::Base#prefix= is deprecated and will be removed in a future version. ' \
      'Use `self.resource_prefix=` instead.'
  )
  self.resource_prefix = value
end

.prefix_sourceObject


81
82
83
# File 'lib/shopify_api/resources/base.rb', line 81

def prefix_source
  ''
end

.resource_prefix(_options = {}) ⇒ Object


85
86
87
# File 'lib/shopify_api/resources/base.rb', line 85

def resource_prefix(_options = {})
  ''
end

.resource_prefix=(value) ⇒ Object

Sets the prefix for a resource's nested URL (e.g., prefix/collectionname/1.json). Default value is site.path.


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/shopify_api/resources/base.rb', line 91

def resource_prefix=(value)
  @prefix_parameters = nil

  resource_prefix_call = value.gsub(/:\w+/) { |key| "\#{URI::DEFAULT_PARSER.escape options[#{key}].to_s}" }

  silence_warnings do
    # Redefine the new methods.
    instance_eval(<<-RUBY_EVAL, __FILE__, __LINE__ + 1)
      def prefix_source() "#{value}" end
      def resource_prefix(options={}) "#{resource_prefix_call}" end
    RUBY_EVAL
  end
rescue => e
  logger&.error("Couldn't set prefix: #{e}\n  #{code}")
  raise
end

.version_validation!(minimum_version) ⇒ Object


138
139
140
141
142
143
144
# File 'lib/shopify_api/resources/base.rb', line 138

def version_validation!(minimum_version)
  available_in_version = ShopifyAPI::ApiVersion.find_version(minimum_version)

  unless ShopifyAPI::Base.api_version >= available_in_version
    raise NotImplementedError, "The minimum supported version is #{minimum_version}."
  end
end

Instance Method Details

#as_json(options = nil) ⇒ Object


23
24
25
26
27
28
29
30
31
# File 'lib/shopify_api/resources/base.rb', line 23

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


16
17
18
19
20
21
# File 'lib/shopify_api/resources/base.rb', line 16

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

#persisted?Boolean

Returns:

  • (Boolean)

155
156
157
# File 'lib/shopify_api/resources/base.rb', line 155

def persisted?
  !id.nil?
end