This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
gem "rails" | |
gem "puma" | |
gem "twitter" | |
gem "omniauth-twitter" | |
end | |
require "action_controller/railtie" | |
class TwitterApp < Rails::Application | |
secrets.secret_token = "secret_token" | |
secrets.secret_key_base = "secret_key_base" | |
config.logger = Logger.new($stdout) | |
Rails.logger = config.logger | |
config.session_store :cookie_store, key: "cookie_store_key" | |
config.middleware.use OmniAuth::Builder do | |
provider :twitter, ENV.fetch("TWITTER_API_KEY"), ENV.fetch("TWITTER_API_SECRET") | |
end | |
OmniAuth.config.allowed_request_methods = [:post, :get] | |
routes.draw do | |
get '/auth/:provider/callback', to: 'sessions#create' | |
end | |
end | |
class SessionsController < ActionController::Base | |
include Rails.application.routes.url_helpers | |
def create | |
auth = request.env['omniauth.auth'] | |
client = Twitter::REST::Client.new do |config| | |
config.consumer_key = ENV.fetch('TWITTER_API_KEY') | |
config.consumer_secret = ENV.fetch('TWITTER_API_SECRET') | |
config.access_token = auth.dig(:credentials, :token) | |
config.access_token_secret = auth.dig(:credentials, :secret) | |
end | |
client.update("Tweeting this from a Rails app") | |
render plain: "All good" | |
end | |
end | |
require "rails/command" | |
require "rails/commands/server/server_command" | |
options = { | |
app: TwitterApp, | |
Host: '0.0.0.0' | |
} | |
Rails::Server.new(options).start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment