Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to toot purchased drinks to Mastodon #137

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ gem 'thor', '~> 1.0.1'
gem 'sentry-ruby'
gem 'sentry-rails'

# To toot purchases to Mastodon
gem 'httparty'

group :development do
gem 'faker'
gem 'ruby-prof'
Expand Down
21 changes: 21 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true
require 'uri'
require 'httparty'

class User < ApplicationRecord
validates :name, presence: true
Expand All @@ -23,6 +25,9 @@ def buy(drink)
self.balance -= drink.price
@purchased_drink = drink
save!
if Rails.application.config.mastodon_token && Rails.application.config.mastodon_instance
toot(drink)
end
end
end

Expand All @@ -38,4 +43,20 @@ def initial
return first if first =~ /[a-z]/
'0'
end

def toot(drink)
t = Thread.new do
response = HTTParty.post(
Rails.application.config.mastodon_instance + "/api/v1/statuses",
body: URI.encode_www_form({status: drink.name + '!'}),
:headers => {"Authorization" => 'Bearer ' + Rails.application.config.mastodon_token}
)
case response.code
when 200...204
puts "I tooted! Status ID: " + response.parsed_response['id']
when 300...600
puts "Tooting failed! #{response.code}: #{response}"
end
end
end
end
5 changes: 5 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@

# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true

# Get Mastodon data from environment variables
# Note: ENV["FOO"] returns nil if variable does not exist
config.mastodon_token = ENV["MASTODON_TOKEN"]
config.mastodon_instance = ENV["MASTODON_INSTANCE"] # expected format is https://example.social
end
5 changes: 5 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

# Get Mastodon data from environment variables
# Note: ENV["FOO"] returns nil if variable does not exist
config.mastodon_token = ENV["MASTODON_TOKEN"]
config.mastodon_instance = ENV["MASTODON_INSTANCE"] # expected format is https://example.social
end
5 changes: 5 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@

# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true

# Get Mastodon data from environment variables
# Note: ENV["FOO"] returns nil if variable does not exist
config.mastodon_token = ENV["MASTODON_TOKEN"]
config.mastodon_instance = ENV["MASTODON_INSTANCE"] # expected format is https://example.social
end