Module: Epages::Utils

Class Method Summary collapse

Class Method Details

.build_shop_from(shop) ⇒ Object

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



77
78
79
80
# 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) ⇒ Object

return the hash with the keys converted to lower camelcase

Parameters:

  • hash (Hash)


53
54
55
56
57
58
59
60
61
# 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) ⇒ Object

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

Parameters:

  • string (Strimg)

    , [Symbol]



116
117
118
# 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) ⇒ Object

returns the json body for the patch calls

Parameters:

  • options (Hash)


107
108
109
110
111
# 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) ⇒ Object

returns the json body for the patch calls

Parameters:

  • options (Hash)


97
98
99
100
101
102
# 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) ⇒ Object

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

Parameters:

  • name (Symbol)
    String
  • data (Hash)
  • klass (Class) (defaults to: NilClass)


16
17
18
19
20
21
22
23
24
25
# 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) ⇒ Object

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

Parameters:

  • name (Symbol)
    String
  • data (Hash)
  • klass (Class)


32
33
34
35
# 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) ⇒ Object

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

Parameters:

  • data (Hash)
  • klass (Class) (defaults to: nil)


42
43
44
45
46
47
48
# 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) ⇒ Object

returns the object replacing all the keys as symbols

Parameters:

  • object (Hash)

    , [Array]



85
86
87
88
89
90
91
92
# 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) ⇒ Object

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

Parameters:

  • options (Hash)


123
124
125
126
# 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) ⇒ Object

return the hash with the keys converted to underscore

Parameters:

  • hash (Hash)


66
67
68
69
70
71
72
73
74
# 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