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
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
|
# 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_keys ⇒ Object
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_version ⇒ Object
139
140
141
|
# File 'lib/flex_commerce_api/base_resource.rb', line 139
def endpoint_version
raise NotImplementedError
end
|
63
64
65
66
67
68
|
# File 'lib/flex_commerce_api/base_resource.rb', line 63
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
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
|
.paginate ⇒ Object
Paginates the list of resources by a preset page size
|
# File 'lib/flex_commerce_api/base_resource.rb', line 48
|
.password ⇒ String
The password to use for authentication. This is the same as the access key token from the flex platform.
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
|
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
|
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
|
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_required ⇒ Object
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
|
.username ⇒ String
The username to use for authentication.
72
73
74
75
76
|
# 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
|
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
|
#freeze ⇒ Object
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
|
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_attributes ⇒ Object
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
|