Class: Votifier::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_key_file, service_name, opts = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/votifier/client.rb', line 9

def initialize(public_key_file, service_name, opts = {})
  init_public_key(public_key_file)
  @service_name = service_name
  @username = opts.fetch(:username, nil)
  @ip_address = opts.fetch(:ip_address, detect_ip_address)
  @timestamp = opts.fetch(:timestamp, Time.now.to_i)
end

Instance Attribute Details

#ip_addressObject

Returns the value of attribute ip_address.



7
8
9
# File 'lib/votifier/client.rb', line 7

def ip_address
  @ip_address
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



6
7
8
# File 'lib/votifier/client.rb', line 6

def public_key
  @public_key
end

#service_nameObject (readonly)

Returns the value of attribute service_name.



6
7
8
# File 'lib/votifier/client.rb', line 6

def service_name
  @service_name
end

#timestampObject

Returns the value of attribute timestamp.



7
8
9
# File 'lib/votifier/client.rb', line 7

def timestamp
  @timestamp
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/votifier/client.rb', line 7

def username
  @username
end

Instance Method Details

#detect_ip_addressObject



21
22
23
24
25
# File 'lib/votifier/client.rb', line 21

def detect_ip_address
  remote_ip = ENV['HTTP_X_FORWARDED_FOR'] || ENV['REMOTE_ADDR'] || my_best_ip_address
  remote_ip = remote_ip.ip_address if remote_ip.respond_to?(:ip_address)
  remote_ip = remote_ip.scan(/[\d.]+/).first
end

#encrypt(votifier_string) ⇒ Object



54
55
56
57
58
# File 'lib/votifier/client.rb', line 54

def encrypt(votifier_string)
  encrypted = public_key.public_encrypt(votifier_string)
  raise "Error encrypting, invalid size: #{encrypted.bytesize}" unless encrypted.bytesize == 256
  encrypted
end

#init_public_key(public_key_file) ⇒ Object



17
18
19
# File 'lib/votifier/client.rb', line 17

def init_public_key(public_key_file)
  @public_key = Votifer::Key.from_public_key_file(public_key_file)
end

#ipsObject



39
40
41
# File 'lib/votifier/client.rb', line 39

def ips
  @ips ||= Socket.ip_address_list
end

#my_best_ip_addressObject



27
28
29
# File 'lib/votifier/client.rb', line 27

def my_best_ip_address
  my_first_public_ipv4 || my_first_private_ipv4
end

#my_first_private_ipv4Object



35
36
37
# File 'lib/votifier/client.rb', line 35

def my_first_private_ipv4
  ips.detect{|intf| intf.ipv4_private?}
end

#my_first_public_ipv4Object



31
32
33
# File 'lib/votifier/client.rb', line 31

def my_first_public_ipv4
  ips.detect{|intf| intf.ipv4? and !intf.ipv4_loopback? and !intf.ipv4_multicast? and !intf.ipv4_private?}
end

#sendObject



43
44
45
46
47
48
# File 'lib/votifier/client.rb', line 43

def send
  validate!
  votifier_string = to_a.join("\n")
  encrypted = encrypt(votifier_string)
  send_to_server(encrypted)
end

#send_to_server(encrypted) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/votifier/client.rb', line 60

def send_to_server(encrypted)
  hostname = 'localhost'
  port = 8193

  s = TCPSocket.open(hostname, port)

  s.print encrypted
  s.close
end

#to_aObject



70
71
72
73
74
75
76
77
78
# File 'lib/votifier/client.rb', line 70

def to_a
  [
   "VOTE",
   service_name,
   username,
   ip_address,
   timestamp
  ]
end

#validate!Object



50
51
52
# File 'lib/votifier/client.rb', line 50

def validate!
  raise 'Usename must be present' unless !username.nil?
end