Class: FlexCommerceApi::BaseResource
Overview
Base class for all flex commerce models
Constant Summary
collapse
- PRIVATE_ATTRIBUTES =
%w(id type relationships links meta)
- RELATED_META_RESOURCES =
%w(related-categories related-static_pages related-resources related-files related-products)
Class Method Summary
collapse
Instance Method Summary
collapse
#save
Constructor Details
#initialize(attrs = {}) ⇒ BaseResource
Ensures all attributes are with indifferent access
149
150
151
|
# File 'lib/flex_commerce_api/base_resource.rb', line 149
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
171
172
173
174
175
176
177
|
# File 'lib/flex_commerce_api/base_resource.rb', line 171
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
|
# File 'lib/flex_commerce_api/base_resource.rb', line 40
|
.append_version(base_url) ⇒ Object
131
132
133
|
# File 'lib/flex_commerce_api/base_resource.rb', line 131
def append_version(base_url)
"#{base_url}/#{endpoint_version}"
end
|
.capture_surrogate_keys ⇒ Object
85
86
87
88
89
90
91
92
|
# File 'lib/flex_commerce_api/base_resource.rb', line 85
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
34
35
36
37
38
|
# File 'lib/flex_commerce_api/base_resource.rb', line 34
def create!(*args)
create(*args).tap do |resource|
raise(::FlexCommerceApi::Error::RecordInvalid.new(resource)) unless resource.errors.empty?
end
end
|
.endpoint_version ⇒ Object
135
136
137
|
# File 'lib/flex_commerce_api/base_resource.rb', line 135
def endpoint_version
raise NotImplementedError
end
|
59
60
61
62
63
64
|
# File 'lib/flex_commerce_api/base_resource.rb', line 59
def find(*args)
result = super
result.length <= 1 ? result.first : result
end
|
.find_all ⇒ Object
Finds many resources, always returning an array, even if 1 result
51
|
# File 'lib/flex_commerce_api/base_resource.rb', line 51
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
125
126
127
128
129
|
# File 'lib/flex_commerce_api/base_resource.rb', line 125
def load(params)
return super unless self == FlexCommerceApi::ApiBase
klass = JsonApiClient::Utils.compute_type(FlexCommerce, params["type"].singularize.classify)
klass.load(params)
end
|
.paginate ⇒ Object
Paginates the list of resources by a preset page size
|
# File 'lib/flex_commerce_api/base_resource.rb', line 44
|
.password ⇒ String
The password to use for authentication. This is the same as the access key token from the flex platform.
77
78
79
|
# File 'lib/flex_commerce_api/base_resource.rb', line 77
def password
FlexCommerceApi.config.flex_api_key
end
|
.path(params = nil, record = nil) ⇒ Object
81
82
83
|
# File 'lib/flex_commerce_api/base_resource.rb', line 81
def path(params = nil, record = nil)
super(params)
end
|
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/flex_commerce_api/base_resource.rb', line 107
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
|
94
95
96
97
98
|
# File 'lib/flex_commerce_api/base_resource.rb', line 94
def reconfigure_all options = {}
self.subclasses.each do |sub|
sub.reconfigure_api_base options
end
end
|
100
101
102
103
104
105
|
# File 'lib/flex_commerce_api/base_resource.rb', line 100
def reconfigure_api_base options = {}
self.subclasses.each do |sub|
sub.reconfigure options
end
reconfigure options
end
|
.reload_connection_if_required ⇒ Object
120
121
122
|
# File 'lib/flex_commerce_api/base_resource.rb', line 120
def reload_connection_if_required
_build_connection(true) if connection_object
end
|
.username ⇒ String
The username to use for authentication.
68
69
70
71
72
|
# File 'lib/flex_commerce_api/base_resource.rb', line 68
def username
username = FlexCommerceApi.config.flex_account
username = URI.parse(site).path.split("/").reject(&:empty?).first if username.nil? || username.empty?
username
end
|
Instance Method Details
#as_json_api(*args) ⇒ Object
179
180
181
|
# File 'lib/flex_commerce_api/base_resource.rb', line 179
def as_json_api(*args)
convert_relationship_attributes! super(*args)
end
|
#freeze ⇒ Object
140
141
142
143
144
145
146
|
# File 'lib/flex_commerce_api/base_resource.rb', line 140
def freeze
attributes.freeze
associations.freeze
links.freeze
relationships.freeze
self
end
|
162
163
164
165
166
167
168
169
|
# File 'lib/flex_commerce_api/base_resource.rb', line 162
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_attributes ⇒ Object
158
159
160
|
# File 'lib/flex_commerce_api/base_resource.rb', line 158
def public_attributes
attributes.reject { |k, v| PRIVATE_ATTRIBUTES.include?(k.to_s) }
end
|
#save! ⇒ Object
153
154
155
156
|
# File 'lib/flex_commerce_api/base_resource.rb', line 153
def save!
return if save
raise_record_invalid
end
|