Class: Vines::Storage::Local

Inherits:
Vines::Storage show all
Defined in:
lib/vines/storage/local.rb

Overview

A storage implementation that persists data to YAML files on the local file system.

Instance Method Summary collapse

Methods inherited from Vines::Storage

#authenticate, defer, fiber, #find_avatar_by_jid, from_name, register

Methods included from Log

#log, set_log_file

Constructor Details

#initialize(&block) ⇒ Local

Returns a new instance of Local.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vines/storage/local.rb', line 11

def initialize(&block)
  @dir = nil
  instance_eval(&block)
  unless @dir && File.directory?(@dir) && File.writable?(@dir)
    raise 'Must provide a writable storage directory'
  end

  %w[user vcard fragment].each do |sub|
    sub = File.expand_path(sub, @dir)
    Dir.mkdir(sub, 0700) unless File.exists?(sub)
  end
end

Instance Method Details

#destroy_message(id) ⇒ Object



88
89
90
# File 'lib/vines/storage/local.rb', line 88

def destroy_message(id)
  # do nothing
end

#dir(dir = nil) ⇒ Object



24
25
26
# File 'lib/vines/storage/local.rb', line 24

def dir(dir=nil)
  dir ? @dir = File.expand_path(dir) : @dir
end

#find_fragment(jid, node) ⇒ Object



66
67
68
69
70
71
# File 'lib/vines/storage/local.rb', line 66

def find_fragment(jid, node)
  jid = JID.new(jid).bare.to_s
  return if jid.empty?
  file = 'fragment/%s' % fragment_id(jid, node)
  Nokogiri::XML(read(file)).root rescue nil
end

#find_messages(jid) ⇒ Object



80
81
82
# File 'lib/vines/storage/local.rb', line 80

def find_messages(jid)
  {}
end

#find_user(jid) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vines/storage/local.rb', line 28

def find_user(jid)
  jid = JID.new(jid).bare.to_s
  file = "user/#{jid}" unless jid.empty?
  record = YAML.load(read(file)) rescue nil
  return User.new(jid: jid).tap do |user|
    user.name, user.password = record.values_at('name', 'password')
    (record['roster'] || {}).each_pair do |jid, props|
      user.roster << Contact.new(
        jid: jid,
        name: props['name'],
        subscription: props['subscription'],
        ask: props['ask'],
        groups: props['groups'] || [])
    end
  end if record
end

#find_vcard(jid) ⇒ Object



53
54
55
56
57
58
# File 'lib/vines/storage/local.rb', line 53

def find_vcard(jid)
  jid = JID.new(jid).bare.to_s
  return if jid.empty?
  file = "vcard/#{jid}"
  Nokogiri::XML(read(file)).root rescue nil
end

#save_fragment(jid, node) ⇒ Object



73
74
75
76
77
78
# File 'lib/vines/storage/local.rb', line 73

def save_fragment(jid, node)
  jid = JID.new(jid).bare.to_s
  return if jid.empty?
  file = 'fragment/%s' % fragment_id(jid, node)
  save(file, node.to_xml)
end

#save_message(from, to, message) ⇒ Object



84
85
86
# File 'lib/vines/storage/local.rb', line 84

def save_message(from, to, message)
  # do nothing
end

#save_user(user) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/vines/storage/local.rb', line 45

def save_user(user)
  record = {'name' => user.name, 'password' => user.password, 'roster' => {}}
  user.roster.each do |contact|
    record['roster'][contact.jid.bare.to_s] = contact.to_h
  end
  save("user/#{user.jid.bare}", YAML.dump(record))
end

#save_vcard(jid, card) ⇒ Object



60
61
62
63
64
# File 'lib/vines/storage/local.rb', line 60

def save_vcard(jid, card)
  jid = JID.new(jid).bare.to_s
  return if jid.empty?
  save("vcard/#{jid}", card.to_xml)
end