Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.rvmrc
.bundle
.ruby-version
pkg
rdoc
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ gemspec
group :development, :test do
gem 'rake'
gem 'rdoc'
gem 'minitest'
end
14 changes: 9 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ GEM
httparty (0.8.1)
multi_json
multi_xml
json (1.7.3)
metaclass (0.0.1)
mocha (0.10.0)
json (1.8.6)
metaclass (0.0.4)
minitest (5.14.4)
mocha (1.1.0)
metaclass (~> 0.0.1)
multi_json (1.0.4)
multi_xml (0.4.1)
Expand All @@ -27,7 +28,10 @@ PLATFORMS
DEPENDENCIES
fakeweb (~> 1.3.0)
geckoboard-push!
httparty (~> 0.8.1)
mocha (~> 0.10.0)
minitest
mocha (~> 1.1.0)
rake
rdoc

BUNDLED WITH
1.17.3
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "rdoc/task"

require "rake/testtask"
Rake::TestTask.new do |t|
t.libs << "test"
t.libs << "minitest"
t.test_files = FileList["test/**/*_test.rb"]
t.verbose = true
end
Expand All @@ -30,7 +30,7 @@ spec = Gem::Specification.new do |s|
s.add_dependency "httparty", "~> 0.8.1"

s.add_development_dependency "fakeweb", "~> 1.3.0"
s.add_development_dependency "mocha", "~> 0.10.0"
s.add_development_dependency "mocha", "~> 1.1.0"
end

Gem::PackageTask.new(spec) do |pkg|
Expand Down
6 changes: 3 additions & 3 deletions geckoboard-push.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ Gem::Specification.new do |s|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<httparty>, ["~> 0.8.1"])
s.add_development_dependency(%q<fakeweb>, ["~> 1.3.0"])
s.add_development_dependency(%q<mocha>, ["~> 0.10.0"])
s.add_development_dependency(%q<mocha>, ["~> 1.1.0"])
else
s.add_dependency(%q<httparty>, ["~> 0.8.1"])
s.add_dependency(%q<fakeweb>, ["~> 1.3.0"])
s.add_dependency(%q<mocha>, ["~> 0.10.0"])
s.add_dependency(%q<mocha>, ["~> 1.1.0"])
end
else
s.add_dependency(%q<httparty>, ["~> 0.8.1"])
s.add_dependency(%q<fakeweb>, ["~> 1.3.0"])
s.add_dependency(%q<mocha>, ["~> 0.10.0"])
s.add_dependency(%q<mocha>, ["~> 1.1.0"])
end
end
17 changes: 8 additions & 9 deletions lib/geckoboard/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

module Geckoboard
class Push
class << self
# API configuration
attr_accessor :api_key
attr_accessor :api_version
end
# API configuration
attr_accessor :api_key
attr_accessor :api_version

# Custom error type for handling API errors
class Error < Exception; end
Expand All @@ -17,15 +15,16 @@ class Error < Exception; end
base_uri 'https://push.geckoboard.com'

# Initializes the push object for a specific widget
def initialize(widget_key)
def initialize(api_key, widget_key)
@api_key = api_key
@widget_key = widget_key
end

# Makes a call to Geckoboard to push data to the current widget
def push(data)
raise Geckoboard::Push::Error.new("Api key not configured.") if Geckoboard::Push.api_key.nil? || Geckoboard::Push.api_key.empty?
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}))
raise Geckoboard::Push::Error.new(result["error"]) unless result["success"]
raise Geckoboard::Push::Error.new("Api key not configured.") if @api_key.nil? || @api_key.empty?
result = JSON.parse(self.class.post("/#{@api_version || 'v1'}/send/#{@widget_key}", {:body => {:api_key => @api_key, :data => data}.to_json}).body)
raise Geckoboard::Push::Error.new(result["error"] || result["message"]) unless result["success"]
result["success"]
end

Expand Down
47 changes: 28 additions & 19 deletions test/geckoboard/push_test.rb
Original file line number Diff line number Diff line change
@@ -1,68 +1,77 @@
require 'test/unit'
require 'minitest/autorun'
gem 'fakeweb'
require 'fakeweb'
gem 'mocha'
require 'mocha'
require 'minitest/unit'
require 'mocha/mini_test'
require File.join(File.expand_path(File.dirname(__FILE__)), "..", "..", "lib", "geckoboard", "push")

class PushTest < Test::Unit::TestCase
class PushTest < MiniTest::Unit::TestCase
def setup
FakeWeb.allow_net_connect = false
Geckoboard::Push.api_key = "12345"
@push = Geckoboard::Push.new("54321")
@api_key = "12345"
@widget_key = "54321"
end

def test_with_no_api_key
Geckoboard::Push.api_key = nil
assert_raise Geckoboard::Push::Error do
@push.number_and_secondary_value(100, 10)
assert_raises Geckoboard::Push::Error do
Geckoboard::Push.new(nil, @widget_key).number_and_secondary_value(100, 10)
end
end

def test_invalid_key
Geckoboard::Push.api_key = "invalid"
response = Net::HTTPOK.new("1.1", 200, "OK")
response.instance_variable_set(:@body, '{"success":false,"error":"Api key has wrong format. "}')
response.instance_variable_set(:@body, '{"success":false,"error":"Api key has wrong format."}')
response.instance_variable_set(:@read, true)
Net::HTTP.any_instance.expects(:request).returns(response)
assert_raise Geckoboard::Push::Error do
@push.number_and_secondary_value(100, 10)
assert_raises(Geckoboard::Push::Error, "Api key has wrong format.") do
Geckoboard::Push.new("invalid", @widget_key).number_and_secondary_value(100, 10)
end
end

def test_invalid_api_key_with_new_error_location
response = Net::HTTPOK.new("1.1", 401, "Unauthorized")
response.instance_variable_set(:@body, '{"message":"Your API key is invalid"}')
response.instance_variable_set(:@read, true)
Net::HTTP.any_instance.expects(:request).returns(response)
assert_raises(Geckoboard::Push::Error, "Your API key is invalid") do
Geckoboard::Push.new("invalid", @widget_key).number_and_secondary_value(100, 10)
end
end

def test_number_and_secondary_value
expect_http_request({"api_key" => "12345", "data" => {"item" => [{"text" => "", "value" => 100}, {"text" => "", "value" => 10}]}}.to_json)
assert_equal true, @push.number_and_secondary_value(100, 10)
assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).number_and_secondary_value(100, 10)
end

def test_text
expect_http_request({"api_key" => "12345", "data" => {"item" => [{"text" => "Test1", "type" => 2}, {"text" => "Test2", "type" => 1}]}}.to_json)
assert_equal true, @push.text([{:type => :info, :text => "Test1"}, {:type => :alert, :text => "Test2"}])
assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).text([{:type => :info, :text => "Test1"}, {:type => :alert, :text => "Test2"}])
end

def test_rag
expect_http_request({"api_key" => "12345", "data" => {"item" => [{"value" => 1}, {"value" => 2}, {"value" => 3}]}}.to_json)
assert_equal true, @push.rag(1,2,3)
assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).rag(1,2,3)
end

def test_line
expect_http_request({"api_key" => "12345", "data" => {"item" => [1,2,3], "settings" => {"axisx" => "x axis", "axisy" => "y axis", "colour" => "ff9900"}}}.to_json)
assert_equal true, @push.line([1,2,3], "ff9900", "x axis", "y axis")
assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).line([1,2,3], "ff9900", "x axis", "y axis")
end

def test_pie
expect_http_request({"api_key" => "12345", "data" => {"item" => [{"value" => 1, "label" => "Test1", "colour" => "ff9900"}, {"value" => 2, "label" => "Test2", "colour" => "ff0099"}]}}.to_json)
assert_equal true, @push.pie([{:value => 1, :label => "Test1", :colour => "ff9900"}, {:value => 2, :label => "Test2", :colour => "ff0099"}])
assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).pie([{:value => 1, :label => "Test1", :colour => "ff9900"}, {:value => 2, :label => "Test2", :colour => "ff0099"}])
end

def test_geckometer
expect_http_request({"api_key" => "12345", "data" => {"item" => 100, "min" => {"value" => 50}, "max" => {"value" => 200}}}.to_json)
assert_equal true, @push.geckometer(100, 50, 200)
assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).geckometer(100, 50, 200)
end

def test_funnel
expect_http_request({"api_key" => "12345", "data" => {"item" => [{"value" => 5, "label" => "Test1"}, {"value" => 10, "label" => "Test2"}], "type" => "reverse", "percentage" => "hide"}}.to_json)
assert_equal true, @push.funnel([{:label => "Test1", :value => 5}, {:label => "Test2", :value => 10}], true, true)
assert_equal true, Geckoboard::Push.new(@api_key, @widget_key).funnel([{:label => "Test1", :value => 5}, {:label => "Test2", :value => 10}], true, true)
end

def expect_http_request(json)
Expand Down