nogara-geckoboard-push 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,70 @@
1
+ = geckoboard-push
2
+
3
+ * http://github.com/kickcode/geckoboard-push
4
+
5
+ == DESCRIPTION:
6
+
7
+ This is a Ruby library allowing you to push updates to Geckoboard widgets.
8
+
9
+ == FEATURES:
10
+
11
+ Exposes a Geckoboard::Push class allowing updates for the following widget types:
12
+
13
+ * number and secondary value
14
+ * text
15
+ * RAG
16
+ * line chart
17
+ * pie chart
18
+ * geckometer
19
+ * funnel chart
20
+
21
+ == SYNOPSIS:
22
+
23
+ Simply add to your Gemfile as follows:
24
+
25
+ gem 'geckoboard-push'
26
+
27
+ Then run "bundle install" and once installed, you need to configure the API key for your Geckoboard account:
28
+
29
+ Geckoboard::Push.api_key = "my_api_key"
30
+
31
+ Once that's done, you can push updates to widgets as so:
32
+
33
+ Geckoboard::Push.new("my_widget_key").number_and_secondary_value(100, 10)
34
+
35
+ == REQUIREMENTS:
36
+
37
+ geckoboard-push depends upon httparty.
38
+
39
+ == INSTALL:
40
+
41
+ It's best to add it to the Gemfile for your app, but if needed you can install the gem manually:
42
+
43
+ gem install geckoboard-push
44
+
45
+ == CONTRIBUTORS:
46
+
47
+ Elliott Draper
48
+
49
+ == LICENSE:
50
+
51
+ Copyright 2011 Elliott Draper <el@kickcode.com>
52
+
53
+ Permission is hereby granted, free of charge, to any person obtaining
54
+ a copy of this software and associated documentation files (the
55
+ "Software"), to deal in the Software without restriction, including
56
+ without limitation the rights to use, copy, modify, merge, publish,
57
+ distribute, sublicense, and/or sell copies of the Software, and to
58
+ permit persons to whom the Software is furnished to do so, subject to
59
+ the following conditions:
60
+
61
+ The above copyright notice and this permission notice shall be
62
+ included in all copies or substantial portions of the Software.
63
+
64
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
65
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
66
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
67
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
68
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
69
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
70
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "geckoboard", "push")
@@ -0,0 +1,99 @@
1
+ gem 'httparty'
2
+ require 'httparty'
3
+ require 'json'
4
+
5
+ module Geckoboard
6
+ class Push
7
+ class << self
8
+ # API configuration
9
+ attr_accessor :api_key
10
+ attr_accessor :api_version
11
+ end
12
+
13
+ # Custom error type for handling API errors
14
+ class Error < Exception; end
15
+
16
+ include HTTParty
17
+ base_uri 'https://push.geckoboard.com'
18
+
19
+ # Initializes the push object for a specific widget
20
+ def initialize(widget_key)
21
+ @widget_key = widget_key
22
+ end
23
+
24
+ # Makes a call to Geckoboard to push data to the current widget
25
+ 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}))
28
+ raise Geckoboard::Push::Error.new(result["error"]) unless result["success"]
29
+ result["success"]
30
+ end
31
+
32
+ # Value and previous value should be numeric values
33
+ def number_and_secondary_value(value, previous_value, prefix = nil)
34
+ item = {:text => "", :value => value}
35
+ item.merge!({:prefix => prefix}) if prefix
36
+ self.push(:item => [item, {:text => "", :value => previous_value}])
37
+ end
38
+
39
+ # Items should be an array of hashes, each hash containing:
40
+ # - text
41
+ # - type (should be either :alert, or :info, optional)
42
+ def text(items)
43
+ data = items.collect do |item|
44
+ type = case item[:type]
45
+ when :alert
46
+ 1
47
+ when :info
48
+ 2
49
+ else
50
+ 0
51
+ end
52
+ {:text => item[:text], :type => type}
53
+ end
54
+ self.push(:item => data)
55
+ end
56
+
57
+ # Red, amber and green should be values
58
+ def rag(text_red, red, text_amber, amber, text_green, green)
59
+ self.push(:item => [{:value => red, :text => text_red}, {:value => amber, :text => text_amber}, {:value => green, :text => text_green}])
60
+ end
61
+
62
+ # Values should be an array of numeric values
63
+ # Colour, x_axis and y_axis are optional settings
64
+ def line(values, colour = nil, x_axis = nil, y_axis = nil)
65
+ self.push(:item => values, :settings => {:axisx => x_axis, :axisy => y_axis, :colour => colour})
66
+ end
67
+
68
+ # Items should be an array of hashes, each hash containing:
69
+ # - value (numeric value)
70
+ # - label (optional)
71
+ # - colour (optional)
72
+ def pie(items)
73
+ data = items.collect do |item|
74
+ {:value => item[:value], :label => item[:label], :colour => item[:colour]}
75
+ end
76
+ self.push(:item => data)
77
+ end
78
+
79
+ # Value, min and max should be numeric values
80
+ def geckometer(value, min, max)
81
+ self.push(:item => value, :min => {:value => min}, :max => {:value => max})
82
+ end
83
+
84
+ # Items should be an array of hashes, each hash containing:
85
+ # - value (numeric value)
86
+ # - label (optional)
87
+ # Reverse defaults to false, and when true flips the colours on the widget
88
+ # Hide percentage defaults to false, and when true hides the percentage value on the widget
89
+ def funnel(items, reverse = false, hide_percentage = false)
90
+ data = items.collect do |item|
91
+ {:value => item[:value], :label => item[:label]}
92
+ end
93
+ opts = {:item => data}
94
+ opts[:type] = "reverse" if reverse
95
+ opts[:percentage] = "hide" if hide_percentage
96
+ self.push(opts)
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,82 @@
1
+ require 'test/unit'
2
+ gem 'fakeweb'
3
+ require 'fakeweb'
4
+ gem 'mocha'
5
+ require 'mocha'
6
+ require File.join(File.expand_path(File.dirname(__FILE__)), "..", "..", "lib", "geckoboard", "push")
7
+
8
+ class PushTest < Test::Unit::TestCase
9
+ def setup
10
+ FakeWeb.allow_net_connect = false
11
+ Geckoboard::Push.api_key = "12345"
12
+ @push = Geckoboard::Push.new("54321")
13
+ end
14
+
15
+ def test_with_no_api_key
16
+ Geckoboard::Push.api_key = nil
17
+ assert_raise Geckoboard::Push::Error do
18
+ @push.number_and_secondary_value(100, 10)
19
+ end
20
+ end
21
+
22
+ def test_invalid_key
23
+ Geckoboard::Push.api_key = "invalid"
24
+ response = Net::HTTPOK.new("1.1", 200, "OK")
25
+ response.instance_variable_set(:@body, '{"success":false,"error":"Api key has wrong format. "}')
26
+ response.instance_variable_set(:@read, true)
27
+ Net::HTTP.any_instance.expects(:request).returns(response)
28
+ assert_raise Geckoboard::Push::Error do
29
+ @push.number_and_secondary_value(100, 10)
30
+ end
31
+ end
32
+
33
+ def test_number_and_secondary_value
34
+ 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)
36
+ end
37
+
38
+ def test_number_and_secondary_value_and_prefix
39
+ 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, '$')
41
+ end
42
+
43
+ def test_text
44
+ 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"}])
46
+ end
47
+
48
+ def test_rag
49
+ 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)
51
+ end
52
+
53
+ def test_line
54
+ 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")
56
+ end
57
+
58
+ def test_pie
59
+ 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"}])
61
+ end
62
+
63
+ def test_geckometer
64
+ 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)
66
+ end
67
+
68
+ def test_funnel
69
+ 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)
71
+ end
72
+
73
+ def expect_http_request(json)
74
+ response = Net::HTTPOK.new("1.1", 200, "OK")
75
+ response.instance_variable_set(:@body, '{"success":true}')
76
+ response.instance_variable_set(:@read, true)
77
+ Net::HTTP.any_instance.expects(:request).with do |request|
78
+ assert_equal json, request.body
79
+ request.path == "/v1/send/54321" && request.method == "POST" && request.body == json
80
+ end.returns(response)
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nogara-geckoboard-push
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Elliott Draper
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>'
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>'
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: fakeweb
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.3.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.3.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: mocha
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.10.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.0
62
+ description:
63
+ email: el@kickcode.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files:
67
+ - README.rdoc
68
+ files:
69
+ - README.rdoc
70
+ - test/geckoboard/push_test.rb
71
+ - lib/geckoboard/push.rb
72
+ - lib/geckoboard-push.rb
73
+ homepage: http://docs.geckoboard.com/api/push.html
74
+ licenses: []
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --main
78
+ - README.rdoc
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.25
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Ruby library for pushing widget updates to Geckoboard.
99
+ test_files: []