Last active
December 13, 2015 16:49
-
-
Save danhigham/4943057 to your computer and use it in GitHub Desktop.
Simple session persistence with Redis and Sinatra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'sinatra/base' | |
| require 'httparty' | |
| require 'redis' | |
| require 'json' | |
| class SessionApp < Sinatra::Base | |
| set :sessions, true | |
| get "/" do | |
| session_id = session[:session_id] | |
| redis = Redis.new | |
| tweet_hash = JSON.parse(redis.get(session_id) || '[]') | |
| tweet = HTTParty.get('http://search.twitter.com/search.json?q=twitter&rpp=1').parsed_response['results'].first | |
| tweet_hash << tweet | |
| redis.set session_id, tweet_hash.to_json | |
| tweet_hash.inspect | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require './app' | |
| run SessionApp.new |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source :rubygems | |
| gem 'sinatra' | |
| gem 'redis' | |
| gem 'httparty' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment