Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Truncated history

  • Loading branch information...
commit e7c2761736bcbae9b0d5b3779493bb1618148f39 0 parents
Rob Hudson authored
Showing with 105 additions and 0 deletions.
  1. +3 −0  Gemfile
  2. +43 −0 app.rb
  3. +2 −0  config.ru
  4. +57 −0 views/index.erb
3  Gemfile
@@ -0,0 +1,3 @@
+source 'http://rubygems.org'
+gem 'sinatra'
+gem 'rest-client'
43 app.rb
@@ -0,0 +1,43 @@
+require 'rubygems'
+require 'sinatra'
+require 'json'
+require 'rest_client'
+
+enable :sessions
+
+get '/' do
+ if session[:data].nil? then
+ session[:data] = {}
+ end
+
+ erb :index
+end
+
+post '/push' do
+ # take the three parameters and push in the correct format to GB
+ url = "https://push.geckoboard.com/v1/send/#{params[:widget_key]}"
+
+ data = {:item => [{:text=>params[:text], :type => 0}]}
+ payload = {:api_key => params[:api_key], :data => data}
+
+ # fire off the request
+ begin
+ response = RestClient.post url, payload.to_json, :content_type => "application/json"
+ session[:message] = "Successfully updated widget!"
+
+ rescue RestClient::Exception => e
+ # oops, got an error. Try to parse it and display to the user
+ # puts e
+ puts "got an error"
+ puts e.message
+ body = JSON.parse(e.response.body)
+ session[:error] = body["error"]
+ session[:data] = {
+ :api_key => params[:api_key],
+ :text => params[:text],
+ :widget_key => params[:widget_key]
+ }
+ end
+
+ redirect '/'
+end
2  config.ru
@@ -0,0 +1,2 @@
+require './app'
+run Sinatra::Application
57 views/index.erb
@@ -0,0 +1,57 @@
+<html>
+<head>
+ <link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css">
+ <style>
+ /* Hmm, for some reason we have to set this here */
+ input{
+ height:28px;
+ }
+ </style>
+</head>
+<body>
+ <div class="container">
+ <section style="padding-top:60px">
+ <div class="page-header">
+ <h1>Geckoboard Push API Tester</h1>
+ </div>
+ <p>This is a simple tool to demonstrate and test the Geckoboard Push API. For more information about the Push API see the <a href="http://docs.geckoboard.com/api/push.html">documentation site</a>.</p>
+ <% if !session[:error].nil? then %>
+ <p style="color:red"><%= session[:error] %></p>
+ <% session.delete(:error)%>
+ <% end %>
+
+ <% if !session[:message].nil? then %>
+ <p><%= session[:message] %></p>
+ <% session.delete(:message)%>
+ <% end %>
+
+ <form method="POST" action="/push">
+ <fieldset>
+ <div class="clearfix">
+ <label for="text">Message</label>
+ <div class="input">
+ <textarea name="text" class="xlarge"><%= session[:data].fetch(:text, nil) %></textarea>
+ </div>
+ </div>
+ <div class="clearfix">
+ <label for="widget_key">Widget Key</label>
+ <div class="input">
+ <input type="text" name="widget_key" class="xlarge" size="30" value="<%= session[:data].fetch(:widget_key, nil) %>"/>
+ </div>
+ </div>
+ <div class="clearfix">
+ <label for="api_key">API Key</label>
+ <div class="input">
+ <input type="text" name="api_key" class="xlarge" size="30" value="<%= session[:data].fetch(:api_key, nil) %>"/>
+ </div>
+ </div>
+ <div class="actions">
+ <input type="submit" class="btn primary" value="Submit">
+ </div>
+ <% session.delete(:data)%>
+ </fieldset>
+ </form>
+ </div>
+ </div>
+</body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.