Browse files

use coffeescript

  • Loading branch information...
1 parent a4e1c5a commit 2f9bc662222f38c5bf93a5bc733b391775b503ec @heupel committed Dec 22, 2011
Showing with 114 additions and 109 deletions.
  1. +6 −0 Cakefile
  2. +0 −107 lib/pingdom.js
  3. +11 −2 package.json
  4. +97 −0 src/pingdom.coffee
View
6 Cakefile
@@ -0,0 +1,6 @@
+{exec} = require 'child_process'
+
+task 'build', 'Build project from src/*.coffee to lib/*.js', ->
+ exec 'coffee --compile --output lib/ src/', (err, stdout, stderr) ->
+ throw err if err
+ console.log stdout + stderr
View
107 lib/pingdom.js
@@ -1,107 +0,0 @@
-var http = require('http'),
- url = require('url'),
- request = require('request');
-
-// new Pingdom(apiKey, username, password[, version='2.0'])
-Pingdom = function(apiKey, username, password, version) {
- if ( !(this instanceof Pingdom) ) {
- return new Pingdom(apiKey, username, password, version);
- }
-
- this.apiKey = apiKey;
- this.username = username;
- this.password = password;
- this.checks = [];
-
- if (typeof version === 'undefined') version = '2.0';
-
- this.baseUrl = 'https://api.pingdom.com/api/' + version + '/';
-}
-
-Pingdom.toQueryString = function(options) {
- var qs = '';
- for (option in options) {
- qs += option + '=' + options[option] + '&';
- }
-
- return qs;
-}
-
-
-Pingdom.prototype = {
- getCurrentServerTime: function(dataCallback) {
- var requestUrl = this.baseUrl + 'servertime';
- this.apiCall(requestUrl, dataCallback);
- },
-
- // Memoized
- // client.getCheckList([force=false,] dataCallback(checks))
- getCheckList: function(force, dataCallback) {
- if (arguments.length == 1) {
- dataCallback = force;
- force = false;
- }
-
- if (this.checks.length > 0 && !force) return this.checks;
-
- var requestUrl = this.baseUrl + 'checks';
-
- this.apiCall(requestUrl, function(checks) {
- this.checks = checks;
- if (dataCallback) dataCallback(checks);
- });
- },
-
- getDetailedCheckInfo: function(checkId, dataCallback) {
- var requestUrl = this.baseUrl + 'checks/' + checkId;
- this.apiCall(requestUrl, dataCallback);
- },
-
- getSummaryAverage: function(checkId, options, dataCallback) {
- /*Name Description Type Req? Default
- from Start time of period. Format is UNIX timestamp Integer no 0
- to End time of period. Format is UNIX timestamp Integer no current time
- probes Filter to only use results from a list of probes. Format is a comma separated list of probe identifiers String no all probes
- includeuptime Include uptime information Boolean no false
- bycountry Split response times into country groups Boolean no false
- byprobe Split response times into probe groups Boolean no false */
-
- var requestUrl = this.baseUrl + 'summary.average/' + checkId + '/?' + Pingdom.toQueryString(options);
- this.apiCall(requestUrl, dataCallback);
- },
-
- getSummaryHoursOfDay: function(checkId, options, dataCallback) {
- /*Name Description
- from Start time of period. Format is UNIX timestamp Integer no One week earlier than "to"
- to End time of period. Format is UNIX timestamp Integer no Current time
- probes Filter to only use results from a list of probes. Format is a comma separated list of probe identifiers String no all probes
- uselocaltime If true, use the user's local time zone for results (from and to parameters should still be specified in UTC). If false, use UTC for results. Boolean no false */
- if (!dataCallback) { dataCallback = options; options = {}; }
-
- var requestUrl = this.baseUrl + 'summary.hoursofday/' + checkId + '/?' + Pingdom.toQueryString(options);
- this.apiCall(requestUrl, dataCallback);
- },
-
- apiCall: function(requestUrl, dataCallback) {
- if (!this.auth) { this.auth = 'Basic ' + new Buffer(this.username + ':' + this.password).toString('base64'); } // Memoize the auth header
-
- var options = {
- 'uri': url.parse(requestUrl),
- 'headers': { 'App-Key': this.apiKey, 'Authorization': this.auth }
- };
-
- var req = request(options, function(error, response, body) {
- if (!error && response.statusCode == 200) {
- dataCallback(JSON.parse(body));
- } else {
- console.log('Error: ' + error + '\nResponse: ' + response.body);
- }
- });
- }
-};
-
-function createClient(apiKey, username, password, version) {
- return new Pingdom(apiKey, username, password, version);
-}
-
-exports.createClient = createClient;
View
13 package.json
@@ -7,11 +7,20 @@
{ "name": "Tony Heupel", "email": "tonyheupel@gmail.com"},
{ "name": "Grant Goodale", "email": "<ggoodale@moreblinktag.com>"}
],
- "directories": {"lib": "./lib", "test": "./test"},
+ "directories": {"src": "./src", "lib": "./lib", "test": "./test"},
"dependencies": {
"request": ">=1.9.6"
},
- "engines": {"node": ">=0.4.0"},
+ "devDependencies": {
+ "coffee-script": ">=1.2.1-pre",
+ "vows": ">=0.6.0"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/jashkenas/coffee-script.git"
+ },
+ "engines": {"node": ">=0.6.0"},
"scripts": {
+ "test": "vows test/*.coffee --spec",
}
}
View
97 src/pingdom.coffee
@@ -0,0 +1,97 @@
+http = require 'http'
+url = require 'url'
+request = require 'request'
+
+# new Pingdom(apiKey, username, password[, version='2.0'])
+class Pingdom
+ constructor: (@apiKey, @username, @password, @version='2.0') ->
+ return new Pingdom(apiKey, username, password, version) unless this instanceof Pingdom
+
+ @checks = []
+ @baseUrl = "https://api.pingdom.com/api/#{@version}"
+
+
+ getCurrentServerTime: (dataCallback) ->
+ requestUrl = "#{@baseUrl}/servertime"
+ @apiCall requestUrl, dataCallback
+
+
+ # Memoized
+ # client.getCheckList([force=false,] dataCallback(checks))
+ getCheckList: (force, dataCallback) ->
+ if arguments.length == 1
+ dataCallback = force
+ force = false
+
+ return @checks unless force || @checks.length == 0
@heupel
Owner
heupel added a note Dec 24, 2011

Do we want to return the @checks member immediately or call the dataCallback? I see code near the bottom that doesn't call the callback unless it's set, but I just don't think that works quite right...there's no consistency or unsetting of dataCallback if force is passed in...what about when @checks.length == 0?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ requestUrl = "#{@baseUrl}/checks"
+
+ @apiCall requestUrl, (checks) ->
+ @checks = checks
+ dataCallback(checks) if dataCallback
+
+
+ getDetailedCheckInfo: (checkId, dataCallback) ->
+ requestUrl = "#{@baseUrl}/checks/#{checkId}"
+ @apiCall requestUrl, dataCallback
+
+
+ getSummaryAverage: (checkId, options, dataCallback) ->
+ # Name Description Type Req? Default
+ # from Start time of period. Format is UNIX timestamp Integer no 0
+ # to End time of period. Format is UNIX timestamp Integer no current time
+ # probes Filter to only use results from a list of probes. Format is a comma separated list of probe identifiers String no all probes
+ # includeuptime Include uptime information Boolean no false
+ # bycountry Split response times into country groups Boolean no false
+ # byprobe Split response times into probe groups Boolean no false
+
+ requestUrl = "#{@baseUrl}/summary.average/#{checkId}/?#{Pingdom.toQueryString(options)}"
+ @apiCall requestUrl, dataCallback
+
+
+ # getSummaryHoursOfDay(checkId[, options], dataCallback)
+ getSummaryHoursOfDay: (checkId, options={}, dataCallback) ->
+ # Name Description
+ # from Start time of period. Format is UNIX timestamp Integer no One week earlier than "to"
+ # to End time of period. Format is UNIX timestamp Integer no Current time
+ # probes Filter to only use results from a list of probes. Format is a comma separated list of probe identifiers String no all probes
+ # uselocaltime If true, use the user's local time zone for results (from and to parameters should still be specified in UTC). If false, use UTC for results. Boolean no false */
+ #if !dataCallback
+ # dataCallback = options
+ # options = {}
+
+ requestUrl = "#{@baseUrl}/summary.hoursofday/#{checkId}/?#{Pingdom.toQueryString(options)}"
+ @apiCall requestUrl, dataCallback
+
+
+ apiCall: (requestUrl, dataCallback) ->
+ @auth = 'Basic ' + new Buffer(@username + ':' + @password).toString('base64') unless @auth # Memoize the auth header
+
+ options =
+ uri: url.parse(requestUrl),
+ headers:
+ 'App-Key': @apiKey,
+ 'Authorization': @auth
+
+ req = request options, (error, response, body) ->
+ if !error && response.statusCode == 200
+ dataCallback(JSON.parse(body))
+ else
+ console.log "Error: #{error}\nResponse: #{response.body}"
+
+
+ @toQueryString: (options) ->
+ pairs = for key, value of options
+ "#{key}=#{value}"
+
+ pairs.join('&')
+
+
+
+
+createClient = (apiKey, username, password, version) ->
+ new Pingdom(apiKey, username, password, version)
+
+
+exports.createClient = createClient

0 comments on commit 2f9bc66

Please sign in to comment.