Permalink
Browse files

Added basic pull API support to widgets.

This can be invoked via the last_values/since/timespan methods
on the widgets base class, and uses the Ducksboard HTTP Pull
API.
  • Loading branch information...
1 parent 69a56dc commit 443e18b115b1e12326135cbf23b8516a024a6134 @jshiell jshiell committed Jun 25, 2012
Showing with 43 additions and 3 deletions.
  1. +11 −1 README.mdown
  2. +32 −2 lib/ducksboard/widget.rb
View
@@ -34,7 +34,7 @@ or in an initializer
### Gauge
- widget = DUcksboard::GRaph.new(1235)
+ widget = Ducksboard::Gauge.new(1235)
widget.value = 0.93
widget.save
@@ -71,4 +71,14 @@ or in an initializer
]
widget1.save
+### Pull API
+ # You can use the HTTP Pull API to retrieve historical data from any widget.
+ # It will be returned in a map as per the API (http://dev.ducksboard.com/apidoc/pull-api-http)
+
+ widget = Ducksboard::Gauge.new(1235)
+ data = widget.since(300)['data']
+ # or
+ data = widget.last_values(15)
+ # or
+ data = widget.timespan(:daily, "Europe/London")
View
@@ -2,7 +2,6 @@
module Ducksboard
class Widget
include ::HTTParty
- base_uri "https://push.ducksboard.com/values"
attr_accessor :id, :data, :type
@@ -29,7 +28,6 @@ def timestamp=(time)
def update(data=nil)
@data = data if data
- auth = {:username => ::Ducksboard.api_key, :password => "ducksboard-gem"}
self.class.post('/' + id.to_s,
:basic_auth => auth,
:body => @data.to_json)
@@ -46,5 +44,37 @@ def save
def valid?
true
end
+
+ def last_values(number_of_values=3)
+ pull("last?count=#{number_of_values}")
+ end
+
+ def since(seconds_ago=3600)
+ pull("since?seconds=#{seconds_ago.to_i}")
+ end
+
+ def timespan(timespan=:monthly, timezone="UTC")
+ pull("timespan?timespan=#{timespan.to_s}&timezone=#{timezone}")
+ end
+
+ private
+
+ PUSH_URI = "https://push.ducksboard.com/values"
+ PULL_URI = "https://pull.ducksboard.com/values"
+
+ def auth()
+ {:username => ::Ducksboard.api_key, :password => "ducksboard-gem"}
+ end
+
+ def pull(service_uri)
+ response = self.class.get("#{PULL_URI}/#{@id.to_s}/#{service_uri}",
+ :basic_auth => auth)
+
+ if response.code.to_i == 200
+ JSON.parse(response.body)
+ else
+ raise "Unexpected response code: #{response.code}; body: #{response.body}"
+ end
+ end
end
end

0 comments on commit 443e18b

Please sign in to comment.