Class: Ayadn::Databases

Inherits:
Object
  • Object
show all
Defined in:
lib/ayadn/databases.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.aliasesObject

Returns the value of attribute aliases.



7
8
9
# File 'lib/ayadn/databases.rb', line 7

def aliases
  @aliases
end

.blacklistObject

Returns the value of attribute blacklist.



7
8
9
# File 'lib/ayadn/databases.rb', line 7

def blacklist
  @blacklist
end

.bookmarksObject

Returns the value of attribute bookmarks.



7
8
9
# File 'lib/ayadn/databases.rb', line 7

def bookmarks
  @bookmarks
end

.channelsObject

Returns the value of attribute channels.



7
8
9
# File 'lib/ayadn/databases.rb', line 7

def channels
  @channels
end

.indexObject

Returns the value of attribute index.



7
8
9
# File 'lib/ayadn/databases.rb', line 7

def index
  @index
end

.nicerankObject

Returns the value of attribute nicerank.



7
8
9
# File 'lib/ayadn/databases.rb', line 7

def nicerank
  @nicerank
end

.paginationObject

Returns the value of attribute pagination.



7
8
9
# File 'lib/ayadn/databases.rb', line 7

def pagination
  @pagination
end

.usersObject

Returns the value of attribute users.



7
8
9
# File 'lib/ayadn/databases.rb', line 7

def users
  @users
end

Class Method Details

.add_bookmark(bookmark) ⇒ Object



168
169
170
# File 'lib/ayadn/databases.rb', line 168

def self.add_bookmark bookmark
  @bookmarks[bookmark[:id]] = bookmark
end

.add_channel_object(channel) ⇒ Object



180
181
182
# File 'lib/ayadn/databases.rb', line 180

def self.add_channel_object channel
  @channels[channel['id']] = channel
end

.add_client_to_blacklist(target) ⇒ Object



73
74
75
# File 'lib/ayadn/databases.rb', line 73

def self.add_client_to_blacklist(target)
  target.each {|source| @blacklist[source.downcase] = :client}
end

.add_hashtag_to_blacklist(target) ⇒ Object



76
77
78
# File 'lib/ayadn/databases.rb', line 76

def self.add_hashtag_to_blacklist(target)
  target.each {|tag| @blacklist[tag.downcase] = :hashtag}
end

.add_mention_to_blacklist(target) ⇒ Object



70
71
72
# File 'lib/ayadn/databases.rb', line 70

def self.add_mention_to_blacklist(target)
  target.each {|username| @blacklist[username.downcase] = :mention}
end

.add_niceranks(niceranks) ⇒ Object



57
58
59
# File 'lib/ayadn/databases.rb', line 57

def self.add_niceranks niceranks
  niceranks.each {|id,infos| @nicerank[id] = infos}
end

.add_to_users_db(id, username, name) ⇒ Object



160
161
162
# File 'lib/ayadn/databases.rb', line 160

def self.add_to_users_db(id, username, name)
  @users[id] = {username => name}
end

.add_to_users_db_from_list(list) ⇒ Object



156
157
158
# File 'lib/ayadn/databases.rb', line 156

def self.add_to_users_db_from_list(list)
  list.each { |id, content_array| @users[id] = {content_array[0] => content_array[1]} }
end

.add_user_to_blacklist(target) ⇒ Object



67
68
69
# File 'lib/ayadn/databases.rb', line 67

def self.add_user_to_blacklist(target)
  target.each {|username| @blacklist["-#{username.downcase}"] = :user}
end

.all_dbsObject



27
28
29
# File 'lib/ayadn/databases.rb', line 27

def self.all_dbs
  [@users, @index, @pagination, @aliases, @blacklist, @bookmarks, @nicerank, @channels]
end

.clear_aliasesObject



111
112
113
# File 'lib/ayadn/databases.rb', line 111

def self.clear_aliases
  @aliases.clear
end

.clear_blacklistObject



115
116
117
# File 'lib/ayadn/databases.rb', line 115

def self.clear_blacklist
  @blacklist.clear
end

.clear_bookmarksObject



119
120
121
# File 'lib/ayadn/databases.rb', line 119

def self.clear_bookmarks
  @bookmarks.clear
end

.close_allObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ayadn/databases.rb', line 31

def self.close_all

  if @nicerank.size > 5000
    if Settings.options[:timeline][:show_debug] == true
      puts "\n-Purging NiceRank database-\n"
    end
    limit = Time.now - (3600 * 48)
    @nicerank.each {|k,v| @nicerank.delete(k) if v[:cached] < limit}
  end

  Debug.db all_dbs

  all_dbs.each do |db|
    if Settings.options[:timeline][:show_debug] == true
      puts "\n-Closing #{File.basename(db.file)}-"
    end
    db.flush
    db.compact
    db.close
  end
end

.convert_blacklistObject



87
88
89
90
91
92
# File 'lib/ayadn/databases.rb', line 87

def self.convert_blacklist
  dummy = {}
  @blacklist.each {|v,k| dummy[v.downcase] = k}
  @blacklist.clear
  dummy.each {|v,k| @blacklist[v] = k}
end

.create_alias(channel_id, channel_alias) ⇒ Object



103
104
105
# File 'lib/ayadn/databases.rb', line 103

def self.create_alias(channel_id, channel_alias)
  @aliases[channel_alias] = channel_id
end

.delete_alias(channel_alias) ⇒ Object



107
108
109
# File 'lib/ayadn/databases.rb', line 107

def self.delete_alias(channel_alias)
  @aliases.delete(channel_alias)
end

.delete_bookmark(post_id) ⇒ Object



172
173
174
# File 'lib/ayadn/databases.rb', line 172

def self.delete_bookmark post_id
  @bookmarks.delete post_id
end

.get_alias_from_id(channel_id) ⇒ Object



133
134
135
136
# File 'lib/ayadn/databases.rb', line 133

def self.get_alias_from_id(channel_id)
  @aliases.each {|al, id| return al if id == channel_id}
  nil
end

.get_channel_id(channel_alias) ⇒ Object



123
124
125
# File 'lib/ayadn/databases.rb', line 123

def self.get_channel_id(channel_alias)
  @aliases[channel_alias]
end

.get_index_lengthObject



143
144
145
# File 'lib/ayadn/databases.rb', line 143

def self.get_index_length
  @index.length
end

.get_niceranks(user_ids) ⇒ Object



61
62
63
64
65
# File 'lib/ayadn/databases.rb', line 61

def self.get_niceranks user_ids
  ids = {}
  user_ids.each {|id| ids[id] = @nicerank[id]}
  ids
end

.get_post_from_index(number) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/ayadn/databases.rb', line 147

def self.get_post_from_index(number)
  unless number > @index.length || number <= 0
    @index.each {|id, values| return values if values[:count] == number}
  else
    puts Status.must_be_in_index
    Errors.global_error({error: "Out of range", caller: caller, data: [number]})
  end
end

.has_new?(stream, title) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/ayadn/databases.rb', line 164

def self.has_new?(stream, title)
  stream['meta']['max_id'].to_i > @pagination[title].to_i
end

.import_aliases(aliases) ⇒ Object



127
128
129
130
131
# File 'lib/ayadn/databases.rb', line 127

def self.import_aliases(aliases)
  new_aliases = self.init aliases
  new_aliases.each {|al,id| @aliases[al] = id}
  new_aliases.close
end

.import_blacklist(blacklist) ⇒ Object



82
83
84
85
86
# File 'lib/ayadn/databases.rb', line 82

def self.import_blacklist(blacklist)
  new_list = self.init blacklist
  new_list.each {|name,type| @blacklist[name] = type}
  new_list.close
end

.init(path) ⇒ Object



53
54
55
# File 'lib/ayadn/databases.rb', line 53

def self.init(path)
    Daybreak::DB.new "#{path}"
end

.open_databasesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ayadn/databases.rb', line 10

def self.open_databases
  if Settings.options[:timeline][:show_debug] == true
    puts "\n-Opening databases-\n"
  end
  @users = self.init "#{Settings.config[:paths][:db]}/users.db"
  @index = self.init "#{Settings.config[:paths][:pagination]}/index.db"
  @pagination = self.init "#{Settings.config[:paths][:pagination]}/pagination.db"
  @aliases = self.init "#{Settings.config[:paths][:db]}/aliases.db"
  @blacklist = self.init "#{Settings.config[:paths][:db]}/blacklist.db"
  @bookmarks = self.init "#{Settings.config[:paths][:db]}/bookmarks.db"
  @nicerank = self.init "#{Settings.config[:paths][:db]}/nicerank.db"
  @channels = self.init "#{Settings.config[:paths][:db]}/channels.db"
  if Settings.options[:timeline][:show_debug] == true
    puts "\n-Done-\n"
  end
end

.remove_channel(channel_id) ⇒ Object



184
185
186
# File 'lib/ayadn/databases.rb', line 184

def self.remove_channel channel_id
  @channels.delete channel_id
end

.remove_from_blacklist(target) ⇒ Object



79
80
81
# File 'lib/ayadn/databases.rb', line 79

def self.remove_from_blacklist(target)
  target.each {|el| @blacklist.delete(el.downcase)}
end

.rename_bookmark(post_id, new_title) ⇒ Object



176
177
178
# File 'lib/ayadn/databases.rb', line 176

def self.rename_bookmark post_id, new_title
  @bookmarks[post_id][:title] = new_title
end

.save_indexed_posts(posts) ⇒ Object



138
139
140
141
# File 'lib/ayadn/databases.rb', line 138

def self.save_indexed_posts(posts)
  @index.clear
  posts.each {|id, hash| @index[id] = hash}
end

.save_max_id(stream, name = 'unknown') ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/ayadn/databases.rb', line 94

def self.save_max_id(stream, name = 'unknown')
  if stream['meta']['marker'].nil?
    key = name
  else
    key = stream['meta']['marker']['name']
  end
  @pagination[key] = stream['meta']['max_id']
end