nogara-geckoboard-push 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,11 +4,9 @@ require 'json'
4
4
 
5
5
  module Geckoboard
6
6
  class Push
7
- class << self
8
- # API configuration
9
- attr_accessor :api_key
10
- attr_accessor :api_version
11
- end
7
+ # API configuration
8
+ attr_accessor :api_key
9
+ attr_accessor :api_version
12
10
 
13
11
  # Custom error type for handling API errors
14
12
  class Error < Exception; end
@@ -17,14 +15,15 @@ module Geckoboard
17
15
  base_uri 'https://push.geckoboard.com'
18
16
 
19
17
  # Initializes the push object for a specific widget
20
- def initialize(widget_key)
18
+ def initialize(api_key, widget_key)
19
+ @api_key = api_key
21
20
  @widget_key = widget_key
22
21
  end
23
22
 
24
23
  # Makes a call to Geckoboard to push data to the current widget
25
24
  def push(data)
26
- raise Geckoboard::Push::Error.new("Api key not configured.") if Geckoboard::Push.api_key.nil? || Geckoboard::Push.api_key.empty?
27
- result = JSON.parse(self.class.post("/#{Geckoboard::Push.api_version || 'v1'}/send/#{@widget_key}", {:body => {:api_key => Geckoboard::Push.api_key, :data => data}.to_json}))
25
+ raise Geckoboard::Push::Error.new("Api key not configured.") if @api_key.nil? || @api_key.empty?
26
+ result = JSON.parse(self.class.post("/#{@api_version || 'v1'}/send/#{@widget_key}", {:body => {:api_key => @api_key, :data => data}.to_json}))
28
27
  raise Geckoboard::Push::Error.new(result["error"]) unless result["success"]
29
28
  result["success"]
30
29
  end
@@ -8,66 +8,64 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "..", "..", "lib", "
8
8
  class PushTest < Test::Unit::TestCase
9
9
  def setup
10
10
  FakeWeb.allow_net_connect = false
11
- Geckoboard::Push.api_key = "12345"
12
- @push = Geckoboard::Push.new("54321")
11
+ @api_key = "12345"
12
+ @widget_key = "54321"
13
13
  end
14
14
 
15
15
  def test_with_no_api_key
16
- Geckoboard::Push.api_key = nil
17
16
  assert_raise Geckoboard::Push::Error do
18
- @push.number_and_secondary_value(100, 10)
17
+ Geckoboard::Push.new(nil, @widget_key).number_and_secondary_value(100, 10)
19
18
  end
20
19
  end
21
20
 
22
21
  def test_invalid_key
23
- Geckoboard::Push.api_key = "invalid"
24
22
  response = Net::HTTPOK.new("1.1", 200, "OK")
25
23
  response.instance_variable_set(:@body, '{"success":false,"error":"Api key has wrong format. "}')
26
24
  response.instance_variable_set(:@read, true)
27
25
  Net::HTTP.any_instance.expects(:request).returns(response)
28
26
  assert_raise Geckoboard::Push::Error do
29
- @push.number_and_secondary_value(100, 10)
27
+ Geckoboard::Push.new("invalid", @widget_key).number_and_secondary_value(100, 10)
30
28
  end
31
29
  end
32
30
 
33
31
  def test_number_and_secondary_value
34
32
  expect_http_request({"api_key" => "12345", "data" => {"item" => [{"text" => "", "value" => 100}, {"text" => "", "value" => 10}]}}.to_json)
35
- assert_equal true, @push.number_and_secondary_value(100, 10)
33
+ assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).number_and_secondary_value(100, 10)
36
34
  end
37
35
 
38
36
  def test_number_and_secondary_value_and_prefix
39
37
  expect_http_request({"api_key" => "12345", "data" => {"item" => [{"text" => "", "value" => 100, "prefix" => "$"}, {"text" => "", "value" => 10}]}}.to_json)
40
- assert_equal true, @push.number_and_secondary_value(100, 10, '$')
38
+ assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).number_and_secondary_value(100, 10, '$')
41
39
  end
42
40
 
43
41
  def test_text
44
42
  expect_http_request({"api_key" => "12345", "data" => {"item" => [{"text" => "Test1", "type" => 2}, {"text" => "Test2", "type" => 1}]}}.to_json)
45
- assert_equal true, @push.text([{:type => :info, :text => "Test1"}, {:type => :alert, :text => "Test2"}])
43
+ assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).text([{:type => :info, :text => "Test1"}, {:type => :alert, :text => "Test2"}])
46
44
  end
47
45
 
48
46
  def test_rag
49
47
  expect_http_request({"api_key" => "12345","data" => {"item" => [{"value" => 1,"text" => "red"},{"value" => 2,"text" => "amber"},{"value" => 3,"text" => "green"}]}}.to_json)
50
- assert_equal true, @push.rag('red', 1, 'amber', 2, 'green', 3)
48
+ assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).rag('red', 1, 'amber', 2, 'green', 3)
51
49
  end
52
50
 
53
51
  def test_line
54
52
  expect_http_request({"api_key" => "12345", "data" => {"item" => [1,2,3], "settings" => {"axisx" => "x axis", "axisy" => "y axis", "colour" => "ff9900"}}}.to_json)
55
- assert_equal true, @push.line([1,2,3], "ff9900", "x axis", "y axis")
53
+ assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).line([1,2,3], "ff9900", "x axis", "y axis")
56
54
  end
57
55
 
58
56
  def test_pie
59
57
  expect_http_request({"api_key" => "12345", "data" => {"item" => [{"value" => 1, "label" => "Test1", "colour" => "ff9900"}, {"value" => 2, "label" => "Test2", "colour" => "ff0099"}]}}.to_json)
60
- assert_equal true, @push.pie([{:value => 1, :label => "Test1", :colour => "ff9900"}, {:value => 2, :label => "Test2", :colour => "ff0099"}])
58
+ assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).pie([{:value => 1, :label => "Test1", :colour => "ff9900"}, {:value => 2, :label => "Test2", :colour => "ff0099"}])
61
59
  end
62
60
 
63
61
  def test_geckometer
64
62
  expect_http_request({"api_key" => "12345", "data" => {"item" => 100, "min" => {"value" => 50}, "max" => {"value" => 200}}}.to_json)
65
- assert_equal true, @push.geckometer(100, 50, 200)
63
+ assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).geckometer(100, 50, 200)
66
64
  end
67
65
 
68
66
  def test_funnel
69
67
  expect_http_request({"api_key" => "12345", "data" => {"item" => [{"value" => 5, "label" => "Test1"}, {"value" => 10, "label" => "Test2"}], "type" => "reverse", "percentage" => "hide"}}.to_json)
70
- assert_equal true, @push.funnel([{:label => "Test1", :value => 5}, {:label => "Test2", :value => 10}], true, true)
68
+ assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).funnel([{:label => "Test1", :value => 5}, {:label => "Test2", :value => 10}], true, true)
71
69
  end
72
70
 
73
71
  def expect_http_request(json)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nogara-geckoboard-push
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: