module Epages::REST::Utils

Public Instance Methods

epages_id(object) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
parse_price_info(data) click to toggle source
# File lib/epages/rest/utils.rb, line 176
def parse_price_info(data)
  Epages::PriceInfo.new(data)
end
parse_product_lowest_price(data) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source

@param path [String] @param options [Hash]

# 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) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# 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 = {}) click to toggle source

@param path [String] @param options [Hash]

# 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) click to toggle source

@param path [String] @param options [Hash] @param key [Symbol] @param klass [Class]

# 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) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# 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) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# 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) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# 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) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# 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 = {}) click to toggle source

@param path [String] @param options [Hash]

# 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) click to toggle source

@param path [String] @param options [Hash] @param key [Symbol] @param klass [Class]

# 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) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# 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) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# 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 = {}) click to toggle source

@param path [String] @param options [Hash]

# 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) click to toggle source

@param path [String] @param options [Hash] @param klass [Class]

# 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 = {}) click to toggle source

@param request_method [Symbol] @param path [String] @param options [Hash]

# 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) click to toggle source

@param path [String] @param options [Hash] @param key [Symbol] @param klass [Class]

# 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) click to toggle source

@param request_method [Symbol] @param path [String] @param options [Hash] @param klass [Class]

# 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) click to toggle source

@param request_method [Symbol] @param path [String] @param options [Hash] @param klass [Class]

# 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) click to toggle source
# 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