diff options
author | Marie <ml.kochsiek@fu-berlin.de> | 2016-09-08 16:16:49 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-08 16:16:49 +0000 |
commit | c0aaafcd048c4a2ac74f582ee8d6383c46558427 (patch) | |
tree | 630d88039b2e7127290cab2df8889e3029a46243 | |
parent | 3b638103ae7c6094d79812f4cbd44bfce320a217 (diff) | |
parent | 160c3a961943aa996982a8d84403957afef0a459 (diff) |
Merge pull request #2 from LoadToCode/thea
Bearer Token + Documentation
-rw-r--r-- | README.md | 26 | ||||
-rw-r--r-- | app/helpers/twitter_helper.rb | 38 | ||||
-rw-r--r-- | app/views/twitter/_index.html.erb | 2 | ||||
-rwxr-xr-x | script/generate_bearer_token | 84 |
4 files changed, 146 insertions, 4 deletions
@@ -187,3 +187,29 @@ Known problems designed to allow query of a user database via proxy in order to provide network perspective. +Twitter Timeline on Main View +--------------------------- + +This is a feature to include a twitter feed that displays most recent tweets +of a (determined) twitter account (accessed via Twitter API). +If you chose to use it, the feature gets included in `home/index` of +LEAP web app (as part of the main view). + +* Create Twitter Application on https://apps.twitter.com/ + * Visit https://apps.twitter.com/ and log in with the twitter account you want to use + * Make sure you have a mobile phone number registered with your account to be able to proceed + * Choose the option to `Create New App` + * Fill in Application Details and Developer Agreement and `Create your Twitter application` + * Choose the section "Keys and Access Tokens" to get your consumer key and consumer secret + * Optional: Go to section "Permissions" and change the "Access" from `Read and Write` (by default) to `Read only` + * Have your consumer key and secret by hand for one of the next steps + +* Activate the feature within your local LEAP Web Application + * If not already existing create a secrets-file in /config with the name secrets.yml (`/config/secrets.yml`) + * Secrets-file should contain the following, make sure its in YAML: {"development"=> {"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}}, "test"=>{"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}}} + * To have your bearer token created, run script in terminal being in the file of leap_web: `script/generate_bearer_token` + * To have the script run properly you have to add before running: `--key your_consumerkey --secret your_consumersecret` + * Add also `--projectroot your_projectroot --twitterhandle your_twitterhandle` as well to not have manually put the data in your secrets-file + * The full command looks like this: `script/generate_bearer_token --key your_consumerkey --secret your_consumersecret --projectroot your_projectroot --twitterhandle your_twitterhandle` + * If you didn't give all your information to the script, had a typo or want to change anything else, please do so by finding the secrets-file at `/config/secrets.yml` + * Make sure that the correct twitterhandle and bearer-token is included diff --git a/app/helpers/twitter_helper.rb b/app/helpers/twitter_helper.rb index 1081c05..a5e7ae4 100644 --- a/app/helpers/twitter_helper.rb +++ b/app/helpers/twitter_helper.rb @@ -15,11 +15,43 @@ module TwitterHelper Rails.application.secrets.twitter['twitter_handle'] end + def twitter_user_info + $twitter_user_info ||= [] + end + def twitter_name - twitter_client.user(twitter_handle).name + if twitter_user_info[0] == nil + update_twitter_info + else + if Time.now > twitter_user_info[0] + 15.minutes + update_twitter_info + end + end + twitter_user_info[1] + end + + def update_twitter_info + twitter_user_info[0] = Time.now + twitter_user_info[1] = twitter_client.user(twitter_handle).name + end + + def twitter_tweets + $twitter_tweets ||= [] + end + + def twitter_timeline + if twitter_tweets[0] == nil + update_twitter_timeline + else + if Time.now > twitter_tweets[0] + 15.minutes + update_twitter_timeline + end + end + twitter_tweets[1] end - def tweets - twitter_client.user_timeline(twitter_handle).select{ |tweet| tweet.text.start_with?('RT','@')==false}.take(3) + def update_twitter_timeline + twitter_tweets[0] = Time.now + twitter_tweets[1] = twitter_client.user_timeline(twitter_handle).select{ |tweet| tweet.text.start_with?('RT','@')==false}.take(3) end end diff --git a/app/views/twitter/_index.html.erb b/app/views/twitter/_index.html.erb index 2d17aad..ac10aa4 100644 --- a/app/views/twitter/_index.html.erb +++ b/app/views/twitter/_index.html.erb @@ -11,7 +11,7 @@ </div> <div class="twitter_list"> - <% tweets.each do |e| %> + <% twitter_timeline.each do |e| %> <div class="tweet"> <div class="tweet_text"><%= " #{e.text}" %> </div> diff --git a/script/generate_bearer_token b/script/generate_bearer_token new file mode 100755 index 0000000..7091a8d --- /dev/null +++ b/script/generate_bearer_token @@ -0,0 +1,84 @@ +#!/usr/bin/env ruby + +require "net/http" +require "uri" +require "json" +require "base64" +require "optparse" +require "yaml" + +options = {} + +option_parser = OptionParser.new do |opts| + opts.banner = "Create your bearer_token for twitter by including following two [options], feel free to have your secrets-file created/filled giving the other information as well:" + + opts.on("--key KEY", "consumer_key of your twitter application") do |key| + options[:conkey] = key + end + + opts.on("--secret SECRET", "consumer_secret of your twitter application") do |secret| + options[:consec] = secret + end + + opts.on("--projectroot DIR", "directory where leapweb is") do |projectroot| + options[:projectroot] = projectroot + end + + opts.on("--twitterhandle TWI", "twitterhandle without @ which will be passed into secrets-file") do |twitterhandle| + options[:twitterhandle] = twitterhandle + end + +end + +option_parser.parse! + +if options[:conkey].nil? || options[:consec].nil? then + puts option_parser + exit +else + consumer_key = options[:conkey] + consumer_secret = options[:consec] +end + +uri = URI("https://api.twitter.com/oauth2/token") +data = "grant_type=client_credentials" +cre = Base64.strict_encode64("#{consumer_key}:#{consumer_secret}") +authorization_headers = { "Authorization" => "Basic #{cre}"} + +Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| + response = http.request_post(uri, data, authorization_headers) + token_hash = JSON.parse(response.body) + $bearer_token = token_hash["access_token"] +end + +if options[:projectroot].nil? then + puts "You didn't tell us the directory to have your secrets-file created or being filled. Feel free to copy/paste your bearer_token:" + puts $bearer_token +else + if File.exist?("#{options[:projectroot]}/leap_web/config/secrets.yml") + secrets = YAML.load_file("#{options[:projectroot]}/leap_web/config/secrets.yml") + else + puts "Please make sure that you created a secrets-file as described in the documentation or have given the correct directory. No secrets-file could be found." + exit + # secrets_content = {"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}} + # secrets = {"development"=> secrets_content, "test"=>secrets_content} + # secrets = {"development"=> {"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}}, "test"=>{"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}}} + # File.new("#{options[:projectroot]}/leap_web/config/secrets.yml", "w") + end + + if options[:twitterhandle].nil? then + if secrets["development"]["twitter"]["twitter_handle"] == "" then + puts "You didn't put your twitter-handle neither in the secrets-file nor passed it as a flag. Don't forget that you can't use the twitter-feature without your twitter-handle." + end + else + secrets["development"]["twitter"]["twitter_handle"] = options[:twitterhandle] + secrets["test"]["twitter"]["twitter_handle"] = options[:twitterhandle] + end + + secrets["development"]["twitter"]["bearer_token"] = $bearer_token + secrets["test"]["twitter"]["bearer_token"] = $bearer_token + + File.open("#{options[:projectroot]}/leap_web/config/secrets.yml", "r+") do |file| + file.write(secrets.to_yaml) + end +end |