eshq 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,18 @@
1
1
  require "eshq/version"
2
2
  require "eshq/client"
3
+ require "eshq/configuration"
3
4
 
4
5
  module ESHQ
6
+ class << self
7
+ attr_accessor :configuration
8
+ end
9
+
5
10
  def self.client
6
- @@client ||= Client.new(*settings)
11
+ @@client ||= Client.new
7
12
  end
8
13
 
9
14
  def self.reset_client
15
+ self.configuration = nil
10
16
  @@client = nil
11
17
  end
12
18
 
@@ -18,10 +24,8 @@ module ESHQ
18
24
  client.post("/event", options)
19
25
  end
20
26
 
21
- private
22
- def self.settings
23
- ["ESHQ_URL", "ESHQ_KEY", "ESHQ_SECRET"].map { |v|
24
- ENV[v] || raise("Missing environment variable #{v}")
25
- }
27
+ def self.configure
28
+ self.configuration ||= Configuration.new
29
+ yield(configuration) if block_given?
26
30
  end
27
31
  end
@@ -5,12 +5,16 @@ require "multi_json"
5
5
 
6
6
  module ESHQ
7
7
  class Client
8
- attr_reader :url, :key, :secret
8
+ def initialize
9
+ ESHQ.configure
10
+ end
11
+
12
+ def config
13
+ ESHQ.configuration
14
+ end
9
15
 
10
- def initialize(url, api_key, api_secret)
11
- @url = url
12
- @key = api_key
13
- @secret = api_secret
16
+ def url
17
+ config.url
14
18
  end
15
19
 
16
20
  def post(path, params)
@@ -33,11 +37,11 @@ module ESHQ
33
37
 
34
38
  def credentials
35
39
  time = Time.now.to_i.to_s
36
- {:key => key, :timestamp => time, :token => token(time)}
40
+ {:key => config.key, :timestamp => time, :token => token(time)}
37
41
  end
38
42
 
39
43
  def token(time)
40
- Digest::SHA1.hexdigest([key, secret, time].join(":"))
44
+ Digest::SHA1.hexdigest([config.key, config.secret, time].join(":"))
41
45
  end
42
46
 
43
47
  def url_for(path)
@@ -0,0 +1,11 @@
1
+ module ESHQ
2
+ class Configuration
3
+ attr_accessor :url, :key, :secret
4
+
5
+ def initialize
6
+ @url = ENV["ESHQ_URL"] || 'https://app.eventsourcehq.com/'
7
+ @key = ENV["ESHQ_KEY"]
8
+ @secret = ENV["ESHQ_SECRET"]
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module ESHQ
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -4,13 +4,18 @@ require "timecop"
4
4
  require "digest/sha1"
5
5
  require "fakeweb"
6
6
 
7
- require "eshq/client"
7
+ require "eshq"
8
8
 
9
9
  class TestClient < Test::Unit::TestCase
10
10
  attr_reader :client
11
11
 
12
12
  def setup
13
- @client = ESHQ::Client.new("http://example.com", "key", "secret")
13
+ ESHQ.configure do |config|
14
+ config.url = 'http://example.com'
15
+ config.key = 'key'
16
+ config.secret = 'secret'
17
+ end
18
+ @client = ESHQ::Client.new
14
19
  end
15
20
 
16
21
  def test_post
@@ -0,0 +1,53 @@
1
+ require "test/unit"
2
+ require "mocha"
3
+
4
+ require "eshq"
5
+
6
+ class TestConfiguration < Test::Unit::TestCase
7
+ attr_reader :client
8
+
9
+ def setup
10
+ @client = ESHQ.client
11
+ end
12
+
13
+ def teardown
14
+ ESHQ.reset_client
15
+ end
16
+
17
+ def test_defaults
18
+ assert_equal "https://app.eventsourcehq.com/", ESHQ.configuration.url
19
+ end
20
+
21
+ def test_configure_url
22
+ ESHQ.configure do |config|
23
+ config.url = "http://example.com"
24
+ end
25
+ assert_equal "http://example.com", ESHQ.configuration.url
26
+ end
27
+
28
+ def test_configure_key
29
+ ESHQ.configure do |config|
30
+ config.key = "key"
31
+ end
32
+ assert_equal "key", ESHQ.configuration.key
33
+ end
34
+
35
+ def test_configure_secret
36
+ ESHQ.configure do |config|
37
+ config.secret = "secret"
38
+ end
39
+ assert_equal "secret", ESHQ.configuration.secret
40
+ end
41
+
42
+ def test_initializes_with_env_variables_when_available
43
+ ENV["ESHQ_URL"] = "http://fallback.example.com"
44
+ ENV["ESHQ_KEY"] = "fallback-key"
45
+ ENV["ESHQ_SECRET"] = "fallback-secret"
46
+ ESHQ.reset_client
47
+ ESHQ.client
48
+ assert_equal "http://fallback.example.com", ESHQ.configuration.url
49
+ assert_equal "fallback-key", ESHQ.configuration.key
50
+ assert_equal "fallback-secret", ESHQ.configuration.secret
51
+ end
52
+ end
53
+
@@ -13,9 +13,9 @@ class TestESHQ < Test::Unit::TestCase
13
13
 
14
14
  def test_client_instantiation
15
15
  client = ESHQ.client
16
- assert_equal "http://example.com", client.url
17
- assert_equal "key", client.key
18
- assert_equal "secret", client.secret
16
+ assert_equal "http://example.com", client.config.url
17
+ assert_equal "key", client.config.key
18
+ assert_equal "secret", client.config.secret
19
19
  end
20
20
 
21
21
  def test_caches_client
@@ -23,16 +23,6 @@ class TestESHQ < Test::Unit::TestCase
23
23
  2.times { ESHQ.client }
24
24
  end
25
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
26
  def test_open
37
27
  ESHQ.client.expects(:post).with("/socket", {:channel => "test"}).returns({"socket" => "12345"})
38
28
  assert_equal "12345", ESHQ.open(:channel => "test")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eshq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-30 00:00:00.000000000 Z
12
+ date: 2012-11-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mocha
@@ -89,8 +89,10 @@ files:
89
89
  - eshq.gemspec
90
90
  - lib/eshq.rb
91
91
  - lib/eshq/client.rb
92
+ - lib/eshq/configuration.rb
92
93
  - lib/eshq/version.rb
93
94
  - test/client_test.rb
95
+ - test/configuration_test.rb
94
96
  - test/eshq_test.rb
95
97
  homepage: ''
96
98
  licenses: []
@@ -118,4 +120,5 @@ specification_version: 3
118
120
  summary: EventSource HQ API Client
119
121
  test_files:
120
122
  - test/client_test.rb
123
+ - test/configuration_test.rb
121
124
  - test/eshq_test.rb