Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit with working list and create clients.

  • Loading branch information...
commit f1f53b65bf1b990997b7d37eb006b992eb0f1443 0 parents
@jnunemaker jnunemaker authored
6 .gitignore
@@ -0,0 +1,6 @@
+log/*
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
+specs.watchr
9 Gemfile
@@ -0,0 +1,9 @@
+source "http://rubygems.org"
+gemspec
+
+group(:test) do
+ gem 'rake', '~> 0.8.7'
+ gem 'rspec', '~> 2.3'
+ gem 'log_buddy', '~> 0.5.0'
+ gem 'fakeweb', '~> 1.3.0'
+end
7 Rakefile
@@ -0,0 +1,7 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new
+
+task :default => :spec
21 gauges.gemspec
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "gauges/version"
+
+Gem::Specification.new do |s|
+ s.name = "gauges"
+ s.version = Gauges::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ['John Nunemaker']
+ s.email = ['nunemaker@gmail.com']
+ s.homepage = ""
+ s.summary = %q{Simple access to Gaug.es API}
+ s.description = %q{Simple access to Gaug.es API}
+
+ s.add_dependency('httparty', '~> 0.7.8')
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
45 lib/gauges.rb
@@ -0,0 +1,45 @@
+require 'httparty'
+
+class Gauges
+ include HTTParty
+
+ base_uri 'api.gaug.es'
+
+ def initialize(options={})
+ @options = options
+ end
+
+ def email
+ @options[:email]
+ end
+
+ def password
+ @options[:password]
+ end
+
+ def basic_auth
+ {:username => email, :password => password}
+ end
+
+ def clients
+ get('/clients')
+ end
+
+ # :description => Any text describing what this client is or will be used for (ie: Campfire, GitHub, HipChat...)
+ def create_client(params={})
+ post('/clients', params)
+ end
+
+private
+ def get(path)
+ self.class.get(path, options)
+ end
+
+ def post(path, body={})
+ self.class.post(path, options(:body => body))
+ end
+
+ def options(hash={})
+ hash.merge(:basic_auth => basic_auth)
+ end
+end
3  lib/gauges/version.rb
@@ -0,0 +1,3 @@
+class Gauges
+ VERSION = '0.1.0'
+end
11 spec/fixtures/client_create
@@ -0,0 +1,11 @@
+HTTP/1.1 201 Created
+Date: Sun, 03 Jul 2011 15:38:28 GMT
+Server: Apache/2.2.14 (Ubuntu)
+X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.6
+X-Runtime: 0.133349
+Content-Length: 102
+Status: 201
+Connection: close
+Content-Type: application/json
+
+{"created_at":"2011-07-03T15:38:28Z","description":"HipChat","key":"asdf"}
12 spec/fixtures/clients
@@ -0,0 +1,12 @@
+HTTP/1.1 200 OK
+Date: Sun, 03 Jul 2011 15:39:39 GMT
+Server: Apache/2.2.14 (Ubuntu)
+X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.6
+X-Runtime: 0.147350
+Set-Cookie: rack.session=BAh7AA%3D%3D%0A--ec3ba6a2c83f2dc155420e85f25335320848a463; path=/; expires=Sun, 10-Jul-2011 15:39:39 GMT
+Content-Length: 109
+Status: 200
+Connection: close
+Content-Type: application/json
+
+[{"created_at":"2011-07-03T15:38:28Z","description":"HipChat","key":"asdf"}]
11 spec/fixtures/clients_http_auth_failure
@@ -0,0 +1,11 @@
+HTTP/1.1 401 Authorization Required
+Date: Sun, 03 Jul 2011 15:37:12 GMT
+Server: Apache/2.2.14 (Ubuntu)
+X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.6
+X-Runtime: 0.187227
+Content-Length: 53
+Status: 401
+Connection: close
+Content-Type: application/json
+
+{"status":"fail","message":"Authentication required"}
75 spec/gauges_spec.rb
@@ -0,0 +1,75 @@
+require 'helper'
+
+describe Gauges do
+ context "initializing with email and password" do
+ before do
+ @client = Gauges.new(:email => 'john@orderedlist.com', :password => 'foobar')
+ end
+
+ it "sets email" do
+ @client.email.should == 'john@orderedlist.com'
+ end
+
+ it "sets password" do
+ @client.password.should == 'foobar'
+ end
+ end
+
+ context "http auth failure" do
+ before do
+ stub_get('http://john%40orderedlist.com:foobar@api.gaug.es/clients', :clients_http_auth_failure)
+ @client = Gauges.new(:email => 'john@orderedlist.com', :password => 'foobar')
+ @response = @client.clients
+ end
+
+ it "returns status" do
+ @response['status'].should == 'fail'
+ end
+
+ it "returns message" do
+ @response['message'].should == 'Authentication required'
+ end
+
+ it "has correct status code" do
+ @response.code.should == 401
+ end
+
+ it "returns correct content type" do
+ @response.headers['content-type'].should == 'application/json'
+ end
+ end
+
+ describe "#clients" do
+ before do
+ stub_get('http://john%40orderedlist.com:foobar@api.gaug.es/clients', :clients)
+ @client = Gauges.new(:email => 'john@orderedlist.com', :password => 'foobar')
+ @response = @client.clients
+ end
+
+ it "returns an array of clients" do
+ @response.should be_instance_of(Array)
+ @response.size.should be(1)
+
+ client = @response.first
+ client['key'].should == 'asdf'
+ client['created_at'].should == Time.utc(2011, 7, 3, 15, 38, 28)
+ client['description'].should == 'HipChat'
+ end
+ end
+
+ describe "#create_client" do
+ before do
+ stub_post('http://john%40orderedlist.com:foobar@api.gaug.es/clients', :client_create)
+ @client = Gauges.new(:email => 'john@orderedlist.com', :password => 'foobar')
+ @response = @client.create_client(:description => 'HipChat')
+ end
+
+ it "returns created client" do
+ @response.should be_instance_of(Hash)
+
+ @response['key'].should == 'asdf'
+ @response['description'].should == 'HipChat'
+ @response['created_at'].should == Time.utc(2011, 7, 3, 15, 38, 28)
+ end
+ end
+end
43 spec/helper.rb
@@ -0,0 +1,43 @@
+$:.unshift(File.expand_path('../../lib', __FILE__))
+
+require 'gauges'
+
+require 'pathname'
+require 'logger'
+
+ProjectRootPath = Pathname(__FILE__).dirname.join('..').expand_path
+log_path = ProjectRootPath.join('log')
+log_path.mkpath
+
+require 'rubygems'
+require 'bundler'
+
+Bundler.require(:test)
+
+Logger.new(log_path.join('test.log')).tap do |log|
+ LogBuddy.init(:logger => log)
+end
+
+module FakeRequestHelpers
+ def fixture(name)
+ ProjectRootPath.join('spec', 'fixtures', name.to_s).read
+ end
+
+ def stub_get(url, name)
+ FakeWeb.register_uri(:get, url, :response => fixture(name))
+ end
+
+ def stub_post(url, name)
+ FakeWeb.register_uri(:post, url, :response => fixture(name))
+ end
+end
+
+Rspec.configure do |c|
+ c.include(FakeRequestHelpers)
+
+ c.before(:each) do
+ FakeWeb.clean_registry
+ end
+end
+
+FakeWeb.allow_net_connect = false
Please sign in to comment.
Something went wrong with that request. Please try again.