Class: Pushes::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/pushes/config.rb

Constant Summary collapse

PUSHES_FOLDER =
File.join(ENV['HOME'], '.pushes')
STORAGE_FILE =
File.join(PUSHES_FOLDER, 'storage')
CONFIG_FILE =
File.join(PUSHES_FOLDER, 'login')

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



10
11
12
13
14
15
16
17
18
# File 'lib/pushes/config.rb', line 10

def initialize
  configs = File.read(CONFIG_FILE) rescue create_config_file
  configs.split("\n").each do |config|
    key, value = config.split('=')
    define_singleton_method key.downcase do
      value
    end
  end
end

Instance Method Details

#clear_storageObject



59
60
61
# File 'lib/pushes/config.rb', line 59

def clear_storage
  store([])
end

#create_config_fileObject

Raises:

  • (StandardError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pushes/config.rb', line 20

def create_config_file
  github_config = {}
  github_config[:login] = ask('What is your GitHub username? ')
  github_config[:token] = get_github_token(github_config[:login])

  raise StandardError, 'You most likely typed an incorrect username or password, please try again.' unless github_config[:token]
  mkdir_pushes

  content = github_config.each_pair.map { |k, v| "#{k.upcase}=#{v}" }.join("\n") + "\n"
  File.open(CONFIG_FILE, 'w') do |file|
    file.write(content)
  end

  content
end

#initiateObject



45
46
47
# File 'lib/pushes/config.rb', line 45

def initiate
  File.open(STORAGE_FILE, 'w') {}
end

#initiated?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/pushes/config.rb', line 41

def initiated?
  File.exist? STORAGE_FILE
end

#mkdir_pushesObject



36
37
38
39
# File 'lib/pushes/config.rb', line 36

def mkdir_pushes
  return if File.directory?(PUSHES_FOLDER)
  FileUtils.mkdir(PUSHES_FOLDER)
end

#storageObject



55
56
57
# File 'lib/pushes/config.rb', line 55

def storage
  File.read(STORAGE_FILE).split("\n")
end

#store(push_events) ⇒ Object



49
50
51
52
53
# File 'lib/pushes/config.rb', line 49

def store(push_events)
  File.open(STORAGE_FILE, 'w') do |file|
    file.write push_events.join("\n") + "\n"
  end
end