Module: RamTool

Defined in:
lib/ram_tool.rb,
lib/ram_tool/version.rb

Constant Summary collapse

PIN_FILE =
File.expand_path("~/.ram_tool_pin")
SESSION_FILE =
File.expand_path("~/.ram_tool_session")
URL =
URI("https://jjjm03299-wq.github.io/supreme-octo-potato/api/get-ram.json")
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.auth(sub) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ram_tool.rb', line 69

def auth(sub)
  case sub
  when "register"
    if File.exist?(PIN_FILE)
      puts "PIN already registered."
      return
    end

    pin1 = prompt_pin("Enter new PIN: ")
    pin2 = prompt_pin("Confirm new PIN: ")

    unless pin1 == pin2
      puts "PINs do not match."
      exit 1
    end

    File.write(PIN_FILE, pin1)
    puts "Registration successful."

  when "login"
    unless File.exist?(PIN_FILE)
      puts "No PIN registered."
      exit 1
    end

    pin = prompt_pin("Enter PIN: ")

    if pin == File.read(PIN_FILE).strip
      File.write(SESSION_FILE, "logged_in")
      puts "Login successful."
    else
      puts "Invalid PIN."
    end

  when "logout"
    unless logged_in?
      puts "Not logged in."
      return
    end

    pin = prompt_pin("Enter PIN to logout: ")

    if pin == File.read(PIN_FILE).strip
      File.delete(SESSION_FILE)
      puts "Logged out."
    else
      puts "Invalid PIN."
    end

  when "status"
    puts(logged_in? ? "Status: Logged in" : "Status: Logged out")

  when "reset"
    unless File.exist?(PIN_FILE)
      puts "No PIN registered."
      exit 1
    end

    current = prompt_pin("Enter current PIN: ")

    unless current == File.read(PIN_FILE).strip
      puts "Invalid PIN."
      exit 1
    end

    new_pin = prompt_pin("Enter new PIN: ")
    confirm = prompt_pin("Confirm new PIN: ")

    unless new_pin == confirm
      puts "PINs do not match."
      exit 1
    end

    File.write(PIN_FILE, new_pin)
    puts "PIN reset successful."

  else
    help
  end
end

.get_ramObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ram_tool.rb', line 150

def get_ram
  unless logged_in?
    puts "Please login first."
    exit 1
  end

  response = Net::HTTP.get_response(URL)

  if response.is_a?(Net::HTTPSuccess)
    json = JSON.parse(response.body)
    puts "RAM: #{json["ram_mb"]} MB"
  else
    puts "HTTP #{response.code}"
  end
end

.helpObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ram_tool.rb', line 24

def help
  puts <<~TEXT
    ram-tool #{VERSION}

    Usage:
      ram-tool [command]

    Commands:
      auth register
      auth login
      auth logout
      auth status
      auth reset
      get

    Options:
      -h, --help
      -v, --version
  TEXT
end

.logged_in?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/ram_tool.rb', line 20

def logged_in?
  File.exist?(SESSION_FILE)
end

.prompt_pin(prompt) ⇒ Object



15
16
17
18
# File 'lib/ram_tool.rb', line 15

def prompt_pin(prompt)
  print prompt
  STDIN.noecho(&:gets).to_s.strip.tap { puts }
end

.run(argv) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ram_tool.rb', line 45

def run(argv)
  cmd = argv[0]
  sub = argv[1]

  case cmd
  when "--version", "-v"
    puts "ram-tool #{VERSION}"

  when "--help", "-h", nil
    help

  when "auth"
    auth(sub)

  when "get"
    get_ram

  else
    puts "Unknown command: #{cmd}"
    help
    exit 1
  end
end