webhook_stopwatch_client 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # WebhookStopwatchClient
2
2
 
3
- TODO: Write a gem description
3
+ A client for the webhook stopwatch
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,14 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ ```ruby
22
+ client = WebhookStopwatch::Client.new("redis://127.0.0.1/5")
23
+ # OR
24
+ client = WebhookStopwatch::Client.new(Redis.new)
25
+
26
+ client.start(event_id, Time.now.to_f)
27
+ client.stop(event_id, Time.now.to_f)
28
+ ```
22
29
 
23
30
  ## Contributing
24
31
 
@@ -1,27 +1,43 @@
1
- require 'faraday'
1
+ require 'redis'
2
+ require 'multi_json'
2
3
 
3
4
  module WebhookStopwatch
4
5
  class Client
5
- attr_reader :base_url
6
+ attr_reader :redis
6
7
 
7
- def initialize(base_url)
8
- @base_url = base_url
8
+ def initialize(redis_or_url)
9
+ if redis_or_url.is_a?(String)
10
+ @redis = Redis.new(:url => redis_or_url)
11
+ else
12
+ @redis = redis_or_url
13
+ end
9
14
  end
10
15
 
11
16
  def start(message_id, timestamp)
12
- connection.post("/messages/#{message_id}/start", "timestamp" => timestamp).
13
- success?
17
+ publish_event("start", message_id, timestamp)
14
18
  end
15
19
 
16
20
  def stop(message_id, timestamp)
17
- connection.post("/messages/#{message_id}/stop", "timestamp" => timestamp).
18
- success?
21
+ publish_event("stop", message_id, timestamp)
19
22
  end
20
23
 
21
24
  private
22
25
 
23
- def connection
24
- Faraday.new(:url => base_url)
26
+ def publish_event(event, message_id, timestamp)
27
+ redis.publish(channel, encode_json({"message_id" => message_id,
28
+ "timestamp" => timestamp,
29
+ "event" => event}))
30
+ true
31
+ rescue Exception
32
+ return false
33
+ end
34
+
35
+ def channel
36
+ "webhook_stopwatch:events"
37
+ end
38
+
39
+ def encode_json(payload)
40
+ MultiJson.dump(payload)
25
41
  end
26
42
  end
27
43
  end
@@ -1,21 +1,34 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe WebhookStopwatch::Client do
4
- let(:base_url) { "http://0.0.0.0:9292" }
4
+ let(:redis_url) { "redis://127.0.0.1:6379/5" }
5
+ let(:redis) { mock("Redis", :publish => 1) }
5
6
 
6
- subject { WebhookStopwatch::Client.new(base_url) }
7
+ subject { WebhookStopwatch::Client.new(redis_url) }
7
8
 
8
9
  before(:each) do
9
- stub_request(:post, /#{base_url}/)
10
+ Redis.stub(:new).and_return(redis)
10
11
  end
11
12
 
12
13
  describe "#start" do
13
- it "POSTs to start" do
14
+ it "can take a redis connection as the initialize instead of url" do
15
+ Redis.should_not_receive(:new)
16
+ redis.should_receive(:publish).
17
+ with("webhook_stopwatch:events",
18
+ MultiJson.dump({"message_id" => 123, "timestamp" => 1000000, "event" => "start"}))
19
+ WebhookStopwatch::Client.new(redis).start(123, 1000000)
20
+ end
21
+
22
+ it "configures redis" do
23
+ Redis.should_receive(:new).with(:url => "redis://127.0.0.1:6379/5")
14
24
  subject.start(123, 1000000)
25
+ end
15
26
 
16
- a_request(:post, "http://0.0.0.0:9292/messages/123/start").
17
- with(:body => {"timestamp" => "1000000"}).
18
- should have_been_made
27
+ it "publishes a message to redis" do
28
+ redis.should_receive(:publish).
29
+ with("webhook_stopwatch:events",
30
+ MultiJson.dump({"message_id" => 123, "timestamp" => 1000000, "event" => "start"}))
31
+ subject.start(123, 1000000)
19
32
  end
20
33
 
21
34
  it "returns true" do
@@ -24,7 +37,7 @@ describe WebhookStopwatch::Client do
24
37
 
25
38
  context "all hell breaks loose" do
26
39
  before(:each) do
27
- stub_request(:post, /#{base_url}/).to_return(:status => 500)
40
+ redis.stub(:publish).and_raise("oh no")
28
41
  end
29
42
 
30
43
  it "returns false" do
@@ -34,12 +47,25 @@ describe WebhookStopwatch::Client do
34
47
  end
35
48
 
36
49
  describe "#stop" do
37
- it "POSTs to stop" do
38
- subject.stop(123, 1000000)
50
+ it "can take a redis connection as the initialize instead of url" do
51
+ Redis.should_not_receive(:new)
52
+ redis.should_receive(:publish).
53
+ with("webhook_stopwatch:events",
54
+ MultiJson.dump({"message_id" => 123, "timestamp" => 1000000, "event" => "stop"}))
55
+ WebhookStopwatch::Client.new(redis).stop(123, 1000000)
56
+ end
39
57
 
40
- a_request(:post, "http://0.0.0.0:9292/messages/123/stop").
41
- with(:body => {"timestamp" => "1000000"}).
42
- should have_been_made
58
+ it "configures redis" do
59
+ Redis.should_receive(:new).with(:url => "redis://127.0.0.1:6379/5")
60
+ subject.start(123, 1000000)
61
+ end
62
+
63
+ it "publishes a message to redis" do
64
+ redis.should_receive(:publish).
65
+ with("webhook_stopwatch:events",
66
+ MultiJson.dump({"message_id" => 123, "timestamp" => 1000000, "event" => "stop"}))
67
+
68
+ subject.stop(123, 1000000)
43
69
  end
44
70
 
45
71
  it "returns true" do
@@ -48,7 +74,7 @@ describe WebhookStopwatch::Client do
48
74
 
49
75
  context "all hell breaks loose" do
50
76
  before(:each) do
51
- stub_request(:post, /#{base_url}/).to_return(:status => 500)
77
+ redis.stub(:publish).and_raise("oh no")
52
78
  end
53
79
 
54
80
  it "returns false" do
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require "#{File.dirname(__FILE__)}/../lib/webhook_stopwatch_client"
2
- require 'webmock/rspec'
3
2
 
4
3
  RSpec.configure do |config|
5
4
  config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "webhook_stopwatch_client"
5
- spec.version = "0.0.1"
5
+ spec.version = "0.1.1"
6
6
  spec.authors = ["Donald Plummer", "Michael Xavier"]
7
7
  spec.email = ["donald@crystalcommerce.com", "xavier@crystalcommerce.com"]
8
8
  spec.description = %q{Client for sending events to webhook stopwatch}
@@ -16,10 +16,11 @@ Gem::Specification.new do |spec|
16
16
  spec.require_paths = ["lib"]
17
17
 
18
18
  spec.add_dependency "faraday"
19
+ spec.add_dependency "redis"
20
+ spec.add_dependency "multi_json"
19
21
 
20
22
  spec.add_development_dependency "bundler", "~> 1.3"
21
23
  spec.add_development_dependency "rake"
22
24
  spec.add_development_dependency "guard-rspec"
23
25
  spec.add_development_dependency "rspec"
24
- spec.add_development_dependency "webmock"
25
26
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: webhook_stopwatch_client
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Donald Plummer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-15 00:00:00.000000000 Z
13
+ date: 2013-03-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  prerelease: false
@@ -30,24 +30,24 @@ dependencies:
30
30
  version: '0'
31
31
  - !ruby/object:Gem::Dependency
32
32
  prerelease: false
33
- type: :development
34
- name: bundler
33
+ type: :runtime
34
+ name: redis
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  none: false
37
37
  requirements:
38
- - - ~>
38
+ - - ! '>='
39
39
  - !ruby/object:Gem::Version
40
- version: '1.3'
40
+ version: '0'
41
41
  requirement: !ruby/object:Gem::Requirement
42
42
  none: false
43
43
  requirements:
44
- - - ~>
44
+ - - ! '>='
45
45
  - !ruby/object:Gem::Version
46
- version: '1.3'
46
+ version: '0'
47
47
  - !ruby/object:Gem::Dependency
48
48
  prerelease: false
49
- type: :development
50
- name: rake
49
+ type: :runtime
50
+ name: multi_json
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  none: false
53
53
  requirements:
@@ -63,7 +63,23 @@ dependencies:
63
63
  - !ruby/object:Gem::Dependency
64
64
  prerelease: false
65
65
  type: :development
66
- name: guard-rspec
66
+ name: bundler
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ version: '1.3'
73
+ requirement: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: '1.3'
79
+ - !ruby/object:Gem::Dependency
80
+ prerelease: false
81
+ type: :development
82
+ name: rake
67
83
  version_requirements: !ruby/object:Gem::Requirement
68
84
  none: false
69
85
  requirements:
@@ -79,7 +95,7 @@ dependencies:
79
95
  - !ruby/object:Gem::Dependency
80
96
  prerelease: false
81
97
  type: :development
82
- name: rspec
98
+ name: guard-rspec
83
99
  version_requirements: !ruby/object:Gem::Requirement
84
100
  none: false
85
101
  requirements:
@@ -95,7 +111,7 @@ dependencies:
95
111
  - !ruby/object:Gem::Dependency
96
112
  prerelease: false
97
113
  type: :development
98
- name: webmock
114
+ name: rspec
99
115
  version_requirements: !ruby/object:Gem::Requirement
100
116
  none: false
101
117
  requirements: