class Lita::Handlers::LinkLibrary

Constants

DESCRIPTION
TITLE

Public Instance Methods

add_read_command(response) click to toggle source
# File lib/lita/handlers/link-library.rb, line 77
def add_read_command(response)
  if (response.matches.first[5]!='|' or response.matches.first[7]!='|')
    response.reply('Please use a valid format using the <|>pipe<|> character. <<add read LINK|TITLE|DESCRIPTION>>')
  else
    link = response.matches.first[0]
    title = response.matches.first[6]
    description = response.matches.first[8]
    response.reply save_new_entry(link, title, description)
  end
end
delete_entry(title) click to toggle source
# File lib/lita/handlers/link-library.rb, line 119
def delete_entry (title) # allows a librarian to delete an entry from the library
  file = File.read 'library.json'
  hash_array = JSON.parse(file)
  entries_counter = 0
  hash_array.each do |entry|
    if entry.has_value?(title)
      hash_array.reject! {|entry| entry.has_value?(title)}
      file_writer 'library.json', hash_array
      return "The entry with the title #{title} was deleted"
    else
      entries_counter +=1
    end
  end
  if entries_counter==hash_array.length
    response = "No entry with the title #{title} can be found in the library"
  end
end
file_writer(filename, entries) click to toggle source
# File lib/lita/handlers/link-library.rb, line 93
def file_writer (filename, entries)
  open(filename, 'w') do |file|
    file.truncate(0)
    file.write entries.to_json
  end
end
list_library() click to toggle source
# File lib/lita/handlers/link-library.rb, line 175
def list_library # returns the entire library
  file = File.read 'library.json'
  hash_array = JSON.parse(file)
  return_array=[]
  hash_array.each do |entry|
    library_entry = LibraryEntry.new entry['link'], entry['title'], entry['description'], entry['number_of_downloads']
    return_array << "#{library_entry.title} | #{library_entry.link} | #{library_entry.number_of_downloads} reads | #{library_entry.description}\n"
  end
  return return_array
end
list_library_command(response) click to toggle source
# File lib/lita/handlers/link-library.rb, line 68
def list_library_command(response)
  response.reply list_library
end
random_read_command(response) click to toggle source
# File lib/lita/handlers/link-library.rb, line 60
def random_read_command(response)
  response.reply random_read_entry
end
random_read_entry() click to toggle source
# File lib/lita/handlers/link-library.rb, line 155
def random_read_entry # returns a random entry from the library
  file = File.read 'library.json'
  hash_array = JSON.parse(file)
  entry = hash_array.sample
  entry['number_of_downloads']+=1
  file_writer 'library.json', hash_array
  return "Random read: #{entry['title']} | #{entry['link']} | #{entry['number_of_downloads']} reads | #{entry['description']}"
end
read_best_entries() click to toggle source
# File lib/lita/handlers/link-library.rb, line 164
def read_best_entries # returns the top 3 downloaded entries from the library
  file = File.read 'library.json'
  hash_array = JSON.parse(file)
  return_array=[]
  hash_array.sort! {|a1, a2| a2['number_of_downloads'] <=> a1['number_of_downloads']}
  for i in 0..2
    return_array << "#{hash_array[i]['title']} | #{hash_array[i]['link']} | #{hash_array[i]['number_of_downloads']} reads | #{hash_array[i]['description']}\n"
  end
  return return_array
end
read_best_entries_command(response) click to toggle source
# File lib/lita/handlers/link-library.rb, line 64
def read_best_entries_command(response)
  response.reply read_best_entries
end
read_entry(entry_title) click to toggle source
# File lib/lita/handlers/link-library.rb, line 137
def read_entry (entry_title) # returns an entry with the specified title from the library
  file = File.read 'library.json'
  hash_array = JSON.parse(file)
  entries_counter = 0
  hash_array.each do |entry|
    if entry.has_value?(entry_title)
      entry['number_of_downloads']+=1
      file_writer 'library.json', hash_array
      return "#{entry_title} | #{entry['link']} | #{entry['number_of_downloads']} reads | #{entry['description']}"
    else
      entries_counter += 1
    end
  end
  if entries_counter==hash_array.length
    return "No entry entitled #{entry_title} can be found in the library DB!"
  end
end
remove_read_command(response) click to toggle source
# File lib/lita/handlers/link-library.rb, line 88
def remove_read_command(response)
  title = response.matches.first[0]
  response.reply delete_entry(title)
end
save_new_entry(link, title, description) click to toggle source
# File lib/lita/handlers/link-library.rb, line 100
def save_new_entry (link, title, description) # allows a librarian to save a new entry in the library
  file = File.read 'library.json'
  hash_array = JSON.parse(file)
  entry_exists = false
  hash_array.each do |entry|
    if entry.has_value?(title)
      entry_exists = true
    end
  end
  if entry_exists
    return "That title already exists in the DB. Please choose another one"
  else
    new_entry = {"link" => link, "title" => title, "description" => description, "number_of_downloads" => 0}
    hash_array.push new_entry
    file_writer 'library.json', hash_array
    return "The entry #{title} was added in the library!"
  end
end
want_to_read_command(response) click to toggle source
# File lib/lita/handlers/link-library.rb, line 72
def want_to_read_command(response)
  title = response.matches.first[0]
  response.reply read_entry(title)
end