class FlexCommerceApi::BaseResource
Base class for all flex commerce models
Constants
- PRIVATE_ATTRIBUTES
- RELATED_META_RESOURCES
Public Class Methods
append_version(base_url)
click to toggle source
# File lib/flex_commerce_api/base_resource.rb, line 135 def append_version(base_url) "#{base_url}/#{endpoint_version}" end
capture_surrogate_keys() { || ... }
click to toggle source
# 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)
click to toggle source
# 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_version()
click to toggle source
# File lib/flex_commerce_api/base_resource.rb, line 139 def endpoint_version raise NotImplementedError end
find(*args)
click to toggle source
@method find @param [String] spec The spec of what to find
Finds a resource @return [FlexCommerceApi::ApiBase] resource The resource found @raise [FlexCommerceApi::Error::NotFound] If not found
Calls superclass method
# 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
Also aliased as: find_all
find_all(*args)
@method find_all
Finds many resources, always returning an array, even if 1 result
Alias for: find
load(params)
click to toggle source
Allows ApiBase
to be used as the class and the real class is then calculated from the type
Calls superclass method
# 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
new(attrs = {})
click to toggle source
Ensures all attributes are with indifferent access
Calls superclass method
# File lib/flex_commerce_api/base_resource.rb, line 153 def initialize(attrs = {}) super attrs.with_indifferent_access end
password()
click to toggle source
The password to use for authentication. This is the same as the access key token from the flex platform. @return [String] The password
# File lib/flex_commerce_api/base_resource.rb, line 81 def password FlexCommerceApi.config.flex_api_key end
path(params = nil, record = nil)
click to toggle source
Calls superclass method
# File lib/flex_commerce_api/base_resource.rb, line 85 def path(params = nil, record = nil) super(params) end
reconfigure(options = {})
click to toggle source
# 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 = {})
click to toggle source
# 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 = {})
click to toggle source
# 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_required()
click to toggle source
# File lib/flex_commerce_api/base_resource.rb, line 124 def reload_connection_if_required _build_connection(true) if connection_object end
username()
click to toggle source
The username to use for authentication. @return [String] The username
# File lib/flex_commerce_api/base_resource.rb, line 72 def username username = FlexCommerceApi.config.flex_account username = URI.parse(site).path.split("/").reject(&:empty?).first if username.nil? || username.empty? username end
Public Instance Methods
as_json_api(*args)
click to toggle source
Calls superclass method
# File lib/flex_commerce_api/base_resource.rb, line 183 def as_json_api(*args) convert_relationship_attributes! super(*args) end
freeze()
click to toggle source
# 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)
click to toggle source
# 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
method_missing(method, *args)
click to toggle source
Calls superclass method
# 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
public_attributes()
click to toggle source
# 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!()
click to toggle source
# File lib/flex_commerce_api/base_resource.rb, line 157 def save! return if save raise_record_invalid end
Private Instance Methods
convert_relationship_attribute(data)
click to toggle source
# File lib/flex_commerce_api/base_resource.rb, line 201 def convert_relationship_attribute(data) case data when Array data.map do |d| convert_relationship_attribute(d) end when FlexCommerceApi::ApiBase data.as_json_api else data end end
convert_relationship_attributes!(h)
click to toggle source
# File lib/flex_commerce_api/base_resource.rb, line 189 def convert_relationship_attributes!(h) valid_relationships = relationship_attributes (h["attributes"].keys & valid_relationships).each do |attr| h["attributes"][attr] = convert_relationship_attribute(h["attributes"][attr]) end h end
has_many_association_proxy(assoc_name, real_instance, options = {})
click to toggle source
This is temporary code - eventually this will be in the lower level gem
# File lib/flex_commerce_api/base_resource.rb, line 219 def has_many_association_proxy(assoc_name, real_instance, options = {}) JsonApiClientExtension::HasManyAssociationProxy.new(real_instance, self, assoc_name, options) end
raise_record_invalid()
click to toggle source
# File lib/flex_commerce_api/base_resource.rb, line 214 def raise_record_invalid raise(::FlexCommerceApi::Error::RecordInvalid.new(self)) end
relationship_attributes()
click to toggle source
# File lib/flex_commerce_api/base_resource.rb, line 197 def relationship_attributes @relationship_attributes ||= self.class.associations.map { |a| a.is_a?(JsonApiClient::Associations::HasMany::Association) ? "#{a.attr_name}_resources" : "#{a.attr_name}_resource" } end