Module: Helpers

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



74
75
76
# File 'lib/algolia/helpers.rb', line 74

def self.included(base)
  base.extend(Helpers)
end

Instance Method Details

#check_array(object) ⇒ Object

Check the passed object to determine if it’s an array



90
91
92
# File 'lib/algolia/helpers.rb', line 90

def check_array(object)
  raise Algolia::AlgoliaError, 'argument must be an array of objects' unless object.is_a?(Array)
end

#check_object(object, in_array = false) ⇒ Object

Check the passed object



99
100
101
102
103
104
105
106
# File 'lib/algolia/helpers.rb', line 99

def check_object(object, in_array = false)
  case object
  when Array
    raise Algolia::AlgoliaError, in_array ? 'argument must be an array of objects' : 'argument must not be an array'
  when String, Integer, Float, TrueClass, FalseClass, NilClass
    raise Algolia::AlgoliaError, "argument must be an #{'array of' if in_array} object, got: #{object.inspect}"
  end
end

#chunk(action, objects, with_object_id = false) ⇒ Object

Build a batch request



126
127
128
129
130
131
132
133
# File 'lib/algolia/helpers.rb', line 126

def chunk(action, objects, with_object_id = false)
  objects.map do |object|
    check_object(object, true)
    request            = { action: action, body: object }
    request[:objectID] = get_object_id(object).to_s if with_object_id
    request
  end
end

#deserialize_settings(data, symbolize_keys) ⇒ Object

Support to convert old settings to their new names



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/algolia/helpers.rb', line 55

def deserialize_settings(data, symbolize_keys)
  settings = data
  keys     = {
    attributesToIndex: 'searchableAttributes',
    numericAttributesToIndex: 'numericAttributesForFiltering',
    slaves: 'replicas'
  }

  keys.each do |deprecated_key, current_key|
    deprecated_key = symbolize_keys ? deprecated_key : deprecated_key.to_s
    if settings.has_key?(deprecated_key)
      key           = symbolize_keys ? current_key.to_sym : current_key.to_s
      settings[key] = settings.delete(deprecated_key)
    end
  end

  settings
end

#get_object_id(object, object_id = nil) ⇒ Object

Check if passed object has a objectID



113
114
115
116
117
118
# File 'lib/algolia/helpers.rb', line 113

def get_object_id(object, object_id = nil)
  check_object(object)
  object_id ||= object[:objectID] || object['objectID']
  raise Algolia::AlgoliaError, "Missing 'objectID'" if object_id.nil?
  object_id
end

#get_option(hash, key) ⇒ Object

Retrieve the given value associated with a key, in string or symbol format



38
39
40
# File 'lib/algolia/helpers.rb', line 38

def get_option(hash, key)
  hash[key.to_sym] || hash[key] || nil
end

#handle_params(params) ⇒ Object

Convert params to a full query string



18
19
20
# File 'lib/algolia/helpers.rb', line 18

def handle_params(params)
  params.nil? || params.empty? ? '' : "?#{to_query_string(params)}"
end

#hash_includes_subset?(hash, subset) ⇒ Boolean



78
79
80
81
82
83
84
# File 'lib/algolia/helpers.rb', line 78

def hash_includes_subset?(hash, subset)
  res = true
  subset.each do |k, v|
    res &&= hash[k] == v
  end
  res
end

#json_to_hash(json, symbolize_keys) ⇒ Object

Convert a json object to an hash



32
33
34
# File 'lib/algolia/helpers.rb', line 32

def json_to_hash(json, symbolize_keys)
  MultiJson.load(json, symbolize_keys: symbolize_keys)
end

#path_encode(path, *args) ⇒ Object

Build a path with the given arguments



44
45
46
47
48
49
50
51
# File 'lib/algolia/helpers.rb', line 44

def path_encode(path, *args)
  arguments = []
  args.each do |arg|
    arguments.push(CGI.escape(CGI.unescape(arg.to_s)))
  end

  format(path, *arguments)
end

#symbolize_hash(hash) ⇒ Object

Converts each key of a hash to symbols



12
13
14
# File 'lib/algolia/helpers.rb', line 12

def symbolize_hash(hash)
  hash.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
end

#to_json(body) ⇒ Object

Convert an Hash to json



6
7
8
# File 'lib/algolia/helpers.rb', line 6

def to_json(body)
  body.is_a?(String) ? body : MultiJson.dump(body)
end

#to_query_string(params) ⇒ Object

Create a query string from params



24
25
26
27
28
# File 'lib/algolia/helpers.rb', line 24

def to_query_string(params)
  params.map do |key, value|
    "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
  end.join('&')
end