diff --git a/pants.rb b/pants.rb index 5c08bac..6e668f3 100644 --- a/pants.rb +++ b/pants.rb @@ -3,12 +3,6 @@ require 'twitter' require 'redis' -PANTS = "pants OR jeans OR slacks" - -ON = %w(on wearing wear wore worn in) -OFF = %w(off no down without out don't didn't won't) -FLAG = %w(should might had put get back) - REDIS_CONNECTION = redis = Redis.new(host: "localhost", port: 6379) helpers do @@ -16,47 +10,35 @@ def redis REDIS_CONNECTION end - def calculate(status) - parts = status.split(' ') - parts.delete_if {|x| x.downcase! ; (!ON.include? x and !OFF.include? x and !FLAG.include? x) } - - questionable = 0 - on = 0 - off = 0 - - parts.each do |x| - if FLAG.include? x - questionable += 1 - elsif ON.include? x - on -= 1 - elsif OFF.include? x - off += 1 + def calculate(statuses, term, positive, negative) + # get texts + tweets = statuses.map {|x| x.text} + # delete tweets containing both terms + pos = [] + neg = [] + + tweets.each do |tweet| + if tweet.include?(positive) && !tweet.include?(negative) + pos << tweet + elsif !tweet.include?(positive) && tweet.include?(negative) + neg << tweet + else + tweets.delete tweet end end - - id = key_log(status) - # save search terms to redis - redis.hset(:terms, id, parts.join(",")) - percent = ((on + off).to_f.abs / parts.size.to_f) * 100.0 - variation = (questionable.to_f / parts.size.to_f) * 100.0 + tc = tweets.count + nc = neg.count + pc = pos.count - percent = 0 if percent.nan? - variation = 100 if variation.nan? + # store this result in redis to be updated on future searches + tweets.each { |x| redis.sadd("tweets:#{term}", tweets) } + redis.set "total:#{term}:count", tc + redis.set "positive:#{term}:count", pc + redis.set "negative:#{term}:count", nc - redis.hset(:stats, id, [percent, variation].join(":")) - - "has a #{percent}% chance of pantslessness with a margin of #{variation}!" - end - - def key_log(status) - # incr counter - id = redis.incr :uniq_id - # save tweet for reference - redis.hset(:tweets, id, status) - return id + return tc.to_f, pc.to_f, nc.to_f end - end @@ -65,27 +47,19 @@ def key_log(status) end post '/search' do - search = Twitter::Search.new - search.containing(PANTS).from(params[:username]) + search = Twitter::Search.new.phrase(params[:term]).containing("#{params[:positive]} OR #{params[:negative]}").per_page(100) statuses = search.fetch - - if statuses.empty? - status = "currently has no data pertaining to pants." - key_log("No status found") - else - status = calculate(statuses.first.text) - end - @result = "User @#{params[:username]} #{status}" + @total, @positive, @negative = calculate(statuses, params[:term].downcase, params[:positive], params[:negative]) - @tweet = statuses.first.text unless statuses.empty? - @id = redis.get "uniq_id" + @pcent = (@positive/@total)*100.0 + @ncent = (@negative/@total)*100.0 erb :status end post '/evaluate' do id = params[:id] - redis.hset :evaluation, params[:id], params[:eval] + redis.hset :evaluation, params[:term], params[:eval] end diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index f3b3747..045231a 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -52,10 +52,6 @@ table, caption, tbody, tfoot, thead, tr, th, td { /* @end */ -* { - margin: 40px; -} - body { font-family: helvetica; background: @@ -66,6 +62,12 @@ body { color: #ebeaeb; } +h1 { font-size: 30pt; } +h2 { font-size: 20pt; } +h2 { font-size: 18pt; } + +div { padding: 20px; } + .content { height: auto; padding: 20px; @@ -73,6 +75,7 @@ body { width: 700px; margin-right: auto; margin-left: auto; + margin-top: 0; background-color: rgba(47,47,47,0.8); -moz-border-radius-bottomleft: 15px; -moz-border-radius-bottomright: 15px; @@ -108,6 +111,7 @@ body { border: 1px solid #c3c3c3; text-align: center; padding: 5px; + margin: 2px; } .result .data { @@ -129,3 +133,15 @@ t { color: #5253f7; } +.graph-positive, .graph-negative { + width:5px; + height:50px; + display:inline-block; + padding: 0; + margin: 0; +} +.graph-positive { background-color: #905C71; } +.graph-positive:hover { background-color: #cd86a5; } +.graph-negative { background-color: #6486A1; } +.graph-negative:hover { background-color: #92c2e9; } + diff --git a/views/layout.erb b/views/layout.erb index 114fd00..69f1d83 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -31,7 +31,7 @@
-
+

State of the Pants

diff --git a/views/pants.erb b/views/pants.erb index 87c2337..13cbd6b 100644 --- a/views/pants.erb +++ b/views/pants.erb @@ -1,12 +1,29 @@ diff --git a/views/status.erb b/views/status.erb index 300be48..f31962f 100644 --- a/views/status.erb +++ b/views/status.erb @@ -1,17 +1,29 @@
-
-

<%= @result %>

+ +

Based on <%= @total.to_i %> data-points, people give the following results regarding:

+ +
+

<%= params[:term] %>

+
+ + <%= '%.2f' % @pcent %>% <%= params[:positive] %> +
+ <% @pcent.to_i.times do %> +
+ <% end %>
- <% if @tweet %> -

- This is loosely* based on the following tweet: -

-

"<%= @tweet %>"

- <% end %> +
+ + <%= '%.2f' % @ncent %>% <%= params[:negative] %> +
+ <% @ncent.to_i.times do %> +
+ <% end %> +
-

Help us make us better

+

Help us make us better

Was this evaluation any good?
Yes @@ -19,15 +31,14 @@
-
+

- *Please keep in mind that this is in no way an accurate representation of whether or not - this individual actually is wearing pants. We are meerly estimating the likelihood of - the current pants situation. Please take all results with a grain of salt, two cents, and any other - measure that cancels all credibility of these results. + * + These stats are aggregated based on all searches for <%= params[:term] %>, therefore the results may be better or worse depending on how many searches + have been made on this term.