Class: FlexCommerceApi::BaseResource

Inherits:
JsonApiClient::Resource
  • Object
show all
Defined in:
lib/flex_commerce_api/base_resource.rb

Overview

Base class for all flex commerce models

Direct Known Subclasses

ApiBase, V2::ApiBase

Constant Summary collapse

PRIVATE_ATTRIBUTES =
%w(id type relationships links meta)
%w(related-categories related-static_pages related-resources related-files related-products)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ BaseResource

Ensures all attributes are with indifferent access



153
154
155
# File 'lib/flex_commerce_api/base_resource.rb', line 153

def initialize(attrs = {})
  super attrs.with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/flex_commerce_api/base_resource.rb', line 175

def method_missing(method, *args)
  if relationships and relationships.has_attribute?(method)
    super
  else
    has_attribute?(method) || method.to_s=~(/=$/) || method.to_s=~/!$/ ? super : nil
  end
end

Class Method Details

.allFlexCommerceApi::ApiBase[]

Returns all resources

Returns:



# File 'lib/flex_commerce_api/base_resource.rb', line 44

.append_version(base_url) ⇒ Object



135
136
137
# File 'lib/flex_commerce_api/base_resource.rb', line 135

def append_version(base_url)
  "#{base_url}/#{endpoint_version}"
end

.capture_surrogate_keysObject



89
90
91
92
93
94
95
96
# File 'lib/flex_commerce_api/base_resource.rb', line 89

def capture_surrogate_keys
  Thread.current[:shift_surrogate_keys] = []
  yield
  Thread.current[:shift_surrogate_keys].uniq!
  results = Thread.current[:shift_surrogate_keys].join(' ')
  Thread.current[:shift_surrogate_keys] = nil
  results
end

.create!(*args) ⇒ Object



38
39
40
41
42
# File 'lib/flex_commerce_api/base_resource.rb', line 38

def create!(*args)
  create(*args).tap do |resource|
    raise(::FlexCommerceApi::Error::RecordInvalid.new(resource)) unless resource.errors.empty?
  end
end

.endpoint_versionObject

Raises:

  • (NotImplementedError)


139
140
141
# File 'lib/flex_commerce_api/base_resource.rb', line 139

def endpoint_version
  raise NotImplementedError
end

.findFlexCommerceApi::ApiBase

Finds a resource

Parameters:

  • spec (String)

    The spec of what to find

Returns:

Raises:



63
64
65
66
67
68
# File 'lib/flex_commerce_api/base_resource.rb', line 63

def find(*args)
  # This is required as currently the underlying gem returns an array
  # even if 1 record is found.  This is inconsistent with active record
  result = super
  result.length <= 1 ? result.first : result
end

.find_allObject

Finds many resources, always returning an array, even if 1 result



55
# File 'lib/flex_commerce_api/base_resource.rb', line 55

alias_method :find_all, :find

.load(params) ⇒ Object

Allows ApiBase to be used as the class and the real class is then calculated from the type



129
130
131
132
133
# File 'lib/flex_commerce_api/base_resource.rb', line 129

def load(params)
  return super unless self == FlexCommerceApi::ApiBase
  klass = JsonApiClient::Utils.compute_type(FlexCommerce, params["type"].singularize.classify)
  klass.load(params)
end

.paginateObject

Paginates the list of resources by a preset page size

Parameters:

  • options (Hash)

    The options to paginate with

  • options (Numeric|String)

    :page The page to fetch



# File 'lib/flex_commerce_api/base_resource.rb', line 48

.passwordString

The password to use for authentication. This is the same as the access key token from the flex platform.

Returns:

  • (String)

    The password



81
82
83
# File 'lib/flex_commerce_api/base_resource.rb', line 81

def password
  FlexCommerceApi.config.flex_api_key
end

.path(params = nil, record = nil) ⇒ Object



85
86
87
# File 'lib/flex_commerce_api/base_resource.rb', line 85

def path(params = nil, record = nil)
  super(params)
end

.reconfigure(options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/flex_commerce_api/base_resource.rb', line 111

def reconfigure options = {}
  self.site = append_version(FlexCommerceApi.config.api_base_url)
  base_options = {
    adapter: FlexCommerceApi.config.adapter || :net_http,
    http_cache: FlexCommerceApi.config.http_cache,
    timeout: FlexCommerceApi.config.timeout,
    open_timeout: FlexCommerceApi.config.open_timeout
  }
  self.connection_options.delete(:include_previewed)
  self.connection_options = connection_options.merge(base_options).merge(options)
  reload_connection_if_required
end

.reconfigure_all(options = {}) ⇒ Object



98
99
100
101
102
# File 'lib/flex_commerce_api/base_resource.rb', line 98

def reconfigure_all options = {}
  self.subclasses.each do |sub|
    sub.reconfigure_api_base options
  end
end

.reconfigure_api_base(options = {}) ⇒ Object



104
105
106
107
108
109
# File 'lib/flex_commerce_api/base_resource.rb', line 104

def reconfigure_api_base options = {}
  self.subclasses.each do |sub|
    sub.reconfigure options
  end
  reconfigure options
end

.reload_connection_if_requiredObject



124
125
126
# File 'lib/flex_commerce_api/base_resource.rb', line 124

def reload_connection_if_required
  _build_connection(true) if connection_object
end

.usernameString

The username to use for authentication.

Returns:

  • (String)

    The username



72
73
74
75
76
# File 'lib/flex_commerce_api/base_resource.rb', line 72

def username
  username = FlexCommerceApi.config.
  username = URI.parse(site).path.split("/").reject(&:empty?).first if username.nil? || username.empty?
  username
end

Instance Method Details

#as_json_api(*args) ⇒ Object



183
184
185
# File 'lib/flex_commerce_api/base_resource.rb', line 183

def as_json_api(*args)
  convert_relationship_attributes! super(*args)
end

#freezeObject



144
145
146
147
148
149
150
# File 'lib/flex_commerce_api/base_resource.rb', line 144

def freeze
  attributes.freeze
  associations.freeze
  links.freeze
  relationships.freeze
  self
end

#meta_attribute(key) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/flex_commerce_api/base_resource.rb', line 166

def meta_attribute(key)
  begin
    return self.send(key) if RELATED_META_RESOURCES.include?(attributes[:meta_attributes][key][:data_type])
    attributes[:meta_attributes][key][:value]
  rescue NoMethodError
    nil
  end
end

#public_attributesObject



162
163
164
# File 'lib/flex_commerce_api/base_resource.rb', line 162

def public_attributes
  attributes.reject { |k, v| PRIVATE_ATTRIBUTES.include?(k.to_s) }
end

#save!Object



157
158
159
160
# File 'lib/flex_commerce_api/base_resource.rb', line 157

def save!
  return if save
  raise_record_invalid
end