eshq 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in eshq.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/eshq.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "eshq/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "eshq"
7
+ s.version = ESHQ::VERSION
8
+ s.authors = ["Mathias Biilmann Christensen"]
9
+ s.email = ["mathiasch@eventsourcehq.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{EventSource HQ API Client}
12
+ s.description = ""
13
+
14
+ s.rubyforge_project = "eshq"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "mocha"
23
+ s.add_development_dependency "timecop"
24
+
25
+ s.add_runtime_dependency "json"
26
+ end
@@ -0,0 +1,36 @@
1
+ require "digest/sha1"
2
+ require "net/http"
3
+ require "uri"
4
+ require "json"
5
+
6
+ module ESHQ
7
+ class Client
8
+ attr_reader :url, :key, :secret
9
+
10
+ def initialize(url, api_key, api_secret)
11
+ @url = url
12
+ @key = api_key
13
+ @secret = api_secret
14
+ end
15
+
16
+ def post(path, params)
17
+ response = Net::HTTP.post_form(url_for(path), params.merge(credentials))
18
+ JSON.parse(response.body)
19
+ end
20
+
21
+ def credentials
22
+ time = Time.now.to_i.to_s
23
+ {:key => key, :timestamp => time, :token => token(time)}
24
+ end
25
+
26
+ def token(time)
27
+ Digest::SHA1.hexdigest([key, secret, time].join(":"))
28
+ end
29
+
30
+ def url_for(path)
31
+ URI.parse(url).tap do |url|
32
+ url.path = path
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module ESHQ
2
+ VERSION = "0.0.2"
3
+ end
data/lib/eshq.rb ADDED
@@ -0,0 +1,27 @@
1
+ require "eshq/version"
2
+ require "eshq/client"
3
+
4
+ module ESHQ
5
+ def self.client
6
+ @@client ||= Client.new(*settings)
7
+ end
8
+
9
+ def self.reset_client
10
+ @@client = nil
11
+ end
12
+
13
+ def self.open(options)
14
+ client.post("/socket", options)["socket"]
15
+ end
16
+
17
+ def self.send(options)
18
+ client.post("/event", options)
19
+ end
20
+
21
+ private
22
+ def self.settings
23
+ ["ESHQ_URL", "ESHQ_KEY", "ESHQ_SECRET"].map { |v|
24
+ ENV[v] || raise("Missing environment variable #{v}")
25
+ }
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ require "test/unit"
2
+ require "mocha"
3
+ require "timecop"
4
+ require "digest/sha1"
5
+ require "net/http"
6
+
7
+ require "eshq/client"
8
+
9
+ class TestClient < Test::Unit::TestCase
10
+ attr_reader :client
11
+
12
+ def setup
13
+ @client = ESHQ::Client.new("http://example.com", "key", "secret")
14
+ end
15
+
16
+ def test_post
17
+ Net::HTTP.expects(:post_form).with() { |uri, params|
18
+ uri.to_s == "http://example.com/socket" &&
19
+ params.keys.sort == [:key, :presence_id, :timestamp, :token]
20
+ }.returns(stub(:body => '{"socket": "12345"}'))
21
+ res = client.post("/socket", :presence_id => "Mathias")
22
+ assert_equal "12345", res["socket"]
23
+ end
24
+
25
+ def test_credentials
26
+ t = Time.now
27
+ hash = Digest::SHA1.hexdigest("key:secret:#{t.to_i}")
28
+ Timecop.freeze(t) do
29
+ assert_equal({:key => "key", :timestamp => t.to_i.to_s, :token => hash}, client.credentials)
30
+ end
31
+ end
32
+ end
data/test/eshq_test.rb ADDED
@@ -0,0 +1,45 @@
1
+ require "test/unit"
2
+ require "mocha"
3
+
4
+ require "eshq"
5
+
6
+ class TestESHQ < Test::Unit::TestCase
7
+ def setup
8
+ ESHQ.reset_client
9
+ ENV["ESHQ_URL"] = "http://example.com"
10
+ ENV["ESHQ_KEY"] = "key"
11
+ ENV["ESHQ_SECRET"] = "secret"
12
+ end
13
+
14
+ def test_client_instantiation
15
+ client = ESHQ.client
16
+ assert_equal "http://example.com", client.url
17
+ assert_equal "key", client.key
18
+ assert_equal "secret", client.secret
19
+ end
20
+
21
+ def test_caches_client
22
+ ESHQ::Client.expects(:new).once.returns(stub(:client))
23
+ 2.times { ESHQ.client }
24
+ end
25
+
26
+ def test_missing_settings
27
+ ENV["ESHQ_KEY"] = nil
28
+ begin
29
+ ESHQ.client
30
+ flunk "No exception raised"
31
+ rescue => e
32
+ assert_match /Missing environment variable/, e.to_s
33
+ end
34
+ end
35
+
36
+ def test_open
37
+ ESHQ.client.expects(:post).with("/socket", {:channel => "test"}).returns({"socket" => "12345"})
38
+ assert_equal "12345", ESHQ.open(:channel => "test")
39
+ end
40
+
41
+ def test_send
42
+ ESHQ.client.expects(:post).with("/event", :channel => "test", :data => "Testing", :type => "test-event")
43
+ ESHQ.send(:channel => "test", :data => "Testing", :type => "test-event")
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eshq
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Mathias Biilmann Christensen
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-09-26 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mocha
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: timecop
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :runtime
58
+ version_requirements: *id003
59
+ description: ""
60
+ email:
61
+ - mathiasch@eventsourcehq.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files: []
67
+
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - Rakefile
72
+ - eshq.gemspec
73
+ - lib/eshq.rb
74
+ - lib/eshq/client.rb
75
+ - lib/eshq/version.rb
76
+ - test/client_test.rb
77
+ - test/eshq_test.rb
78
+ has_rdoc: true
79
+ homepage: ""
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options: []
84
+
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ requirements: []
104
+
105
+ rubyforge_project: eshq
106
+ rubygems_version: 1.3.7
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: EventSource HQ API Client
110
+ test_files:
111
+ - test/client_test.rb
112
+ - test/eshq_test.rb