From 99e36b637e43630a3178812c02723348688cb015 Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 11:59:01 -0500 Subject: [PATCH 01/12] Add tumblr token and secret columns to destinations table --- .../20160424115643_add_tumblr_fields_to_destination.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 db/migrate/20160424115643_add_tumblr_fields_to_destination.rb diff --git a/db/migrate/20160424115643_add_tumblr_fields_to_destination.rb b/db/migrate/20160424115643_add_tumblr_fields_to_destination.rb new file mode 100644 index 0000000..b680216 --- /dev/null +++ b/db/migrate/20160424115643_add_tumblr_fields_to_destination.rb @@ -0,0 +1,6 @@ +class AddTumblrFieldsToDestination < ActiveRecord::Migration + def change + add_column :destinations, :tumblr_token, :string + add_column :destinations, :tumblr_secret, :string + end +end From 3a0cabc886fe8c899be25ffd163606a9c3459deb Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 12:01:19 -0500 Subject: [PATCH 02/12] Add tumblr link to user page --- app/views/users/show.erb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/views/users/show.erb b/app/views/users/show.erb index 6148917..fb1a187 100644 --- a/app/views/users/show.erb +++ b/app/views/users/show.erb @@ -17,6 +17,11 @@ <% else %>
  • Twitter
  • <% end %> + <% if @user.destinations.first.tumblr_token %> +
  • Tumblr
  • + <% else %> +
  • Tumblr
  • + <% end %>

    Disconnect your social networks

    From 9191d479e5ba3764a5c2b0d170cc58f01d952cb9 Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 12:01:55 -0500 Subject: [PATCH 03/12] Add tumblr authentication path to controller --- app/controllers/destinations_controller.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/destinations_controller.rb b/app/controllers/destinations_controller.rb index 028b85e..2bf1079 100644 --- a/app/controllers/destinations_controller.rb +++ b/app/controllers/destinations_controller.rb @@ -20,3 +20,7 @@ twitter_media_upload("This is something, I swear:", "https://dl.dropboxusercontent.com/s/ephkiagrqgfc0y4/IMG_8489.JPG?raw=1") redirect "/users/#{session[:user_id]}" end + +get '/tumblr-authenticate' do + get_tumblr_info +end \ No newline at end of file From 24509f9e34df2020b0f3c4a498efc93b355e28eb Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 13:43:18 -0500 Subject: [PATCH 04/12] Add callback route for tumblr --- app/controllers/destinations_controller.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/controllers/destinations_controller.rb b/app/controllers/destinations_controller.rb index 2bf1079..556261e 100644 --- a/app/controllers/destinations_controller.rb +++ b/app/controllers/destinations_controller.rb @@ -23,4 +23,10 @@ get '/tumblr-authenticate' do get_tumblr_info +end + +get '/tumblr-callback' do + get_tumblr_access_token + store_tumblr_access_token + redirect "/users/#{session[:user_id]}" end \ No newline at end of file From 8600da45538e60dcf7a174444fc74dabd421ab3c Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 13:43:48 -0500 Subject: [PATCH 05/12] Add required tumblr methods and create helper file for tumblr stuff --- app/helpers/tumblr_helper.rb | 4 +++ app/helpers/tumblr_module.rb | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 app/helpers/tumblr_helper.rb create mode 100644 app/helpers/tumblr_module.rb diff --git a/app/helpers/tumblr_helper.rb b/app/helpers/tumblr_helper.rb new file mode 100644 index 0000000..c8548c0 --- /dev/null +++ b/app/helpers/tumblr_helper.rb @@ -0,0 +1,4 @@ +require_relative 'tumblr_module' +helpers do + include TumblrModule +end diff --git a/app/helpers/tumblr_module.rb b/app/helpers/tumblr_module.rb new file mode 100644 index 0000000..92cdcfc --- /dev/null +++ b/app/helpers/tumblr_module.rb @@ -0,0 +1,51 @@ +module TumblrModule + def get_tumblr_consumer + base = 'https://www.tumblr.com' + consumer = OAuth::Consumer.new(ENV['TUMBLR_KEY'], + ENV['TUMBLR_SECRET'], + { site: base, + # request_token_path: '/oauth/request_token', + # authorize_path: '/oauth/authorize', + # access_token_path: '/oauth/access_token', + # http_method: :post + } ) + end + + def get_tumblr_info + consumer = get_tumblr_consumer + request_token = consumer.get_request_token(oauth_callback: ENV['TUMBLR_CALLBACK']) + session[:tumblr_request_token] = request_token + redirect request_token.authorize_url + end + + def get_tumblr_access_token + session[:tumblr_access_token] = session[:tumblr_request_token].get_access_token(oauth_verifier: params[:oauth_verifier]) + end + + def store_tumblr_access_token + id = session[:user_id] + token = session[:tumblr_access_token].token + secret = session[:tumblr_access_token].secret + destination = Destination.find_by(user_id: id) + if destination + destination.tumblr_secret = secret + destination.tumblr_token = token + destination.save + else + Destination.create(user_id: id, tumblr_secret: secret, tumblr_token: token) + end + end + + def tumblr_media_upload(status_msg, media_url, user_id = current_user.id) + tokens = Destination.find_by(user_id: user_id) + access_token = OAuth::AccessToken.new(get_consumer, tokens.tumblr_token, tokens.tumblr_secret) + + client = tumblr::REST::Client.new do |config| + config.consumer_key = ENV["TUMBLR_KEY"] + config.consumer_secret= ENV["TUMBLR_SECRET"] + config.access_token = access_token.token + config.access_token_secret = access_token.secret + end + client.update_with_media(status_msg, open(media_url)) + end +end \ No newline at end of file From 75298b4fe9b27d563a7030862a5cb2e1b63855f4 Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 14:03:54 -0500 Subject: [PATCH 06/12] Add tumblr gem and rebundle --- Gemfile | 1 + Gemfile.lock | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/Gemfile b/Gemfile index 7b7a97c..3225d90 100644 --- a/Gemfile +++ b/Gemfile @@ -20,6 +20,7 @@ gem 'dotenv' gem 'oauth' gem 'twitter' +gem 'tumblr_client' # Clockwork gem 'clockwork' diff --git a/Gemfile.lock b/Gemfile.lock index 296cf30..c7a44e7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -44,6 +44,8 @@ GEM i18n (~> 0.5) faraday (0.9.2) multipart-post (>= 1.2, < 3) + faraday_middleware (0.9.2) + faraday (>= 0.7.4, < 0.10) http (1.0.4) addressable (~> 2.3) http-cookie (~> 1.0) @@ -109,6 +111,13 @@ GEM tilt (~> 1.3) thread_safe (0.3.4) tilt (1.4.1) + tumblr_client (0.8.5) + faraday (~> 0.9.0) + faraday_middleware (~> 0.9.0) + json + mime-types + oauth + simple_oauth twitter (5.16.0) addressable (~> 2.3) buftok (~> 0.2.0) @@ -152,6 +161,7 @@ DEPENDENCIES simplecov sinatra sinatra-contrib + tumblr_client twitter BUNDLED WITH From 7d3f35a0f6f18aca233069d30b3045bc3c5786e6 Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 14:09:53 -0500 Subject: [PATCH 07/12] Add link to disable tumblr access --- app/views/users/show.erb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/views/users/show.erb b/app/views/users/show.erb index fb1a187..f8e0a1c 100644 --- a/app/views/users/show.erb +++ b/app/views/users/show.erb @@ -29,6 +29,9 @@ <% if !!@user.destinations && @user.destinations.first.twitter_token %>
  • Disconnect your Twitter account
  • <% end %> + <% if !!@user.destinations && @user.destinations.first.tumblr_token %> +
  • Disconnect your Tumblr account
  • + <% end %>

    Push your work up early

    From 6cbbe7a335177f5e76be5b4b9b3a41defa8c65af Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 14:10:10 -0500 Subject: [PATCH 08/12] Add code to tumblr module --- app/helpers/tumblr_module.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/helpers/tumblr_module.rb b/app/helpers/tumblr_module.rb index 92cdcfc..17ddb4a 100644 --- a/app/helpers/tumblr_module.rb +++ b/app/helpers/tumblr_module.rb @@ -24,6 +24,7 @@ def get_tumblr_access_token def store_tumblr_access_token id = session[:user_id] + puts session[:tumblr_access_token].inspect token = session[:tumblr_access_token].token secret = session[:tumblr_access_token].secret destination = Destination.find_by(user_id: id) @@ -40,12 +41,14 @@ def tumblr_media_upload(status_msg, media_url, user_id = current_user.id) tokens = Destination.find_by(user_id: user_id) access_token = OAuth::AccessToken.new(get_consumer, tokens.tumblr_token, tokens.tumblr_secret) - client = tumblr::REST::Client.new do |config| - config.consumer_key = ENV["TUMBLR_KEY"] + Tumblr.configure do |config| + config.consumer_key = ENV["TUMBLR_KEY"] config.consumer_secret= ENV["TUMBLR_SECRET"] - config.access_token = access_token.token - config.access_token_secret = access_token.secret - end - client.update_with_media(status_msg, open(media_url)) + config.oauth_token = access_token.token + config.oauth_token_secret = access_token.secret + end + + client = Tumblr::Client.new(client: :httpclient) + end end \ No newline at end of file From d8ba2fe14534be241139d3936cf3d6e5c46d138b Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 14:15:14 -0500 Subject: [PATCH 09/12] Add some testing links/routes --- app/controllers/destinations_controller.rb | 4 ++++ app/views/users/show.erb | 3 +++ 2 files changed, 7 insertions(+) diff --git a/app/controllers/destinations_controller.rb b/app/controllers/destinations_controller.rb index 556261e..f1dd475 100644 --- a/app/controllers/destinations_controller.rb +++ b/app/controllers/destinations_controller.rb @@ -29,4 +29,8 @@ get_tumblr_access_token store_tumblr_access_token redirect "/users/#{session[:user_id]}" +end + +get '/tumblr-testing' do + tumblr_media_upload() end \ No newline at end of file diff --git a/app/views/users/show.erb b/app/views/users/show.erb index f8e0a1c..2e56a12 100644 --- a/app/views/users/show.erb +++ b/app/views/users/show.erb @@ -39,3 +39,6 @@
    + + +Test tumblr access \ No newline at end of file From e76c6d80131f6f0f7cfcbf94e56c22ce6ed982e1 Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 15:08:10 -0500 Subject: [PATCH 10/12] Some progress towards choosing the blog --- app/helpers/tumblr_module.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/app/helpers/tumblr_module.rb b/app/helpers/tumblr_module.rb index 17ddb4a..46818d0 100644 --- a/app/helpers/tumblr_module.rb +++ b/app/helpers/tumblr_module.rb @@ -1,3 +1,4 @@ +require 'tumblr_client' module TumblrModule def get_tumblr_consumer base = 'https://www.tumblr.com' @@ -37,6 +38,28 @@ def store_tumblr_access_token end end + def get_and_store_tumblr_blog_title + client = get_tumblr_client + client.info["user"]["blogs"].each do |blog| + puts blog["url"] + end + gets.chomp + end + + def get_tumblr_client + tokens = Destination.find_by(user_id: current_user.id) + access_token = OAuth::AccessToken.new(get_consumer, tokens.tumblr_token, tokens.tumblr_secret) + + Tumblr.configure do |config| + config.consumer_key = ENV["TUMBLR_KEY"] + config.consumer_secret= ENV["TUMBLR_SECRET"] + config.oauth_token = access_token.token + config.oauth_token_secret = access_token.secret + end + + client = Tumblr::Client.new + end + def tumblr_media_upload(status_msg, media_url, user_id = current_user.id) tokens = Destination.find_by(user_id: user_id) access_token = OAuth::AccessToken.new(get_consumer, tokens.tumblr_token, tokens.tumblr_secret) @@ -48,7 +71,7 @@ def tumblr_media_upload(status_msg, media_url, user_id = current_user.id) config.oauth_token_secret = access_token.secret end - client = Tumblr::Client.new(client: :httpclient) + client = Tumblr::Client.new end end \ No newline at end of file From 9aa42e3216a6d322b5718c09f4a02be8dde4d51b Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 15:11:32 -0500 Subject: [PATCH 11/12] add migration for tumblr blog address and migrate --- ...24150945_add_tumblr_blog_address_field_to_destinations.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20160424150945_add_tumblr_blog_address_field_to_destinations.rb diff --git a/db/migrate/20160424150945_add_tumblr_blog_address_field_to_destinations.rb b/db/migrate/20160424150945_add_tumblr_blog_address_field_to_destinations.rb new file mode 100644 index 0000000..2ac8c6e --- /dev/null +++ b/db/migrate/20160424150945_add_tumblr_blog_address_field_to_destinations.rb @@ -0,0 +1,5 @@ +class AddTumblrBlogAddressFieldToDestinations < ActiveRecord::Migration + def change + add_column :destinations, :tumblr_blog, :string + end +end From 895350918bcc1d3772cc3059117d1b8f6fef1d3e Mon Sep 17 00:00:00 2001 From: pdynowski Date: Sun, 24 Apr 2016 15:52:14 -0500 Subject: [PATCH 12/12] More work on getting the proper blog address --- app/controllers/destinations_controller.rb | 5 +++-- app/helpers/tumblr_module.rb | 10 +++++++--- app/views/blog_selection.erb | 9 +++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 app/views/blog_selection.erb diff --git a/app/controllers/destinations_controller.rb b/app/controllers/destinations_controller.rb index f1dd475..8ea7e40 100644 --- a/app/controllers/destinations_controller.rb +++ b/app/controllers/destinations_controller.rb @@ -28,9 +28,10 @@ get '/tumblr-callback' do get_tumblr_access_token store_tumblr_access_token - redirect "/users/#{session[:user_id]}" + get_and_store_tumblr_blog_title + # redirect "/users/#{session[:user_id]}" end get '/tumblr-testing' do - tumblr_media_upload() + get_and_store_tumblr_blog_title end \ No newline at end of file diff --git a/app/helpers/tumblr_module.rb b/app/helpers/tumblr_module.rb index 46818d0..211de1f 100644 --- a/app/helpers/tumblr_module.rb +++ b/app/helpers/tumblr_module.rb @@ -25,7 +25,6 @@ def get_tumblr_access_token def store_tumblr_access_token id = session[:user_id] - puts session[:tumblr_access_token].inspect token = session[:tumblr_access_token].token secret = session[:tumblr_access_token].secret destination = Destination.find_by(user_id: id) @@ -40,10 +39,15 @@ def store_tumblr_access_token def get_and_store_tumblr_blog_title client = get_tumblr_client + blogs = [] client.info["user"]["blogs"].each do |blog| - puts blog["url"] + blogs << blog["url"].split('/')[2] end - gets.chomp + erb :blog_selection, layout: false, locals: {blogs: blogs} + end + + def store_tumblr_blog_title + end def get_tumblr_client diff --git a/app/views/blog_selection.erb b/app/views/blog_selection.erb new file mode 100644 index 0000000..d142430 --- /dev/null +++ b/app/views/blog_selection.erb @@ -0,0 +1,9 @@ +

    Please select the blog you want Weeker to post to

    +
    + + +