Permalink
Browse files

Load config from a file, allow multiple text widgets with multiple pages

  • Loading branch information...
1 parent 295a932 commit deaf7f6e41fd14ba31e47cbf9ab68a3b5b0a1526 @mozz100 committed Mar 20, 2012
Showing with 98 additions and 60 deletions.
  1. +27 −16 app.rb
  2. +11 −0 config.yml
  3. +60 −44 views/index.erb
View
43 app.rb
@@ -3,35 +3,46 @@
require 'json'
require 'geckoboard-push'
-enable :sessions
+class Hash
+ def symbolize_keys!
+ keys.each do |key|
+ self[(key.to_sym rescue key) || key] = delete(key)
+ end
+ self
+ end
+end
get '/' do
+ @config = YAML.load_file("config.yml")
+ @widgets = @config['widgets']
- if session[:data].nil? then
- session[:data] = {
- # if set, environment variable GECKOBOARD_API_KEY will be used and the user won't be asked for API key
- 'api_key' => ENV['GECKOBOARD_API_KEY']
- }
- end
+ # if set, environment variable GECKOBOARD_API_KEY will override settings.yml
+ @api_key = ENV['GECKOBOARD_API_KEY'] || @config['geckoboard_api_key']
erb :index
end
post '/push' do
+ params.symbolize_keys!
- # take the three parameters and push to Geckoboard
+ # take the parameters and push to Geckoboard
begin
- Geckoboard::Push.api_key = params['api_key']
- response = Geckoboard::Push.new(params['widget_key']).text [{:text => params['text']}]
- session[:message] = "Successfully updated widget!"
+ Geckoboard::Push.api_key = params[:api_key]
+
+ if params[:widget_type] == "text"
+ # symbolize keys and only send if there's actually a value
+ data = params[:widget_data].map(&:symbolize_keys!).select{|t| !t[:text].empty? }
+ raise 'No input data' if data.empty?
+ it_worked = Geckoboard::Push.new(params[:widget_key]).text(data)
+ else
+ raise "Not supporting '#{params[:widget_type]}' widgets yet."
+ end
+ msg = (it_worked == true ? "Successfully updated widget!" : "Something went wrong, sorry.")
rescue Exception => e
# oops, got an error. Try to display to the user
- # puts e
puts e.message
- session[:error] = "Sorry, an error occurred: '#{e.message}'"
+ msg = "Sorry, an error occurred: '#{e.message}'"
end
- session[:data] = {}.merge(params) # retain what was POSTed when redrawing the page.
- redirect '/'
-
+ return msg
end
View
@@ -0,0 +1,11 @@
+geckoboard_api_key: "put_your_api_key_here"
+widgets:
+
+ - type: text
+ key: "10151-4e40ad1e9e0d02525607767c505c7c24"
+ label: "Multi-text example"
+ num_pages: 3
+
+ - type: text
+ key: "10151-b0e68b44d222306de30ccbd18756310e"
+ label: "Single field"
View
@@ -1,56 +1,72 @@
<html>
<head>
- <link rel="stylesheet" href="/css/bootstrap.min.css">
- <style>
- /* Hmm, for some reason we have to set this here */
- input{
- height:28px;
- }
- </style>
+ <title>Geckoboard update</title>
+ <link rel="stylesheet" href="/css/bootstrap.min.css"></link>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
+ <script type="text/javascript">
+ $(function() {
+ $('form').submit(function(e) {
+ // intercept form submit and do by AJAX instead
+ frm = $(this);
+ frm.find('input:submit').attr({disabled: true, value: 'Updating...'});
+ $('#feedback').fadeOut('fast');
+ $.ajax({
+ context: frm,
+ complete: function() {
+ // reset ui to normal
+ $(this).find('input:submit').attr({disabled: null, value: 'Update'});
+ },
+ data: frm.serialize(),
+ error: function(jqXHR, textStatus, errorThrown) {
+ alert('An error occurred.\n' + errorThrown);
+ },
+ success: function(data) {
+ $('#feedback').hide().removeClass();
+ $('#feedback').text(data);
+ $('#feedback').addClass('alert alert-success').fadeIn();
+ },
+ type: 'POST',
+ url: '/push'
+ });
+ e.preventDefault();
+ });
+ })
+ </script>
+ <style>input {height: 28px;}</style>
</head>
<body>
<div class="container">
<section style="padding-top:60px">
<div class="page-header">
- <h1>Geckoboard Push API Tester</h1>
+ <h1>Geckoboard Update</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 %>
- <div class="alert alert-error"><%= session[:error] %></div>
- <% session.delete(:error)%>
- <% end %>
-
- <% if !session[:message].nil? then %>
- <div class="alert alert-success"><%= session[:message] %></div>
- <% session.delete(:message)%>
+ <div style="height: 40px">
+ <div id="feedback" class="alert" style="display: none;">
+ <!-- text and class gets set by jquery -->
+ </div>
+ </div>
+ <% @widgets.select{|w| w['type'] == 'text'}.each do |widget| %>
+ <form method="POST" action="/push" class="form-horizontal">
+ <label for="text"><%= widget['label'] %></label>
+ <span class="input">
+ <% (widget['num_pages'] || 1).times do |p| %>
+ <input
+ type="text"
+ id="text"
+ name="widget_data[][text]"
+ class="input-xlarge"
+ placeholder="<%= p == 0 ? "(text)" : "page #{p+1} (optional)" %>"
+ />
+ <% end %>
+ </span>
+ <input type="hidden" name="widget_type" value="text"/>
+ <input type="hidden" name="widget_key" value="<%= widget['key'] %>"/>
+ <input type="hidden" name="api_key" value="<%= @api_key %>"/>
+ <span class="actions">
+ <input type="submit" class="btn primary" value="Update">
+ </span>
+ </form>
<% 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" <%= 'style="display: none;"' unless session[:data]['api_key'].nil? %>>
- <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>

0 comments on commit deaf7f6

Please sign in to comment.