Module: Epages::REST::Utils

Included in:
Carts, Categories, Customers, Newsletters, Orders, Products, Sales, TaxClasses, Webhooks
Defined in:
lib/epages/rest/utils.rb

Instance Method Summary collapse

Instance Method Details

#epages_id(object) ⇒ Object



137
138
139
140
141
# File 'lib/epages/rest/utils.rb', line 137

def epages_id(object)
  return object if object.class == String
  return if object.class.name.deconstantize != 'Epages'
  object.send(:id)
end

#format_date(date) ⇒ Object



197
198
199
# File 'lib/epages/rest/utils.rb', line 197

def format_date(date)
  (date.is_a?(Date) ? date : DateTime.parse(date)).strftime
end

#format_dates_options(options) ⇒ Object



201
202
203
204
205
# File 'lib/epages/rest/utils.rb', line 201

def format_dates_options(options)
  options[:created_before] = format_date(options[:created_before]) if options[:created_before]
  options[:created_after] = format_date(options[:created_after]) if options[:created_after]
  options
end


157
158
159
160
161
162
163
164
165
166
# File 'lib/epages/rest/utils.rb', line 157

def parse_legal_info(data)
  {
    contact_info: Epages::ContentPageSummary.new(data[:contactInformation].nil? ? {'':''} : data[:contactInformation]),
    privacy_policy: Epages::ContentPageSummary.new(data[:privacyPolicy].nil? ? {'':''} : data[:privacyPolicy]),
    terms_and_conditions: Epages::ContentPageSummary.new(data[:termsAndConditions].nil? ? {'':''} : data[:termsAndConditions]),
    rights_of_withdrawal: Epages::ContentPageSummary.new(data[:rightsOfWithdrawal].nil? ? {'':''} : data[:rightsOfWithdrawal]),
    shipping_info: Epages::ContentPageSummary.new(data[:shippingInformation].nil? ? {'':''} : data[:shippingInformation]),
    links: parse_links(data[:links]),
  }
end


180
181
182
# File 'lib/epages/rest/utils.rb', line 180

def parse_links(data)
  data.collect { |link| Epages::Link.new(link) }
end

#parse_price_info(data) ⇒ Object



176
177
178
# File 'lib/epages/rest/utils.rb', line 176

def parse_price_info(data)
  Epages::PriceInfo.new(data)
end

#parse_product_lowest_price(data) ⇒ Object



150
151
152
153
154
155
# File 'lib/epages/rest/utils.rb', line 150

def parse_product_lowest_price(data)
  {
    price_info: parse_price_info(data[:priceInfo]),
    links: parse_links(data[:links]),
  }
end

#parse_product_variations(data) ⇒ Object



143
144
145
146
147
148
# File 'lib/epages/rest/utils.rb', line 143

def parse_product_variations(data)
  {
    variation_attributes: parse_variations(data),
    items: parse_variation_object(data),
  }
end

#parse_suggestions_to_products(data) ⇒ Object



184
185
186
187
# File 'lib/epages/rest/utils.rb', line 184

def parse_suggestions_to_products(data)
  ids = data[:products].collect { |p| p[:link][:href].split('/').last }
  parallel_calls(product: ids)[:product]
end

#parse_variation_object(data) ⇒ Object



172
173
174
# File 'lib/epages/rest/utils.rb', line 172

def parse_variation_object(data)
  data[:items].collect { |el| Epages::Variation.new(el) }
end

#parse_variations(data) ⇒ Object



168
169
170
# File 'lib/epages/rest/utils.rb', line 168

def parse_variations(data)
  data[:variationAttributes].collect { |el| Epages::VariationAttribute.new(el) }
end

#perform_delete_request(path, options = {}) ⇒ Object

Parameters:

  • path (String)
  • options (Hash) (defaults to: {})


133
134
135
# File 'lib/epages/rest/utils.rb', line 133

def perform_delete_request(path, options = {})
  perform_request(:delete, path, options)
end

#perform_delete_with_object(path, options, klass) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)
  • klass (Class)


42
43
44
# File 'lib/epages/rest/utils.rb', line 42

def perform_delete_with_object(path, options, klass)
  perform_request_with_object(:delete, path, options, klass)
end

#perform_get_request(path, options = {}) ⇒ Object

Parameters:

  • path (String)
  • options (Hash) (defaults to: {})


115
116
117
# File 'lib/epages/rest/utils.rb', line 115

def perform_get_request(path, options = {})
  perform_request(:get, path, options)
end

#perform_get_with_key_and_objects(path, options, key, klass) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)
  • key (Symbol)
  • klass (Class)


91
92
93
# File 'lib/epages/rest/utils.rb', line 91

def perform_get_with_key_and_objects(path, options, key, klass)
  perform_request_with_key_and_objects(:get, path, options, key, klass)
end

#perform_get_with_object(path, options, klass) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)
  • klass (Class)


14
15
16
# File 'lib/epages/rest/utils.rb', line 14

def perform_get_with_object(path, options, klass)
  perform_request_with_object(:get, path, options, klass)
end

#perform_get_with_objects(path, options, klass) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)
  • klass (Class)


58
59
60
# File 'lib/epages/rest/utils.rb', line 58

def perform_get_with_objects(path, options, klass)
  perform_request_with_objects(:get, path, options, klass)
end

#perform_multipart_post_with_objects(path, image, klass) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)
  • klass (Class)


72
73
74
75
# File 'lib/epages/rest/utils.rb', line 72

def perform_multipart_post_with_objects(path, image, klass)
  response = perform_request(:multipart_post, path, file: image)
  response.key?('items') ? response[:items].collect { |data| klass.new(data) } : klass.new(response)
end

#perform_patch_with_object(path, options, klass) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)
  • klass (Class)


35
36
37
# File 'lib/epages/rest/utils.rb', line 35

def perform_patch_with_object(path, options, klass)
  perform_request_with_object(:patch, path, options, klass)
end

#perform_post_request(path, options = {}) ⇒ Object

Parameters:

  • path (String)
  • options (Hash) (defaults to: {})


121
122
123
# File 'lib/epages/rest/utils.rb', line 121

def perform_post_request(path, options = {})
  perform_request(:post, path, options)
end

#perform_post_with_key_and_objects(path, options, key, klass) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)
  • key (Symbol)
  • klass (Class)


99
100
101
# File 'lib/epages/rest/utils.rb', line 99

def perform_post_with_key_and_objects(path, options, key, klass)
  perform_request_with_key_and_objects(:post, path, options, key, klass)
end

#perform_post_with_object(path, options, klass) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)
  • klass (Class)


21
22
23
# File 'lib/epages/rest/utils.rb', line 21

def perform_post_with_object(path, options, klass)
  perform_request_with_object(:post, path, options, klass)
end

#perform_post_with_objects(path, options, klass) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)
  • klass (Class)


65
66
67
# File 'lib/epages/rest/utils.rb', line 65

def perform_post_with_objects(path, options, klass)
  perform_request_with_objects(:post, path, options, klass)
end

#perform_put_request(path, options = {}) ⇒ Object

Parameters:

  • path (String)
  • options (Hash) (defaults to: {})


127
128
129
# File 'lib/epages/rest/utils.rb', line 127

def perform_put_request(path, options = {})
  perform_request(:put, path, options)
end

#perform_put_with_object(path, options, klass) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)
  • klass (Class)


28
29
30
# File 'lib/epages/rest/utils.rb', line 28

def perform_put_with_object(path, options, klass)
  perform_request_with_object(:put, path, options, klass)
end

#perform_request(request_method, path, options = {}) ⇒ Object

Parameters:

  • request_method (Symbol)
  • path (String)
  • options (Hash) (defaults to: {})


7
8
9
# File 'lib/epages/rest/utils.rb', line 7

def perform_request(request_method, path, options = {})
  Epages::REST::Request.new(self, request_method, path, options).perform
end

#perform_request_with_key_and_objects(request_method, path, options, key, klass) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)
  • key (Symbol)
  • klass (Class)


107
108
109
110
111
# File 'lib/epages/rest/utils.rb', line 107

def perform_request_with_key_and_objects(request_method, path, options, key, klass)
  perform_request(request_method, path, options)[key].collect do |element|
    klass.new(element)
  end
end

#perform_request_with_object(request_method, path, options, klass) ⇒ Object

Parameters:

  • request_method (Symbol)
  • path (String)
  • options (Hash)
  • klass (Class)


50
51
52
53
# File 'lib/epages/rest/utils.rb', line 50

def perform_request_with_object(request_method, path, options, klass)
  response = perform_request(request_method, path, options)
  klass.new(response)
end

#perform_request_with_objects(request_method, path, options, klass) ⇒ Object

Parameters:

  • request_method (Symbol)
  • path (String)
  • options (Hash)
  • klass (Class)


81
82
83
84
85
# File 'lib/epages/rest/utils.rb', line 81

def perform_request_with_objects(request_method, path, options, klass)
  perform_request(request_method, path, options).collect do |element|
    klass.new(element)
  end
end

#process_thread(key, value, index = nil) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/epages/rest/utils.rb', line 189

def process_thread(key, value, index = nil)
  Thread.new do
    Thread.current[:name] = key
    Thread.current[:index] = index if index
    Thread.current[:result] = send(key, value)
  end
end