Class: Rmega::Nodes::Node

Inherits:
Object
  • Object
show all
Includes:
Loggable, Traversable
Defined in:
lib/rmega/nodes/node.rb

Direct Known Subclasses

File, Folder, Inbox, Root, Trash

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Traversable

#children, #empty?, #files, #folders, #parent

Methods included from Loggable

included, #logger

Constructor Details

#initialize(session, data) ⇒ Node

Returns a new instance of Node.



16
17
18
19
# File 'lib/rmega/nodes/node.rb', line 16

def initialize(session, data)
  @session = session
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/rmega/nodes/node.rb', line 12

def data
  @data
end

#sessionObject (readonly)

Returns the value of attribute session.



12
13
14
# File 'lib/rmega/nodes/node.rb', line 12

def session
  @session
end

Instance Method Details

#attributesObject



68
69
70
71
72
73
# File 'lib/rmega/nodes/node.rb', line 68

def attributes
  @attributes ||= begin
    return nil unless can_decrypt_attributes?
    Crypto.decrypt_attributes decrypted_file_key, (data['a'] || data['at'])
  end
end

#can_decrypt_attributes?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/rmega/nodes/node.rb', line 64

def can_decrypt_attributes?
  !data['u'] or data['u'] == owner_key
end

#decrypted_file_keyObject



56
57
58
59
60
61
62
# File 'lib/rmega/nodes/node.rb', line 56

def decrypted_file_key
  if data['k']
    Crypto.decrypt_key session.master_key, Utils.base64_to_a32(file_key)
  else
    Utils.base64_to_a32 public_url.split('!').last
  end
end

#file_keyObject



52
53
54
# File 'lib/rmega/nodes/node.rb', line 52

def file_key
  data['k'].split(':').last
end

#handleObject



36
37
38
# File 'lib/rmega/nodes/node.rb', line 36

def handle
  data['h']
end

#nameObject



48
49
50
# File 'lib/rmega/nodes/node.rb', line 48

def name
  return attributes['n'] if attributes
end

#owner_keyObject



44
45
46
# File 'lib/rmega/nodes/node.rb', line 44

def owner_key
  data['k'].split(':').first
end

#parent_handleObject



40
41
42
# File 'lib/rmega/nodes/node.rb', line 40

def parent_handle
  data['p']
end

#public_handleObject



32
33
34
# File 'lib/rmega/nodes/node.rb', line 32

def public_handle
  @public_handle ||= request(a: 'l', n: handle)
end

#public_urlObject



21
22
23
24
25
26
# File 'lib/rmega/nodes/node.rb', line 21

def public_url
  @public_url ||= begin
    b64_dec_key = Utils.a32_to_base64 decrypted_file_key[0..7]
    "https://mega.co.nz/#!#{public_handle}!#{b64_dec_key}"
  end
end

#public_url=(url) ⇒ Object



28
29
30
# File 'lib/rmega/nodes/node.rb', line 28

def public_url=(url)
  @public_url = url
end

#typeObject



75
76
77
# File 'lib/rmega/nodes/node.rb', line 75

def type
  Factory.type(data['t'])
end