module Epages::Utils

Public Instance Methods

build_shop_from(shop) click to toggle source

returns a shop. If [shop] is not a Epages::REST::Shop create one calling the [shop] method shop_mame

# File lib/epages/utils.rb, line 77
def build_shop_from(shop)
  return shop if shop.is_a? Epages::REST::Shop
  Epages::REST::Shop.new(shop.host, shop.shop_name)
end
camelize_keys(hash) click to toggle source

return the hash with the keys converted to lower camelcase

@param hash [Hash]

# File lib/epages/utils.rb, line 53
def camelize_keys(hash)
  return unless hash.is_a?(Hash)
  other = {}
  hash.keys.each do |k|
    key = k.to_s.camelize(:lower).to_sym
    other[key] = hash[k]
  end
  other
end
camelize_words(string) click to toggle source

return the string passed as a parameter with all the words camelized

@param string [Strimg], [Symbol]

# File lib/epages/utils.rb, line 116
def camelize_words(string)
  string.to_s.gsub(/(\w+)/) { |s| s.camelize(:lower) }
end
options_to_multipart_request(options) click to toggle source

returns the json body for the patch calls

@param options [Hash]

# File lib/epages/utils.rb, line 107
def options_to_multipart_request(options)
  file = options[:file]
  @request_method = :post
  {image: HTTP::FormData::File.new(file, filename: File.basename(file), mime_type: mime_type(File.basename(file)))}
end
options_to_patch_request(options) click to toggle source

returns the json body for the patch calls

@param options [Hash]

# File lib/epages/utils.rb, line 97
def options_to_patch_request(options)
  json = []
  Array[options.delete(:remove)].flatten.compact.each { |i| json << {'op' => 'remove', 'path' => "/#{camelize_words(i)}"} }
  options.each { |k, v| json << {'op' => 'add', 'path' => "/#{camelize_words(k)}", 'value' => v} }
  json.collect { |i| Hash[i.each_pair.to_a] }
end
parse_attribute_as(name, data, klass = NilClass) click to toggle source

creates a instance variable (underscore) from the [data] if [klass] is passed, creates a object of [klass] initialized with data

@param name [Symbol] [String] @param data [Hash] @param klass [Class]

# File lib/epages/utils.rb, line 16
def parse_attribute_as(name, data, klass = NilClass)
  return if data.nil?
  value =
    case klass.name
    when 'NilClass' then data
    when 'DateTime' then klass.parse(data)
    else klass.new(data)
    end
  instance_variable_set("@#{name.to_s.underscore}", value)
end
parse_attribute_as_array_of(name, data, klass) click to toggle source

creates a instance variable (underscore). This variable contains an array of <klass> or empty array

@param name [Symbol] [String] @param data [Hash] @param klass [Class]

# File lib/epages/utils.rb, line 32
def parse_attribute_as_array_of(name, data, klass)
  value = data.class != Array || data.empty? ? [] : data.collect { |i| klass.new(i) }
  instance_variable_set("@#{name.to_s.underscore}", value)
end
parse_attributes(data, klass = nil) click to toggle source

creates instance variables from the data if klass is passed, create a object initializing with data

@param data [Hash] @param klass [Class]

# File lib/epages/utils.rb, line 42
def parse_attributes(data, klass = nil)
  data.keys.each do |key|
    next if data[key.to_sym].nil?
    value = klass.nil? ? data[key.to_sym] : klass.new(data[key.to_sym])
    instance_variable_set("@#{key.to_s.underscore}", value)
  end
end
symbolize_keys!(object) click to toggle source

returns the object replacing all the keys as symbols

@param object [Hash], [Array]

# File lib/epages/utils.rb, line 85
def symbolize_keys!(object)
  if object.is_a?(Array)
    object.each_with_index { |val, index| object[index] = symbolize_keys!(val) }
  elsif object.is_a?(Hash)
    object.keys.each { |key| object[key.to_sym] = symbolize_keys!(object.delete(key)) }
  end
  object
end
to_query_options(options) click to toggle source

return a string that acts as a query parameters (for POST calls)

@param options [Hash]

# File lib/epages/utils.rb, line 123
def to_query_options(options)
  return '' if !options.is_a?(Hash) || options.empty?
  "?#{options.map{ |k,v| "#{k}=#{v}" }.join('&')}"
end
underscorize_keys(hash) click to toggle source

return the hash with the keys converted to underscore

@param hash [Hash]

# File lib/epages/utils.rb, line 66
def underscorize_keys(hash)
  return unless hash.is_a?(Hash)
  hash.keys.each do |k|
    key = k.to_s.underscore.to_sym
    hash[key] = hash[k]
    hash.delete(k) if key != k
  end
  hash
end