Class: MeshChat::Models::Entry

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/meshchat/models/entry.rb

Constant Summary collapse

IPV4_WITH_PORT =
/((?:(?:^|\.)(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){4})(:\d*)?/
DOMAIN_WITH_PORT =
/(https?:\/\/)?([\da-z\.-]+)\.?([a-z\.]{2,6})([\/\w \.-]*)*[^\/](:\d*)?/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.as_jsonObject



35
36
37
38
39
40
41
42
# File 'lib/meshchat/models/entry.rb', line 35

def as_json
  # must also include ourselves
  # so that we can pass our own public key
  # to those who don't have it
  others = all.map(&:as_json)
  me = Settings.identity_as_json
  others << me
end

.as_sha512Object



30
31
32
33
# File 'lib/meshchat/models/entry.rb', line 30

def as_sha512
  digest = Digest::SHA512.new
  digest.hexdigest sha_preimage
end

.diff(theirs) ⇒ Array<-,+>

Returns nodes only we have, and nodes only they have.

Parameters:

  • theirs (Array)

    array of hashes representing node entries

Returns:

  • (Array<-,+>)

    nodes only we have, and nodes only they have



59
60
61
62
63
64
65
# File 'lib/meshchat/models/entry.rb', line 59

def diff(theirs)
  ours = as_json
  we_only_have = ours - theirs
  they_only_have = theirs - ours

  [we_only_have, they_only_have]
end

.from_json(json) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/meshchat/models/entry.rb', line 44

def from_json(json)
  new(
    alias_name: json['alias'],
    location: json['location'],
    uid: json['uid'],
    public_key: json['publicKey']
  )
end

.import_from_file(filename) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/meshchat/models/entry.rb', line 67

def import_from_file(filename)
  begin
    f = File.read(filename)
    hash = JSON.parse(f)
    n = from_json(hash)
    n.save
    n
  rescue => e
    Display.alert e.message
  end
end

.public_key_from_uid(uid) ⇒ Object



53
54
55
# File 'lib/meshchat/models/entry.rb', line 53

def public_key_from_uid(uid)
  find_by_uid(uid).try(:public_key)
end

.sha_preimageObject



26
27
28
# File 'lib/meshchat/models/entry.rb', line 26

def sha_preimage
  all.map(&:public_key).sort.join(',')
end

Instance Method Details

#==(other) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/meshchat/models/entry.rb', line 80

def ==(other)
  result = false

  if other.is_a?(Hash)
    result = as_json.values_at(*other.keys) == other.values
  end

  result || super
end

#as_infoObject



99
100
101
# File 'lib/meshchat/models/entry.rb', line 99

def as_info
  "#{alias_name}@#{location}"
end

#as_jsonObject



90
91
92
93
94
95
96
97
# File 'lib/meshchat/models/entry.rb', line 90

def as_json
  {
    'alias' => alias_name,
    'location' => location,
    'uid' => uid,
    'publicKey' => public_key
  }
end