added sharing to Tumblr

This commit is contained in:
nilsding 2015-01-13 00:12:48 +01:00
parent f8a20099fa
commit 65e514c55d
6 changed files with 73 additions and 3 deletions

View File

@ -37,7 +37,6 @@ gem 'ruby-progressbar'
gem 'rails_admin' gem 'rails_admin'
gem 'twitter'
gem 'sidekiq' gem 'sidekiq'
gem 'sinatra', require: false gem 'sinatra', require: false
@ -49,6 +48,11 @@ gem 'redcarpet'
# OmniAuth and providers # OmniAuth and providers
gem 'omniauth' gem 'omniauth'
gem 'omniauth-twitter' gem 'omniauth-twitter'
gem 'omniauth-tumblr'
# OAuth clients
gem 'twitter'
gem 'tumblr_client'
gem 'foreman' gem 'foreman'
gem 'redis' gem 'redis'

View File

@ -111,6 +111,8 @@ GEM
i18n (~> 0.5) i18n (~> 0.5)
faraday (0.9.0) faraday (0.9.0)
multipart-post (>= 1.2, < 3) multipart-post (>= 1.2, < 3)
faraday_middleware (0.9.1)
faraday (>= 0.7.4, < 0.10)
font-awesome-rails (4.2.0.0) font-awesome-rails (4.2.0.0)
railties (>= 3.2, < 5.0) railties (>= 3.2, < 5.0)
font-kit-rails (1.1.0) font-kit-rails (1.1.0)
@ -170,6 +172,8 @@ GEM
omniauth-oauth (1.0.1) omniauth-oauth (1.0.1)
oauth oauth
omniauth (~> 1.0) omniauth (~> 1.0)
omniauth-tumblr (1.1)
omniauth-oauth (~> 1.0)
omniauth-twitter (1.1.0) omniauth-twitter (1.1.0)
multi_json (~> 1.3) multi_json (~> 1.3)
omniauth-oauth (~> 1.0) omniauth-oauth (~> 1.0)
@ -308,6 +312,13 @@ GEM
tilt (1.4.1) tilt (1.4.1)
timers (4.0.1) timers (4.0.1)
hitimes hitimes
tumblr_client (0.8.5)
faraday (~> 0.9.0)
faraday_middleware (~> 0.9.0)
json
mime-types
oauth
simple_oauth
turbolinks (2.5.3) turbolinks (2.5.3)
coffee-rails coffee-rails
twitter (5.13.0) twitter (5.13.0)
@ -368,6 +379,7 @@ DEPENDENCIES
jquery-turbolinks jquery-turbolinks
mysql2 mysql2
omniauth omniauth
omniauth-tumblr
omniauth-twitter omniauth-twitter
paperclip (~> 4.2) paperclip (~> 4.2)
pg pg
@ -389,6 +401,7 @@ DEPENDENCIES
spring spring
sweetalert-rails sweetalert-rails
thin thin
tumblr_client
turbolinks turbolinks
twitter twitter
uglifier (>= 1.3.0) uglifier (>= 1.3.0)

View File

@ -0,0 +1,45 @@
class Services::Tumblr < Service
include Rails.application.routes.url_helpers
include MarkdownHelper
def provider
"tumblr"
end
def post(answer)
Rails.logger.debug "posting to Tumblr {'answer' => #{answer.id}, 'user' => #{self.user_id}}"
create_post answer
end
private
def client
@client ||= Tumblr::Client.new(
consumer_key: APP_CONFIG['sharing']['tumblr']['consumer_key'],
consumer_secret: APP_CONFIG['sharing']['tumblr']['consumer_secret'],
oauth_token: self.access_token,
oauth_token_secret: self.access_secret
)
end
def create_post(answer)
answer_url = show_user_answer_url(
id: answer.id,
username: answer.user.screen_name,
host: APP_CONFIG['hostname'],
protocol: (APP_CONFIG['https'] ? :https : :http)
)
asker = if answer.question.author_is_anonymous?
APP_CONFIG['anonymous_name']
else
answer.question.user.display_name.blank? ? answer.question.user.screen_name : answer.question.user.display_name
end
client.text(
self.uid,
title: "#{asker} asked: #{answer.question.content}",
body: "#{answer.content}\n\n[Smile or comment on the answer here](#{answer_url})",
format: 'markdown',
tweet: 'off'
)
end
end

View File

@ -1,5 +1,7 @@
Rails.application.config.middleware.use OmniAuth::Builder do Rails.application.config.middleware.use OmniAuth::Builder do
if APP_CONFIG['sharing']['twitter']['enabled'] %w(facebook twitter tumblr).each do |service|
provider :twitter, APP_CONFIG['sharing']['twitter']['consumer_key'], APP_CONFIG['sharing']['twitter']['consumer_secret'] if APP_CONFIG['sharing'][service]['enabled']
provider service.to_sym, APP_CONFIG['sharing'][service]['consumer_key'], APP_CONFIG['sharing'][service]['consumer_secret']
end
end end
end end

View File

@ -41,6 +41,7 @@ RailsAdmin.config do |config|
Report Report
Service Service
Services::Twitter Services::Twitter
Services::Tumblr
Smile Smile
User User
] ]

View File

@ -21,6 +21,11 @@ sharing:
consumer_secret: '' consumer_secret: ''
facebook: facebook:
enabled: false enabled: false
tumblr:
enabled: true
# Get the tokens from https://www.tumblr.com/oauth/apps
consumer_key: ''
consumer_secret: ''
# Redis # Redis
redis_url: "redis://localhost:6379" redis_url: "redis://localhost:6379"