Permalink
Browse files

initial pass at port, just need to add the redis cache and second tim…

…eline config
  • Loading branch information...
1 parent 81fc793 commit 76f7493fe2935e24d39497d86e587073106eddf1 Mark Lucovsky committed Jul 24, 2012
View
@@ -0,0 +1,2 @@
+*.idea
+/vendor/*
View
@@ -0,0 +1,9 @@
+source "http://rubygems.org"
+gem 'bundler'
+gem 'sinatra'
+gem 'thin'
+gem 'json_pure'
+gem 'haml'
+gem 'redis'
+gem 'logger'
+gem 'httpclient'
View
@@ -0,0 +1,35 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ daemons (1.1.8)
+ eventmachine (0.12.10)
+ haml (3.1.6)
+ httpclient (2.2.5)
+ json_pure (1.7.3)
+ logger (1.2.8)
+ rack (1.4.1)
+ rack-protection (1.2.0)
+ rack
+ redis (3.0.1)
+ sinatra (1.3.2)
+ rack (~> 1.3, >= 1.3.6)
+ rack-protection (~> 1.2)
+ tilt (~> 1.3, >= 1.3.3)
+ thin (1.4.1)
+ daemons (>= 1.0.9)
+ eventmachine (>= 0.12.6)
+ rack (>= 1.0.0)
+ tilt (1.3.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler
+ haml
+ httpclient
+ json_pure
+ logger
+ redis
+ sinatra
+ thin
View
@@ -0,0 +1,14 @@
+default: gaga
+timelines:
+ - key: gaga
+ title: A GaGa Mind
+ button: Speechless
+ page_bg: /img/gaga-background.jpg
+ tweetbox_bg: /img/gaga-box.jpg
+ twittername: ladygaga
+ - key: gaga2
+ title: A GaGa Mind2
+ button: Speechless2
+ page_bg: /img/gaga-background.jpg
+ tweetbox_bg: /img/gaga-box.jpg
+ twittername: marklucovsky
View
94 gaga.rb
@@ -0,0 +1,94 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'sinatra'
+require 'redis'
+require 'haml'
+require 'json/pure'
+require 'pp'
+require 'logger'
+require 'httpclient'
+require 'lib/gaga/helpers.rb'
+
+# establish global logger to stdout
+# use "vmc logs"" or "vmc files gaga logs/stdout.log" to view
+$log = Logger.new(STDOUT)
+$log.level = Logger::DEBUG
+#$log.level = Logger::INFO
+#$log.level = Logger::WARN
+
+# note the auto-magical cf auto-reconfiguration gem will
+# patch in the real redis config gleaned from the environment
+# this saves me from having to write understandable and straight
+# forward binding code. lazy magical people like this voodoo black
+# magic. I prefer the longhand approach but sigh, I'll reluctantly go
+# along, until it breaks, then I will be vocal...
+# $vcap_services = JSON.parse(ENV['VCAP_SERVICES']) if ENV['VCAP_SERVICES']
+# redis = $vcap_services['redis-2.2'][0]
+# redis_conf = {:host => redis['credentials']['hostname'],
+# :port => redis['credentials']['port'],
+# :password => redis['credentials']['password']}
+# $redis = Redis.new redis_conf
+$redis = Redis.new(:host => '127.0.0.1', :port => '6379')
+
+# load config, prep for being able to run the same logic for multiple time lines
+config_file = File.expand_path("config/config.yml", "#{__FILE__}/..")
+$config = File.open(config_file) do |f|
+ YAML.load(f)
+end
+
+$log.debug("$config => #{$config.pretty_inspect}")
+$log.debug("$redis => #{$redis.pretty_inspect}")
+
+# return the page, rendered via haml
+get '/' do
+ get_timeline_config
+ halt 400 if !@config
+
+ haml :index
+end
+
+get '/boot' do
+ content_type :json
+
+ @founders = {
+ "Apple" => ["Steve Jobs", "Steve Wozniak", "Ronald Wayne"],
+ "Dribbble" => ["Dan Cederholm", "Rich Thornett"],
+ "GitHub" => ["Tom Preston-Werner", "Chris Wanstrath", "PJ Hyett"],
+ "Heroku" => ["James Lindenbaum", "Adam Wiggins", "Orion Henry"],
+ "Gowalla" => ["Josh Williams", "Scott Raymond"],
+ "Square" => ["Jack Dorsey", "Tristan O'Tierney", "Jim McKelvey"],
+ "Twitter" => ["Jack Dorsey", "Biz Stone", "Evan Williams"]
+ }
+
+ # Specify response freshness policy for HTTP caches (Cache-Control header).
+ #
+ # See RFC 2616 / 14.9 for more on standard cache control directives:
+ # http://tools.ietf.org/html/rfc2616#section-14.9.1
+ cache_control :public, :must_revalidate, :max_age => 86400
+
+ # Set the last modified time of the resource (HTTP 'Last-Modified' header)
+ # and halt if conditional GET matches.
+ #
+ # When the current request includes an 'If-Modified-Since' header that is
+ # equal or later than the time specified, execution is immediately halted
+ # with a '304 Not Modified' response.
+ last_modified Date.today
+
+ # Set the response entity tag (HTTP 'ETag' header) and halt if conditional
+ # GET matches. The value argument is an identifier that uniquely
+ # identifies the current version of the resource.
+ #
+ # When the current request includes an 'If-None-Match' header with a
+ # matching etag, execution is immediately halted.
+ #
+ # If the request method is GET or HEAD, a '304 Not Modified'.
+ # response is sent.
+ etag Digest::MD5.hexdigest(@founders.to_s)
+
+ # Sleep in order to demonstrate effect of client-side caching.
+ # The first request will be slow, but if the client is obeying caching
+ # directives, subsequent requests will be nearly instantaneous.
+ sleep 5
+
+ return @founders.to_json
+end
View
@@ -0,0 +1,27 @@
+helpers do
+
+ # return the timeline config block from the config
+ # file. IF the timeline param is passed, use that as
+ # the key. if not, then use the default key from the config
+ def get_timeline_config
+ @config = nil
+ # if a timeline param is specified, then use it as a key
+ # if it fails to find a record, the lookup will return
+ # nil, this will cause a second lookup using the default key
+ if params[:timeline]
+ @config = lookup_config_key(params[:timeline])
+ end
+ @config = lookup_config_key($config['default']) if !@config
+ $log.debug("@config => #{@config.pretty_inspect}")
+ end
+
+ def lookup_config_key(key)
+ $config['timelines'].each do |entry|
+ $log.debug("lookup #{key}, trying #{entry}")
+ if entry['key'] == key
+ return entry
+ end
+ end
+ return nil
+ end
+end
View
@@ -0,0 +1,16 @@
+---
+applications:
+ .:
+ instances: 1
+ services:
+ gaga-redis:
+ type: redis
+ name: gaga
+ url: ${name}.${target-base}
+ framework:
+ name: sinatra
+ info:
+ description: Sinatra Application
+ exec: ruby gaga.rb
+ mem: 128M
+ mem: 128M
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,61 @@
+%style(type = 'text/css')
+ :sass
+ body
+ background-color: #000000
+ margin: 0
+ font: 16px/1.4 "Helvetica Neue", "Lucida Grande", "Arial"
+ font-size: 16px
+ line-height: 1.5
+ -webkit-font-smoothing: antialiased
+ color: #dedede
+
+ body
+ background-image: url("#{@config['page_bg']}")
+ background-size: 100%
+ background-repeat: no-repeat
+ background-color: black
+ font-family: "Audiowide", arial
+ color: #FFFFFF
+
+ h1
+ font-size: 50px
+
+ #container
+ position: absolute
+ right: 3%
+ height: 200px
+ width: 52%
+
+ .rounded-box
+ border-radius: 10px
+ -moz-border-radius: 10px
+ -webkit-border-radius: 10px
+ border: 3px solid #FFFFFF
+ background-color: #000000
+
+ #tweet-box
+ min-height: 50px
+ width: 100%
+ background-image: url("#{@config['tweetbox_bg']}")
+ background-size: 100%
+ padding: 5px 5px 5px 5px
+ margin-bottom: 5%
+ color: #000000
+ background-color: #FFFFFF
+ font-size: 20px
+
+ a:link
+ text-decoration: none
+ color: #000000
+
+ a:hover
+ text-decoration: underline
+ color: #000000
+
+ #generate-tweet
+ font-family: "Audiowide", arial
+ color: #FFFFFF
+ font-size: 15px
+
+ #generate-tweet:hover
+ background-color: red
View
@@ -0,0 +1,3 @@
+%head
+ %title A GaGa Mind
+ %meta(http-equiv='content-type' content='text/html charset=utf-8')
View
@@ -0,0 +1,4 @@
+%h1 #{@config['title']}
+%div{:id => "tweet-box", :class => "rounded-box"}
+ #filler
+%button{:id => "generate-tweet", :class => "rounded-box"} #{@config['button']}
View
@@ -0,0 +1,2 @@
+%script(type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js")
+%link(href="http://fonts.googleapis.com/css?family=Audiowide" rel="stylesheet" type="text/css")
View
@@ -0,0 +1,9 @@
+!!! Strict
+%html{:xmlns => "http://www.w3.org/1999/xhtml", :lang => "en", "xml:lang" => "en"}
+ =haml :head, :layout => false
+ =haml :jslibs, :layout => false
+ =haml :css, :layout => false
+ =haml :local_js, :layout => false
+ %body
+ #container
+ =yield
View
@@ -0,0 +1,24 @@
+:javascript
+ function clickForTweet () {
+ $.ajax({
+ url: "https://api.twitter.com/1/statuses/user_timeline.json",
+ data: {
+ screen_name: "#{@config['twittername']}",
+ count: "200"
+ },
+ dataType: "jsonp",
+ success: function(json) {
+ var i = Math.floor(Math.random() * json.length);
+ var randomTweet = json[i].text;
+ var tweetUrl = "http://twitter.com/#{@config['twittername']}/status/" + json[i].id_str;
+ var linkedTweet = $("<a>").append(randomTweet).attr("href", tweetUrl).attr("target", "_blank").attr("id", "filler");
+ $("#filler").replaceWith(linkedTweet);
+ }
+ });
+ }
+
+ // onload, bind the various click handlers
+ function OnLoad() {
+ $("#generate-tweet").click(clickForTweet);
+ }
+ $(document).ready(function() { OnLoad(); });

0 comments on commit 76f7493

Please sign in to comment.