Module: MedicineShield

Defined in:
lib/medicine_shield.rb,
lib/medicine_shield/version.rb,
lib/medicine_shield/commands.rb

Constant Summary collapse

VERSION =
"0.1.0"
APP_NAME =
"medicine_shield"
SCOPE =
"read write follow"

Class Method Summary collapse

Class Method Details

.config_path ⇒ Object



13
14
15
# File 'lib/medicine_shield/commands.rb', line 13

def config_path
  File.expand_path("config.json", CONFIG[:medicine_shield_directory])
end

.create_config ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/medicine_shield/commands.rb', line 35

def create_config
  url = read_from_minibuffer("Mastodon URL: ",
                             default: "https://mstdn.sanin.club")
  mstdn_client = Mastodon::REST::Client.new(base_url: url)
  app = mstdn_client.create_app(APP_NAME, "urn:ietf:wg:oauth:2.0:oob", SCOPE)
  oauth_client = OAuth2::Client.new(app.client_id,
                                    app.client_secret,
                                    site: url)
  email = read_from_minibuffer("Mastodon user e-mail: ")
  password = read_from_minibuffer("Mastodon password: ")
  token = oauth_client.password.get_token(email, password, scope: SCOPE)
  {
    "url" => url,
    "client_id" => app.client_id,
    "client_secret" => app.client_secret,
    "email" => email,
    "access_token" => token.token
  }
end

.get_config ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/medicine_shield/commands.rb', line 17

def get_config
  path = config_path
  begin
    JSON.load(File.read(path))
  rescue Errno::ENOENT
    config = create_config
    FileUtils.mkdir_p(File.dirname(path))
    old_umask = File.umask
    File.umask(0066)
    begin
      File.write(path, config.to_json)
    ensure
      File.umask(old_umask)
    end
    config
  end
end