Class: FlexCommerceApi::BaseResource
- Inherits:
-
JsonApiClient::Resource
- Object
- JsonApiClient::Resource
- FlexCommerceApi::BaseResource
show all
- Defined in:
- lib/flex_commerce_api/base_resource.rb
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
Constructor Details
#initialize(attrs = {}) ⇒ BaseResource
Ensures all attributes are with indifferent access
152
153
154
|
# File 'lib/flex_commerce_api/base_resource.rb', line 152
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
174
175
176
177
178
179
180
|
# File 'lib/flex_commerce_api/base_resource.rb', line 174
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 43
|
.append_version(base_url) ⇒ Object
134
135
136
|
# File 'lib/flex_commerce_api/base_resource.rb', line 134
def append_version(base_url)
"#{base_url}/#{endpoint_version}"
end
|
.capture_surrogate_keys ⇒ Object
88
89
90
91
92
93
94
95
|
# File 'lib/flex_commerce_api/base_resource.rb', line 88
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
37
38
39
40
41
|
# File 'lib/flex_commerce_api/base_resource.rb', line 37
def create!(*args)
create(*args).tap do |resource|
raise(::FlexCommerceApi::Error::RecordInvalid.new(resource)) unless resource.errors.empty?
end
end
|
.endpoint_version ⇒ Object
138
139
140
|
# File 'lib/flex_commerce_api/base_resource.rb', line 138
def endpoint_version
raise NotImplementedError
end
|
62
63
64
65
66
67
|
# File 'lib/flex_commerce_api/base_resource.rb', line 62
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
54
|
# File 'lib/flex_commerce_api/base_resource.rb', line 54
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
128
129
130
131
132
|
# File 'lib/flex_commerce_api/base_resource.rb', line 128
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 47
|
.password ⇒ String
The password to use for authentication. This is the same as the access key token from the flex platform.
80
81
82
|
# File 'lib/flex_commerce_api/base_resource.rb', line 80
def password
FlexCommerceApi.config.flex_api_key
end
|
.path(params = nil, record = nil) ⇒ Object
84
85
86
|
# File 'lib/flex_commerce_api/base_resource.rb', line 84
def path(params = nil, record = nil)
super(params)
end
|
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/flex_commerce_api/base_resource.rb', line 110
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
|
97
98
99
100
101
|
# File 'lib/flex_commerce_api/base_resource.rb', line 97
def reconfigure_all options = {}
self.subclasses.each do |sub|
sub.reconfigure_api_base options
end
end
|
103
104
105
106
107
108
|
# File 'lib/flex_commerce_api/base_resource.rb', line 103
def reconfigure_api_base options = {}
self.subclasses.each do |sub|
sub.reconfigure options
end
reconfigure options
end
|
.reload_connection_if_required ⇒ Object
123
124
125
|
# File 'lib/flex_commerce_api/base_resource.rb', line 123
def reload_connection_if_required
_build_connection(true) if connection_object
end
|
.username ⇒ String
The username to use for authentication.
71
72
73
74
75
|
# File 'lib/flex_commerce_api/base_resource.rb', line 71
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
182
183
184
|
# File 'lib/flex_commerce_api/base_resource.rb', line 182
def as_json_api(*args)
convert_relationship_attributes! super(*args)
end
|
#freeze ⇒ Object
143
144
145
146
147
148
149
|
# File 'lib/flex_commerce_api/base_resource.rb', line 143
def freeze
attributes.freeze
associations.freeze
links.freeze
relationships.freeze
self
end
|
165
166
167
168
169
170
171
172
|
# File 'lib/flex_commerce_api/base_resource.rb', line 165
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
161
162
163
|
# File 'lib/flex_commerce_api/base_resource.rb', line 161
def public_attributes
attributes.reject { |k, v| PRIVATE_ATTRIBUTES.include?(k.to_s) }
end
|
#save! ⇒ Object
156
157
158
159
|
# File 'lib/flex_commerce_api/base_resource.rb', line 156
def save!
return if save
raise_record_invalid
end
|