Class: DaFace::Datasift::Links

Inherits:
Array
  • Object
show all
Defined in:
lib/da_face/datasift/links.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Links

Returns a new instance of Links.



4
5
6
7
8
# File 'lib/da_face/datasift/links.rb', line 4

def initialize data={}
  self.class.get_elements(data).each do |e|
    self << DaFace::Datasift::Link.new(e)
  end
end

Class Method Details

.get_elements(data = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/da_face/datasift/links.rb', line 10

def self.get_elements data={}
  hashed_links = []
  unless data.empty?
    data.values.first.size.times do |index|
      hashed_links << get_values(index - 1, data.keys, data)
    end
  end
  return hashed_links
end

.get_values(index, keys, hash) ⇒ Object

Recursively search the hash for elements of index ‘index’ from the arrays inside the hashes.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/da_face/datasift/links.rb', line 22

def self.get_values index, keys, hash
  new_hash = {}
  keys.each do |key|
    if hash[key].kind_of? Hash
      new_hash[key] = get_values(index, hash[key].keys, hash[key])
    else
      new_hash[key] = hash[key][index]
    end
  end
  
  return new_hash
end