Class: Votifier::Client
- Inherits:
-
Object
- Object
- Votifier::Client
- Defined in:
- lib/votifier/client.rb
Instance Attribute Summary collapse
-
#ip_address ⇒ Object
Returns the value of attribute ip_address.
-
#public_key ⇒ Object
readonly
Returns the value of attribute public_key.
-
#service_name ⇒ Object
readonly
Returns the value of attribute service_name.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #detect_ip_address ⇒ Object
- #encrypt(votifier_string) ⇒ Object
- #init_public_key(public_key_file) ⇒ Object
-
#initialize(public_key_file, service_name, opts = {}) ⇒ Client
constructor
A new instance of Client.
- #ips ⇒ Object
- #my_best_ip_address ⇒ Object
- #my_first_private_ipv4 ⇒ Object
- #my_first_public_ipv4 ⇒ Object
- #send ⇒ Object
- #send_to_server(encrypted) ⇒ Object
- #to_a ⇒ Object
- #validate! ⇒ Object
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) = opts.fetch(:timestamp, Time.now.to_i) end |
Instance Attribute Details
#ip_address ⇒ Object
Returns the value of attribute ip_address.
7 8 9 |
# File 'lib/votifier/client.rb', line 7 def ip_address @ip_address end |
#public_key ⇒ Object (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_name ⇒ Object (readonly)
Returns the value of attribute service_name.
6 7 8 |
# File 'lib/votifier/client.rb', line 6 def service_name @service_name end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
7 8 9 |
# File 'lib/votifier/client.rb', line 7 def end |
#username ⇒ Object
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_address ⇒ Object
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 |
#ips ⇒ Object
39 40 41 |
# File 'lib/votifier/client.rb', line 39 def ips @ips ||= Socket.ip_address_list end |
#my_best_ip_address ⇒ Object
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_ipv4 ⇒ Object
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_ipv4 ⇒ Object
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 |
#send ⇒ Object
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_a ⇒ Object
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, ] end |
#validate! ⇒ Object
50 51 52 |
# File 'lib/votifier/client.rb', line 50 def validate! raise 'Usename must be present' unless !username.nil? end |