summaryrefslogtreecommitdiff
path: root/app/helpers
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2016-09-14 11:11:48 +0200
committerAzul <azul@riseup.net>2016-09-14 11:11:48 +0200
commitf575f57915d5369269849352a70b0e4704c3fd19 (patch)
tree7b13646448e64e560101493eeac1a03c3a25dbd8 /app/helpers
parent92481029074f34342cc35937f3aab94aca7887c7 (diff)
parent22478d315af3590d2a344eb1aa8cf2aec0730506 (diff)
Merge remote-tracking branch 'pr/237' into develop
Twitter Feature on Main-View from @loadtocode Twitter feature within home/index of LEAP web app as feed of tweets of one user's twitter account How to use it: set up an Application-only authentication by twitter (https://dev.twitter.com/oauth/application-only) and then create a config/secrets.yml including: ``` development: twitter: enabled: false # set to true for usage twitter_handle: XXXXX #put your twitter handle here bearer_token: XXXXX #put your bearer token here test: twitter: enabled: false # set to true for usage twitter_handle: XXXXX #put your twitter handle here bearer_token: XXXXX #put your bearer token here ``` things in the PR: - [x] gemfile:twitter - [x] twitter helper (caching (to avoid reaching twitter rate limits) - [x] index twitter view - [x] twitter css (twitter design as discussed in call: Profil-Header: Picture, Link to twitter + twitter_handle; removed picture and twitter_handle from individual tweet, heading aligns at the top, only the last 3 tweets are shown) - [x] script to generate bearer token - [x] script to invalidate generated bearer token - [x] documentation (Readme + Doc)
Diffstat (limited to 'app/helpers')
-rw-r--r--app/helpers/twitter_helper.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/app/helpers/twitter_helper.rb b/app/helpers/twitter_helper.rb
new file mode 100644
index 0000000..f824a03
--- /dev/null
+++ b/app/helpers/twitter_helper.rb
@@ -0,0 +1,46 @@
+module TwitterHelper
+ def twitter_enabled
+ if Rails.application.secrets.twitter
+ Rails.application.secrets.twitter['enabled'] == true
+ end
+ end
+
+ def twitter_client
+ Twitter::REST::Client.new do |config|
+ config.bearer_token = Rails.application.secrets.twitter['bearer_token']
+ end
+ end
+
+ def twitter_handle
+ Rails.application.secrets.twitter['twitter_handle']
+ end
+
+ def twitter_user_info
+ $twitter_user_info ||= []
+ end
+
+ def update_twitter_info
+ twitter_user_info[0] = Time.now
+ twitter_user_info[1] = twitter_client.user(twitter_handle).name
+ twitter_user_info[2] = twitter_client.user_timeline(twitter_handle).select{ |tweet| tweet.text.start_with?('RT','@')==false}.take(3)
+ end
+
+ def cached_info
+ 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
+ end
+
+ def twitter_name
+ cached_info[1]
+ end
+
+ def tweets
+ cached_info[2]
+ end
+end