Skip to content

Commit

Permalink
Model#sync to sync local data to remote server
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Aug 3, 2012
1 parent 41f5a36 commit 238d19b
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 18 deletions.
2 changes: 2 additions & 0 deletions lib/skype_archive.rb
Expand Up @@ -4,6 +4,8 @@
require "json"

module SkypeArchive
URL = "http://localhost"

autoload :Model, "skype_archive/model"
autoload :Message, "skype_archive/message"

Expand Down
4 changes: 4 additions & 0 deletions lib/skype_archive/message.rb
Expand Up @@ -8,6 +8,10 @@ def initialize(attributes={})
@attributes[:created_at] = Time.at(@timestamp)
end

def to_json(*opts)
@attributes.to_json(*opts)
end

def method_missing(method, *args, &block)
@attributes[method.to_sym] or super
end
Expand Down
49 changes: 49 additions & 0 deletions lib/skype_archive/model.rb
@@ -1,5 +1,9 @@
require "sqlite3"
require "sequel"
require "active_support/core_ext/time/calculations"
require "rest_client"
require "json"
require "base64"

module SkypeArchive
class Model
Expand All @@ -21,6 +25,51 @@ def search(text, options={})
query.all.map { |attrs| Message.new(attrs) }
end

def sync
start_time = Time.now.yesterday.at_beginning_of_day.to_i
end_time = Time.now.yesterday.at_midnight.to_i
sync_contacts
sync_conversations
sync_participants
sync_messages(start_time, end_time)
end

def sync_contacts
contacts = connection[:Contacts].select(:skypename, :displayname).all
RestClient.post "#{URL}/users", contacts.to_json, :content_type => :json, :accept => :json
contacts
end

def sync_conversations
@conversations = connection[:Conversations].select(:id, :identity, :displayname).all
RestClient.post "#{URL}/conversations", @conversations.to_json, :content_type => :json, :accept => :json
@conversations
end

def sync_participants
@conversations or sync_conversations
@conversations.each do |convo|
participants = connection[:Participants].filter(:convo_id => convo[:id]).select(:identity).all
unless participants.empty?
url = "#{URL}/conversations/#{Base64.encode64(convo[:identity])[0...-1]}/participants"
RestClient.post url, participants.to_json, :content_type => :json, :accept => :json
end
end
end

def sync_messages(start_time=0, end_time=Time.now.to_i)
@conversations or sync_conversations
@conversations.each do |convo|
messages = connection[:Messages].filter("convo_id = ? and timestamp >= ? and timestamp <= ?", convo[:id], start_time, end_time).
select(:author, :from_dispname, :remote_id, :body_xml, :timestamp).
all.map { |attrs| Message.new(attrs) }
unless messages.empty?
url = "#{URL}/conversations/#{Base64.encode64(convo[:identity])[0...-1]}/messages"
RestClient.post url, messages.to_json, :content_type => :json, :accept => :json
end
end
end

private
def connection
@connection ||= Sequel.sqlite(File.expand_path("~/Library/Application\ Support/Skype/#{account_name}/main.db"))
Expand Down
4 changes: 4 additions & 0 deletions skype_archive.gemspec
Expand Up @@ -17,7 +17,11 @@ Gem::Specification.new do |gem|

gem.add_dependency "sqlite3"
gem.add_dependency "sequel"
gem.add_dependency "activesupport"
gem.add_dependency "rest-client"
gem.add_dependency "json"

gem.add_development_dependency "rspec"
gem.add_development_dependency "mocha"
gem.add_development_dependency "webmock"
end
62 changes: 48 additions & 14 deletions spec/model/skype_archive_spec.rb
Expand Up @@ -7,24 +7,58 @@
Sequel.stubs(:sqlite).returns(connection)
end

it "should search messages" do
model.search("gree message").should have(3).items
end
context "#search" do
it "should search messages" do
model.search("gree message").should have(3).items
end

it "should search only text messages" do
model.search("self message").should have(1).item
end
it "should search only text messages" do
model.search("self message").should have(1).item
end

it "should search by user" do
model.search("message", :skypename => "gii.jason.lai").should have(2).items
end
it "should search by user" do
model.search("message", :skypename => "gii.jason.lai").should have(2).items
end

it "should search by timestamp" do
timestamp = Time.now.to_i - 2400
model.search("message", :timestamp => timestamp).should have(3).items
end

it "should search by timestamp" do
timestamp = Time.now.to_i - 2400
model.search("message", :timestamp => timestamp).should have(3).items
it "should search by conversation" do
model.search("message", :conversation => "flyerhzm").should have(1).item
end
end

it "should search by conversation" do
model.search("message", :conversation => "flyerhzm").should have(1).item
context "#sync" do
it "should sync_contacts" do
stub_request(:post, "#{SkypeArchive::URL}/users")
model.sync_contacts.should have(3).items
end

it "should sync_conversations" do
stub_request(:post, "#{SkypeArchive::URL}/conversations")
model.sync_conversations.should have(2).items
end

it "should sync_participants" do
stub_request(:post, "#{SkypeArchive::URL}/conversations")
stub_request(:post, "#{SkypeArchive::URL}/conversations/JGdpaS5qYXNvbi5sYWkvMTIzNDU2Nzg5/participants")
stub_request(:post, "#{SkypeArchive::URL}/conversations/Zmx5ZXJoem0=/participants")
model.sync_participants
end

it "should sync_message" do
stub_request(:post, "#{SkypeArchive::URL}/conversations")
stub_request(:post, "#{SkypeArchive::URL}/conversations/JGdpaS5qYXNvbi5sYWkvMTIzNDU2Nzg5/messages")
stub_request(:post, "#{SkypeArchive::URL}/conversations/Zmx5ZXJoem0=/messages")
model.sync_messages
end

it "should sync_message with start_time and end_time" do
stub_request(:post, "#{SkypeArchive::URL}/conversations")
stub_request(:post, "#{SkypeArchive::URL}/conversations/JGdpaS5qYXNvbi5sYWkvMTIzNDU2Nzg5/messages")
model.sync_messages(Time.now.to_i - 4000, Time.now.to_i - 1000)
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -1,6 +1,7 @@
require 'rspec'
require 'skype_archive'
require 'support/sqlite_seed'
require 'webmock/rspec'

DB = Sequel.sqlite

Expand Down
8 changes: 4 additions & 4 deletions spec/support/sqlite_seed.rb
Expand Up @@ -17,13 +17,13 @@ def seed_db
DB[:messages].insert(:convo_id => gree_conversation_id, :author => "gii.jason.lai", :from_dispname => "Jason Lai",
:remote_id => 1, :body_xml => "gree message 1", :type => 61, :timestamp => Time.now.to_i - 3600)
DB[:messages].insert(:convo_id => gree_conversation_id, :author => "gii.richard.huang", :from_dispname => "Richard Huang",
:remote_id => 1, :body_xml => "gree message 2", :type => 61, :timestamp => Time.now.to_i - 1800)
:remote_id => 2, :body_xml => "gree message 2", :type => 61, :timestamp => Time.now.to_i - 1800)
DB[:messages].insert(:convo_id => gree_conversation_id, :author => "gii.jason.lai", :from_dispname => "Jason Lai / (赖)",
:remote_id => 1, :body_xml => "gree message 3", :type => 61, :timestamp => Time.now.to_i)
:remote_id => 3, :body_xml => "gree message 3", :type => 61, :timestamp => Time.now.to_i)
DB[:messages].insert(:convo_id => myself_conversation_id, :author => "gii.richard.huang", :from_dispname => "Richard Huang",
:remote_id => 1, :body_xml => "self message 11", :type => 61, :timestamp => Time.now.to_i)
:remote_id => 4, :body_xml => "self message 11", :type => 61, :timestamp => Time.now.to_i)
DB[:messages].insert(:convo_id => myself_conversation_id, :author => "flyerhzm", :from_dispname => "Richard Huang",
:remote_id => 1, :body_xml => "
:remote_id => 5, :body_xml => "
<partlist alt=\"\">
<part identity=\"flyerhzm\">
<name>Ron Buell</name>
Expand Down

0 comments on commit 238d19b

Please sign in to comment.