From 881518f1f34534b6ce06b51a427db83b7a5cd977 Mon Sep 17 00:00:00 2001 From: AnithaPal Date: Wed, 27 Jul 2016 17:02:40 -0400 Subject: [PATCH] cycle method added to auto cycle through tweets and exception handling is modified to capture all errors that raise from twitter --- INSTALL.md | 5 +++ .../javascripts/refresh_current_events.js | 13 +++++++- .../admin/conferences_controller.rb | 13 ++++---- app/models/event.rb | 14 ++++----- app/models/twitter_wrapper.rb | 20 +++++------- .../conference_wide_screen.html.haml | 31 ++++++++----------- config/secrets.yml.example | 7 +++++ dotenv.example | 6 ++++ .../admin/conferences_controller_spec.rb | 5 ++- spec/models/event_spec.rb | 15 +++++---- 10 files changed, 73 insertions(+), 56 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index cc363338..afd2ded8 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -57,6 +57,11 @@ There are a couple of environment variables you can set to configure OSEM. We use [Stripe](https://stripe.com) for accepting your ticket payments securely over the web. Our application uses iFrame for accepting your user's payment details without storing them, making the application PCI SAQ-A Compliant. Please refer to [PAYMENTS](PAYMENTS.md) documentation file for setting up your stripe account and start accepting payments from your users. +| CLOUDINARY_URL | *string* | Configure your cloudinary.com cloud name and api key/secret +| OSEM_TWITTER_CONSUMER_KEY | *string* | To embed tweets about the conference in the conference wide page +| OSEM_TWITTER_CONSUMER_SECRET | *string* | To embed tweets about the conference in the conference wide page +| OSEM_TWITTER_ACCESS_TOKEN | *string* | To embed tweets about the conference in the conference wide page +| OSEM_TWITTER_ACCESS_TOKEN_SECRET | *string* | To embed tweets about the conference in the conference wide page ## Dependencies diff --git a/app/assets/javascripts/refresh_current_events.js b/app/assets/javascripts/refresh_current_events.js index eee62bec..2d9ac5e0 100644 --- a/app/assets/javascripts/refresh_current_events.js +++ b/app/assets/javascripts/refresh_current_events.js @@ -1,6 +1,17 @@ $(document).ready(function() { // will call refreshCurrentEvents every 15 minutes - setInterval(refreshCurrentEvents, 900000) + setInterval(refreshCurrentEvents, 900000) + // To cycle through tweets + var tweets = $('div[id^="tweet"]').hide(); + var i = 0; + + (function cycle() { + tweets.eq(i).show(0) + .delay(5000) + .hide(0, cycle); + i = ++i % tweets.length; + })(); + }); function refreshCurrentEvents(){ diff --git a/app/controllers/admin/conferences_controller.rb b/app/controllers/admin/conferences_controller.rb index cd1af181..f3d87af1 100644 --- a/app/controllers/admin/conferences_controller.rb +++ b/app/controllers/admin/conferences_controller.rb @@ -174,20 +174,21 @@ module Admin end def conference_wide_screen - #To display sponsors in the conference wide information page + # To display sponsors in the conference wide information page @conference = Conference.find_by(short_title: params[:id]) @sponsors = @conference.sponsors @program = @conference.program - @current_events = @conference.program.events.confirmed.select{ |event| event.is_current?} - @tweets = twitter_client.search_tweets(3, @conference.contact.social_tag) - + @current_events = @conference.program.events.confirmed.select(&:current?) + @tweets = twitter_client.search_tweets(15, @conference.contact.social_tag) + respond_to do |format| - format.html{render layout: "conference_wide_screen"} - format.js {render :action=> "conference_wide_screen.js.haml"} + format.html{render layout: 'conference_wide_screen'} + format.js {render action: 'conference_wide_screen.js.haml'} end end private + # To create a twitter client def twitter_client @twitter_client ||= TwitterWrapper.new diff --git a/app/models/event.rb b/app/models/event.rb index db8338d2..da7efd0f 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -41,8 +41,6 @@ class Event < ActiveRecord::Base scope :canceled, -> { where(state: 'canceled') } scope :withdrawn, -> { where(state: 'withdrawn') } scope :highlighted, -> { where(is_highlight: true) } - - state_machine initial: :new do state :new @@ -250,10 +248,10 @@ class Event < ActiveRecord::Base end ## - #Compares event start_time, end_with with current time to predict current events + # Compares event start_time, end_with with current time to predict current events # - def is_current? - Time.current >= self.start_time && Time.current <= end_time + def current? + Time.current >= self.start_time && Time.current <= self.end_time end private @@ -305,10 +303,10 @@ class Event < ActiveRecord::Base end ## - #Compares event start_time, end_with with current time to predict current events + # Compares event start_time, end_with with current time to predict current events # - def is_current? - Time.current >= self.start_time && Time.current <= end_time + def current? + Time.current >= self.start_time && Time.current <= self.end_time end end diff --git a/app/models/twitter_wrapper.rb b/app/models/twitter_wrapper.rb index 63b47547..d4887720 100644 --- a/app/models/twitter_wrapper.rb +++ b/app/models/twitter_wrapper.rb @@ -3,23 +3,19 @@ class TwitterWrapper def initialize @client = Twitter::REST::Client.new do |config| - config.consumer_key = ENV["OSEM_TWITTER_CONSUMER_KEY"] - config.consumer_secret = ENV["OSEM_TWITTER_CONSUMER_SECRET"] - config.access_token = ENV["OSEM_TWITTER_ACCESS_TOKEN"] - config.access_token_secret = ENV["OSEM_TWITTER_ACCESS_TOKEN_SECRET"] + config.consumer_key = ENV['OSEM_TWITTER_CONSUMER_KEY'] + config.consumer_secret = ENV['OSEM_TWITTER_CONSUMER_SECRET'] + config.access_token = ENV['OSEM_TWITTER_ACCESS_TOKEN'] + config.access_token_secret = ENV['OSEM_TWITTER_ACCESS_TOKEN_SECRET'] end end ## - #Fetches tweets from twitter api based on search_term and no_of_tweets + # Fetches tweets from twitter api based on search_term and no_of_tweets # def search_tweets(no_of_tweets, search_term) - begin - @client.search(search_term, result_type: "recent").take(no_of_tweets).collect - rescue Twitter::Error::TooManyRequests => error - sleep error.rate_limit.reset_in + 1 - retry - end + @client.search(search_term, result_type: 'recent').take(no_of_tweets).collect + rescue + nil end - end diff --git a/app/views/admin/conference/conference_wide_screen.html.haml b/app/views/admin/conference/conference_wide_screen.html.haml index 82a58b2c..4e2991fe 100644 --- a/app/views/admin/conference/conference_wide_screen.html.haml +++ b/app/views/admin/conference/conference_wide_screen.html.haml @@ -1,21 +1,16 @@ -#main-display - - if @conference.present? - %section#main-heading - .container - .row - .col-md-12.text-center - %h1 - = @conference.title - - if @conference.description.present? - %h3 - = @conference.description +- if @conference.present? + .row + .col-md-12.text-center + %h1 + = @conference.title + - if @conference.description.present? + %h3 + = @conference.description - if @program.present? - %section#event-display + .row = render 'current_events' - - if @conference.contact.social_tag.present? - %section#tweets-display - - if @tweets.present? - = render 'conference_tweets' + - if @conference.contact.social_tag.present? && @tweets.present? + .row + = render 'conference_tweets' - if @sponsors.any? - %section#sponsor-display - = render 'sponsors' + = render 'sponsors' diff --git a/config/secrets.yml.example b/config/secrets.yml.example index a8d67504..0ee787cc 100644 --- a/config/secrets.yml.example +++ b/config/secrets.yml.example @@ -54,6 +54,13 @@ production: facebook_key: '' facebook_secret: '' + # Register your application with Twitter from + # https://apps.twitter.com/ + twitter_consumer_key: '' + twitter_consumer_secret: '' + twitter_access_token: '' + twitter_access_token_secret: '' + # Developers do not need to register their application for suse account to # work. You must, however, add some sample value to the variables, for the # login option to appear. For example: diff --git a/dotenv.example b/dotenv.example index ec2715ea..92c51f6d 100644 --- a/dotenv.example +++ b/dotenv.example @@ -57,3 +57,9 @@ OSEM_SMTP_DOMAIN="" # Enable the usage of the devise ichain plugin OSEM_ICHAIN_ENABLED=false + +# To embed tweets about the conference in the conference wide screen +OSEM_TWITTER_CONSUMER_KEY="" +OSEM_TWITTER_CONSUMER_SECRET="" +OSEM_TWITTER_ACCESS_TOKEN="" +OSEM_TWITTER_ACCESS_TOKEN_SECRET="" diff --git a/spec/controllers/admin/conferences_controller_spec.rb b/spec/controllers/admin/conferences_controller_spec.rb index 85c32c82..ac377a69 100644 --- a/spec/controllers/admin/conferences_controller_spec.rb +++ b/spec/controllers/admin/conferences_controller_spec.rb @@ -296,8 +296,8 @@ describe Admin::ConferencesController do describe 'GET #conference_wide_screen' do it 'requires organizer privileges' do get :conference_wide_screen, id: conference.short_title, - conference: attributes_for(:conference, - short_title: 'ExCon') + conference: attributes_for(:conference, + short_title: 'ExCon') expect(response).to redirect_to(send(path)) if message expect(flash[:alert]).to match(/#{message}/) @@ -306,7 +306,6 @@ describe Admin::ConferencesController do end end - describe 'participant access' do before(:each) do sign_in(participant) diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 709a3bc1..77aa4566 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -328,27 +328,26 @@ describe Event do end end - - describe 'is_current?' do + describe 'current?' do let(:past_event) { create(:event, program: conference.program) } let(:future_event) { create(:event, program: conference.program) } before do - event.update_attributes(start_time: "#{Time.current}") - past_event.update_attributes(start_time: "#{Time.current - 2.days}") - future_event.update_attributes(start_time: "#{Time.current + 2.days}") + event.update_attributes(start_time: Time.current.to_s) + past_event.update_attributes(start_time: (Time.current - 2.days).to_s) + future_event.update_attributes(start_time: (Time.current + 2.days).to_s) end it 'returns false for a completed event' do - expect(past_event.is_current?).to be_falsey + expect(past_event.current?).to be_falsey end it 'returns false for a future event' do - expect(future_event.is_current?).to be_falsey + expect(future_event.current?).to be_falsey end it 'returns true for a current event' do - expect(event.is_current?).to be_truthy + expect(event.current?).to be_truthy end end