Module: Rmega::Nodes::Factory

Extended by:
Factory
Included in:
Factory
Defined in:
lib/rmega/nodes/factory.rb

Constant Summary collapse

URL_REGEXP =
/(https{0,1}:\/\/[w\.]*mega\.[a-z\.]+\/\#[A-Z0-9\_\-\!\=]+)/i
URL_REGEXP_NEW =
/(https{0,1}:\/\/[w\.]*mega\.[a-z\.]+\/[a-z]{4,6}\/[a-z0-9\_\-\=]+\#[a-z0-9\_\-\=]+)/i

Instance Method Summary collapse

Instance Method Details

#build(session, data) ⇒ Object



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

def build(session, data)
  type = Node::TYPES[data['t']].to_s
  return Nodes.const_get(type.capitalize).new(session, data)
end

#build_from_url(url, session = Session.new) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rmega/nodes/factory.rb', line 45

def build_from_url(url, session = Session.new)
  public_handle, key = *public_handle_and_key_from_url(url)

  raise "Invalid url or missing file key" unless key

  node = if url.include?("/folder/") or url.include?("/#F!")
    nodes_data = session.request({a: 'f', c: 1, r: 1}, {n: public_handle})
    session.master_key = Utils.base64urldecode(key)
    session.storage.nodes = nodes_data['f'].map do |data|
      data["__n"] = public_handle
      Nodes::Factory.build(session, data)
    end
    session.storage.nodes[0]
  else
    data = session.request(a: 'g', g: 1, p: public_handle)
    Nodes::File.new(session, data)
  end

  node.instance_variable_set('@public_handle', public_handle)
  node.instance_variable_set('@public_url', url)

  return node
end

#public_handle_and_key_from_url(string) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rmega/nodes/factory.rb', line 22

def public_handle_and_key_from_url(string)
  if string.to_s =~ URL_REGEXP
    public_handle, key = string.strip.split('!')[1, 2]
    return [] if key and (Utils.base64urldecode(key) rescue nil).nil?
    return [public_handle, key]
  elsif string.to_s =~ URL_REGEXP_NEW
    public_handle, key = *string.scan(/\/([^\/]+)\#(.+)$/).flatten
    return [] if key and (Utils.base64urldecode(key) rescue nil).nil?
    return [public_handle, key]
  else
    return []
  end
end

#url?(string) ⇒ Boolean

Returns:

  • (Boolean)


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

def url?(string)
  public_handle_and_key_from_url(string).any?
end