Class: Jekyll::MastodonSocial::MastodonSetup
- Inherits:
-
Command
- Object
- Command
- Jekyll::MastodonSocial::MastodonSetup
- Defined in:
- lib/jekyll/commands/mastodon-social.rb
Class Method Summary collapse
- .clean ⇒ Object
- .create_client_id(options = {}) ⇒ Object
- .do_help ⇒ Object
- .get_client(options = {}) ⇒ Object
- .get_token ⇒ Object
- .init_with_program(prog) ⇒ Object
- .mark(options = {}) ⇒ Object
-
.post(options = {}) ⇒ Object
Look for any blog posts that haven’t been sent to Mastodon, and post a status for each.
Class Method Details
.clean ⇒ Object
84 85 86 87 88 |
# File 'lib/jekyll/commands/mastodon-social.rb', line 84 def clean client = get_client() MastodonSocial.clear_status() MastodonSocial.save_config() end |
.create_client_id(options = {}) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/jekyll/commands/mastodon-social.rb', line 41 def create_client_id( = {}) client = get_client() app = client.create_app("jekyll-social", MastodonSocial.site.config["url"], 'read write') MastodonSocial.client_id = app.client_id MastodonSocial.client_secret = app.client_secret MastodonSocial.save_config() end |
.do_help ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/jekyll/commands/mastodon-social.rb', line 116 def do_help puts <<-END_HELP jekyll mastodon [command] Commands: setup: Initial setup authorizing this plugin to post to your account authorize: Enter your password to authorize this plugin to post to your Mastodon account mark: Mark all posts as published (to Mastodon) post: Publish any new posts to Mastodon clean: Erase all cached mastodon data END_HELP end |
.get_client(options = {}) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/jekyll/commands/mastodon-social.rb', line 90 def get_client( = {}) = () site = Jekyll::Site.new() MastodonSocial.setup(site) return Mastodon::REST::Client.new(base_url: MastodonSocial.config["server"], bearer_token: MastodonSocial.bearer_token) end |
.get_token ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/jekyll/commands/mastodon-social.rb', line 49 def get_token puts "Enter your Mastodon credentials for the account you want to post to" print "Account email: " email = STDIN.cooked(&:gets).chomp print "Account password: " password = STDIN.noecho(&:gets).chomp client = get_client() auth_uri = URI.parse("#{MastodonSocial.config['server']}/oauth/token") Net::HTTP.start(auth_uri.host, auth_uri.port, :use_ssl => auth_uri.scheme == 'https') do |http| request = Net::HTTP::Post.new(auth_uri, 'Content-Type' => 'application/json') request.body = { 'client_id': MastodonSocial.client_id, 'client_secret': MastodonSocial.client_secret, 'grant_type': 'password', 'username': email, 'password': password, 'scope': 'read write' }.to_json response = http.request request json_response = JSON.parse(response.body) MastodonSocial.bearer_token = json_response['access_token'] end MastodonSocial.save_config() end |
.init_with_program(prog) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jekyll/commands/mastodon-social.rb', line 12 def init_with_program(prog) prog.command(:mastodon) do |c| c.syntax('mastodon') c.description("Connect your blog with the Fediverse") c.action do |args, opts| if args.empty? do_help else case args[0] when /setup/ create_client_id opts when /mark/ mark opts when /post/ post opts when /clean/ clean when /help/ do_help when /auth/ get_token else puts "Error, try 'mastodon help'" end end end end end |
.mark(options = {}) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/jekyll/commands/mastodon-social.rb', line 75 def mark( = {}) client = get_client() # Scan through all posts and see which ones need to be posted to Mastodon for post_url in MastodonSocial.mastodon_status.keys MastodonSocial.mark_as_published(post_url, true) end MastodonSocial.save_config() end |
.post(options = {}) ⇒ Object
Look for any blog posts that haven’t been sent to Mastodon, and post a status for each
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/jekyll/commands/mastodon-social.rb', line 98 def post( = {}) client = get_client() for post_url, status in MastodonSocial.mastodon_status next if status[:mastodon_status] puts "Publishing #{post_url} to Mastodon" msg_text = <<~ENDMSG #{status[:excerpt].gsub("\n", '')} #{MastodonSocial.site.config["url"]}#{post_url} ENDMSG msg_text += ("\n#" + status[:hashtags].join(' #')) if status[:hashtags] status_result = client.create_status(msg_text) new_status = {id: status_result.id, url: status_result.url} MastodonSocial.mark_as_published(post_url, new_status) end MastodonSocial.save_config() end |