Class: Servicon::Icon

Inherits:
Object
  • Object
show all
Defined in:
lib/servicon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fingerprint, color = options[:color]) ⇒ Icon

Returns a new instance of Icon.



12
13
14
15
16
# File 'lib/servicon.rb', line 12

def initialize(fingerprint, color = options[:color])
  color=="true" ? @color = true : @color = false
  @fingerprint = fingerprint
  @uuid = self.uuid
end

Instance Attribute Details

#fingerprintObject

Returns the value of attribute fingerprint.



10
11
12
# File 'lib/servicon.rb', line 10

def fingerprint
  @fingerprint
end

Instance Method Details

#displayObject



47
48
49
# File 'lib/servicon.rb', line 47

def display
  draw_matrix(matrix)
end

#draw_char(character) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/servicon.rb', line 71

def draw_char(character)
  rgb = [self.rgb[:red], self.rgb[:green], self.rgb[:blue]]
  rgb.collect! { |n| n / 2 } if character == 1

  background = :default
  foreground = :default

  if @color
    background = rgb
  else
    background = :inverse if character == 1
  end

  Paint[" ", foreground, background]

end

#draw_matrix(matrix) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/servicon.rb', line 51

def draw_matrix(matrix)
  my_row = Array.new
  
  matrix.each do |row|
     my_row << draw_row(row)
  end
  
  return my_row.join
end

#draw_row(matrix_row) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/servicon.rb', line 61

def draw_row(matrix_row)
  line = Array.new

  matrix_row.each do |character|
    line << draw_char(character)
  end
    
  return line.join + "\n"
end

#matrixObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/servicon.rb', line 23

def matrix
  line = Array.new
  identicon = Array.new

  0.upto(63) do |i|
    line << @uuid[i]
    if line.count == 8
      identicon << line + line.reverse
      line = Array.new
    end
  end

  return identicon
end

#rgbObject



38
39
40
41
42
43
44
45
# File 'lib/servicon.rb', line 38

def rgb
  color = Hash.new
  color[:red] = ((@uuid >> 4) & 0xff)
  color[:blue] = ((@uuid >> 8) & 0xff)
  color[:green] = ((@uuid >> 16) & 0xff)

  return color
end

#uuidObject



18
19
20
21
# File 'lib/servicon.rb', line 18

def uuid
  uuid = Digest::SHA1.hexdigest(@fingerprint).unpack('H*')[0].to_i(16)
  return uuid
end