This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some initial functionality on PrivatePub class
- Loading branch information
Showing
9 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*.swp | ||
**/*.swp | ||
*.gem | ||
Gemfile.lock | ||
.bundle |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--color |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rvm use 1.9.2@private_pub --create |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source "http://rubygems.org" | ||
|
||
gemspec |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'rubygems' | ||
require 'rake' | ||
require 'rspec/core/rake_task' | ||
|
||
desc "Run RSpec" | ||
RSpec::Core::RakeTask.new do |t| | ||
t.verbose = false | ||
end | ||
|
||
task :default => :spec |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
# TODO | ||
require "digest/sha1" | ||
require "net/http" | ||
|
||
class PrivatePub | ||
class << self | ||
def server=(server) | ||
@config[:server] = server | ||
end | ||
|
||
def server | ||
@config[:server] | ||
end | ||
|
||
def key_expiration=(key_expiration) | ||
@config[:key_expiration] = key_expiration | ||
end | ||
|
||
def key_expiration | ||
@config[:key_expiration] | ||
end | ||
|
||
def secret_token=(secret_token) | ||
@config[:secret_token] = secret_token | ||
end | ||
|
||
def secret_token | ||
@config[:secret_token] | ||
end | ||
|
||
def reset_config | ||
@config = { | ||
:server => "http://localhost:9292/faye", | ||
:key_expiration => 60 * 60, # one hour | ||
} | ||
end | ||
|
||
def subscription(options = {}) | ||
sub = {:timestamp => (Time.now.to_f * 1000).round}.merge(options) | ||
sub[:key] = Digest::SHA1.hexdigest([secret_token, sub[:channel], sub[:timestamp]].join) | ||
sub | ||
end | ||
|
||
def publish(data) | ||
Net::HTTP.post_form(URI.parse(PrivatePub.server), data) | ||
end | ||
end | ||
reset_config | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require "spec_helper" | ||
|
||
describe PrivatePub do | ||
before(:each) do | ||
PrivatePub.reset_config | ||
end | ||
|
||
it "has secret token, server, and key expiration settings" do | ||
PrivatePub.secret_token = "secret token" | ||
PrivatePub.secret_token.should == "secret token" | ||
PrivatePub.server = "http://localhost/" | ||
PrivatePub.server.should == "http://localhost/" | ||
PrivatePub.key_expiration = 1000 | ||
PrivatePub.key_expiration.should == 1000 | ||
end | ||
|
||
it "defaults server to localhost:9292/faye" do | ||
PrivatePub.server.should == "http://localhost:9292/faye" | ||
end | ||
|
||
it "defaults key_expiration to 1 hour" do | ||
PrivatePub.key_expiration.should == 60 * 60 | ||
end | ||
|
||
it "defaults subscription timestamp to current time in milliseconds" do | ||
time = Time.now | ||
Time.stub!(:now).and_return(time) | ||
PrivatePub.subscription[:timestamp].should == (time.to_f * 1000).round | ||
end | ||
|
||
it "includes channel and custom time in subscription" do | ||
subscription = PrivatePub.subscription(:timestamp => 123, :channel => "hello") | ||
subscription[:timestamp].should == 123 | ||
subscription[:channel].should == "hello" | ||
end | ||
|
||
it "does a sha1 digest of channel, timestamp, and secret token" do | ||
PrivatePub.secret_token = "token" | ||
subscription = PrivatePub.subscription(:timestamp => 123, :channel => "channel") | ||
subscription[:key].should == Digest::SHA1.hexdigest("tokenchannel123") | ||
end | ||
|
||
it "publishes to server using Net::HTTP" do | ||
Net::HTTP.should_receive(:post_form).with(URI.parse(PrivatePub.server), "hello world").and_return(:result) | ||
PrivatePub.publish("hello world").should == :result | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'rubygems' | ||
require 'bundler/setup' | ||
Bundler.require(:default) | ||
|
||
RSpec.configure do |config| | ||
end |