Class: GithubDash::DataDepository

Inherits:
Object
  • Object
show all
Defined in:
lib/github_dash/data_depository.rb

Overview

Store and manage all repositories the user choses to follow

and the tokens needed to access them

Class Method Summary collapse

Class Method Details

.add_repo(repo_name, token = nil) ⇒ Object

Save a repo name in the following list



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Object

Delete a token



51
52
53
54
# 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_tokensObject

Get all the tokens saved



67
68
69
70
71
# 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_followingObject

Get an array of the names of followed repositories



37
38
39
40
41
42
# 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) ⇒ Object

Get the github API token



57
58
59
60
61
62
63
64
# 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) ⇒ Object

Get the token for a certain repository



74
75
76
77
78
79
80
# 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) ⇒ Object

Remove a repository from a list of followed repositories



27
28
29
30
31
32
33
34
# 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) ⇒ Object

Save a token to be used for logging in



45
46
47
48
# 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