Class: MastodonAccount

Inherits:
Object
  • Object
show all
Defined in:
app/mastodon_account.rb

Constant Summary collapse

APP_NAME =
"tootify"
CONFIG_FILE =
File.expand_path(File.join(__dir__, '..', 'config', 'mastodon.yml'))
OAUTH_SCOPES =
'read:accounts read:statuses write:media write:statuses'

Instance Method Summary collapse

Constructor Details

#initializeMastodonAccount

Returns a new instance of MastodonAccount.



11
12
13
# File 'app/mastodon_account.rb', line 11

def initialize
  @config = File.exist?(CONFIG_FILE) ? YAML.load(File.read(CONFIG_FILE)) : {}
end

Instance Method Details

#oauth_login(handle, email, password) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/mastodon_account.rb', line 19

def (handle, email, password)
  instance = handle.split('@').last
  app_response = register_oauth_app(instance, OAUTH_SCOPES)

  api = MastodonAPI.new(instance)

  json = api.(
    app_response.client_id,
    app_response.client_secret,
    email, password, OAUTH_SCOPES
  )

  api.access_token = json['access_token']
  info = api.

  @config['handle'] = handle
  @config['access_token'] = api.access_token
  @config['user_id'] = info['id']
  save_config
end

#post_status(text) ⇒ Object



45
46
47
48
49
# File 'app/mastodon_account.rb', line 45

def post_status(text)
  instance = @config['handle'].split('@').last
  api = MastodonAPI.new(instance, @config['access_token'])
  api.post_status(text)
end

#register_oauth_app(instance, scopes) ⇒ Object



40
41
42
43
# File 'app/mastodon_account.rb', line 40

def register_oauth_app(instance, scopes)
  client = Mastodon::REST::Client.new(base_url: "https://#{instance}")
  client.create_app(APP_NAME, 'urn:ietf:wg:oauth:2.0:oob', scopes)
end

#save_configObject



15
16
17
# File 'app/mastodon_account.rb', line 15

def save_config
  File.write(CONFIG_FILE, YAML.dump(@config))
end