Navigation Menu

Skip to content

Commit

Permalink
Добавлен faye gem. Сопутствующий рефакторинг
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehonoshin committed Jun 3, 2012
1 parent a9efbfd commit 26b6625
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -10,5 +10,6 @@ gem "twitter"
gem "bson_ext"
gem "will_paginate"

gem "faye"
gem "capistrano"
gem "unicorn"
18 changes: 18 additions & 0 deletions Gemfile.lock
Expand Up @@ -7,6 +7,7 @@ GEM
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
addressable (2.2.8)
bson (1.6.2)
bson_ext (1.6.2)
bson (~> 1.6.2)
Expand All @@ -17,10 +18,25 @@ GEM
net-sftp (>= 2.0.0)
net-ssh (>= 2.0.14)
net-ssh-gateway (>= 1.1.0)
cookiejar (0.3.0)
daemons (1.1.8)
em-http-request (0.3.0)
addressable (>= 2.0.0)
escape_utils
eventmachine (>= 0.12.9)
escape_utils (0.2.4)
eventmachine (0.12.10)
faraday (0.8.0)
multipart-post (~> 1.1)
faye (0.8.2)
cookiejar (>= 0.3.0)
em-http-request (>= 0.3.0)
eventmachine (>= 0.12.0)
faye-websocket (>= 0.4.0)
rack (>= 1.0.0)
yajl-ruby (>= 1.0.0)
faye-websocket (0.4.5)
eventmachine (>= 0.12.0)
haml (3.1.4)
highline (1.6.11)
i18n (0.6.0)
Expand Down Expand Up @@ -68,13 +84,15 @@ GEM
rack
raindrops (~> 0.7)
will_paginate (3.0.3)
yajl-ruby (1.1.0)

PLATFORMS
ruby

DEPENDENCIES
bson_ext
capistrano
faye
haml
mongo_mapper
rack
Expand Down
4 changes: 4 additions & 0 deletions faye.ru
@@ -0,0 +1,4 @@
require 'faye'

notificator = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
notificator.listen(9292)
32 changes: 14 additions & 18 deletions twibot.rb → lib/twibot.rb
@@ -1,16 +1,8 @@
require "twitter"
require "mongo_mapper"
require "./models/feed"
require "./models/tweet"
require './config/db.rb'
require "./lib/logger"
require 'psych'
require "time"

class Twibot

def initialize
$api_calls = 0
@new_tweets = []
twitter_settings = Psych.load(File.open('config/settings/twitter.yml'))
Twitter.configure do |config|
config.consumer_key = twitter_settings['twitter']['consumer_key']
Expand All @@ -25,6 +17,7 @@ def check_for_new_tweets
Twitter.home_timeline.each do |tweet|
if Tweet.where(status_id: tweet.id.to_s).first.nil?
Tweet.create!(text: tweet.text, status_id: tweet.id.to_s, username: tweet.user.screen_name, created_at: tweet.created_at)
@new_tweets << tweet
new_tweets_counter += 1
end
end
Expand All @@ -36,14 +29,17 @@ def print_api_calls_num
"Total api calls: #{$api_calls}"
end

end
def notify_clients
@new_tweets.each do |new_tweet|
push_notifiction(new_tweet.to_json)
end
end

def push_notification(data)
message = {channel: "/tweets/new", data: data}
uri = URI.parse("http://localhost:9292/faye")
Net::HTTP.post_form(uri, :message => message.to_json)
end

bot = Twibot.new
l = CustomLogger.new
loop do
puts l.log("Checking for new tweets")
counter = bot.check_for_new_tweets
puts l.log("Found #{counter} new tweets")
puts l.log(bot.print_api_calls_num)
sleep(60)
end

8 changes: 8 additions & 0 deletions public/js/csf-twi.js
@@ -1,4 +1,12 @@
$(document).ready(function() {

var client = new Faye.Client('http://localhost:9292/faye');

// Subscribe to the public channel
var public_subscription = client.subscribe('/tweets/new', function(data) {
console.log(data);
});

$('.pagination').hide();

$('.tweets').infinitescroll({
Expand Down
21 changes: 21 additions & 0 deletions runner.rb
@@ -0,0 +1,21 @@
require "twitter"
require "mongo_mapper"
require "./models/feed"
require "./models/tweet"
require './config/db.rb'
require "./lib/logger"
require "./lib/twibot"
require 'psych'
require "time"
require "net/http"


bot = Twibot.new
l = CustomLogger.new
loop do
puts l.log("Checking for new tweets")
counter = bot.check_for_new_tweets
puts l.log("Found #{counter} new tweets")
puts l.log(bot.print_api_calls_num)
sleep(60)
end
1 change: 1 addition & 0 deletions views/layout.haml
Expand Up @@ -14,6 +14,7 @@
%link{ rel: 'stylesheet', href: '/css/custom.css', type: "text/css" }
%script{ src: '/js/jquery-1.7.2.min.js', type: "text/javascript" }
%script{ src: '/js/jquery.infinitescroll.js', type: "text/javascript" }
%script{ src: 'http://localhost:9292/faye.js', type: "text/javascript" }
%script{ src: '/js/csf-twi.js', type: "text/javascript" }
%body
Expand Down

0 comments on commit 26b6625

Please sign in to comment.