Class: MastodonAccount
- Inherits:
-
Object
- Object
- MastodonAccount
- Defined in:
- app/mastodon_account.rb
Constant Summary collapse
- APP_NAME =
"tootify"
- CONFIG_FILE =
File.(File.join(__dir__, '..', 'config', 'mastodon.yml'))
- OAUTH_SCOPES =
'read:accounts read:statuses write:media write:statuses'
Instance Method Summary collapse
-
#initialize ⇒ MastodonAccount
constructor
A new instance of MastodonAccount.
- #oauth_login(handle, email, password) ⇒ Object
- #post_status(text) ⇒ Object
- #register_oauth_app(instance, scopes) ⇒ Object
- #save_config ⇒ Object
Constructor Details
#initialize ⇒ MastodonAccount
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 oauth_login(handle, email, password) instance = handle.split('@').last app_response = register_oauth_app(instance, OAUTH_SCOPES) api = MastodonAPI.new(instance) json = api.oauth_login_with_password( app_response.client_id, app_response.client_secret, email, password, OAUTH_SCOPES ) api.access_token = json['access_token'] info = api.account_info @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_config ⇒ Object
15 16 17 |
# File 'app/mastodon_account.rb', line 15 def save_config File.write(CONFIG_FILE, YAML.dump(@config)) end |