class GithubDash::DataDepository

Store and manage all repositories the user choses to follow

and the tokens needed to access them

Public Class Methods

add_repo(repo_name, token=nil) click to toggle source

Save a repo name in the following list

# File lib/github_dash/data_depository.rb, line 11
def self.add_repo(repo_name, token=nil)
  # Check the repo is not already followed
  if get_db[:repos].where(:name => repo_name.downcase).all.count > 0
    raise ArgumentError, "Tried to follow a repository that was already followed!"
  end

  # Add repository to database
  if token.nil?
    token_id = nil
  else
    token_id = get_db[:tokens].where(:token => token).first[:id]
  end
  get_db[:repos].insert(:name => repo_name.downcase, :token_id => token_id)
end
delete_token(token) click to toggle source

Delete a token

# File lib/github_dash/data_depository.rb, line 51
def self.delete_token(token)
  # Delete it
  get_db[:tokens].where(:token => token).delete
end
get_all_tokens() click to toggle source

Get all the tokens saved

# File lib/github_dash/data_depository.rb, line 67
def self.get_all_tokens
  get_db[:tokens].all.map do |t|
    { token: t[:token], name: t[:name] }
  end
end
get_following() click to toggle source

Get an array of the names of followed repositories

# File lib/github_dash/data_depository.rb, line 37
def self.get_following
  # Create an arrau of just the repo's names
  get_db[:repos].all.map do |r|
    r[:name]
  end
end
get_token(place_from_last = 0) click to toggle source

Get the github API token

# File lib/github_dash/data_depository.rb, line 57
def self.get_token(place_from_last = 0)
  tokens = get_db[:tokens].order(:id)
  # Will return nil if empty
  return nil if tokens.empty? || place_from_last >= tokens.all.count

  # Returns the token in the place from last specifiedl
  tokens.all[- (place_from_last + 1)][:token]
end
get_token_for_repo(repo_name) click to toggle source

Get the token for a certain repository

# File lib/github_dash/data_depository.rb, line 74
def self.get_token_for_repo(repo_name)
  repo = get_db[:repos].where(:name => repo_name.downcase).first
  return nil if repo.nil? || repo[:token_id].nil?
  token_id = repo[:token_id]

  get_db[:tokens].where(:id => token_id).first[:token]
end
remove_repo(repo_name) click to toggle source

Remove a repository from a list of followed repositories

# File lib/github_dash/data_depository.rb, line 27
def self.remove_repo(repo_name)
  # Remove the repository
  if get_db[:repos].where(:name => repo_name.downcase).delete == 0
    # `delete` will return the number of entries deleted
    #   So if none were deleted, raise an error
    raise ArgumentError, "Tried removing a repository that was not followed!"
  end
end
save_token(token, token_name) click to toggle source

Save a token to be used for logging in

# File lib/github_dash/data_depository.rb, line 45
def self.save_token(token, token_name)
  # Add this token
  get_db[:tokens].insert(:token => token, :name => token_name)
end

Private Class Methods

get_db() click to toggle source

Get the database from the .db file and creates

all tables unless they already exist
# File lib/github_dash/data_depository.rb, line 84
def self.get_db

  # Does not load the database twice
  unless class_variable_defined?(:@@db)

    @@db = Sequel.sqlite("#{ENV['HOME']}/.github_dash/data.db")

    # Create the tables
    #   Note: create_table? works the same as create_table,
    #   except that it will not override an existing table
    #   with the same name.
    @@db.create_table? :repos do
      primary_key :id
      String :name, :unique => true
      # The ID of the token used to fetch this repo
      foriegn_key :token_id
    end

    @@db.create_table? :tokens do
      primary_key :id
      String :token, :unique => true
      String :name, :unique => true
    end
  end

  @@db
end