Permalink
Browse files

Added polling JS implementation with jQuery

  • Loading branch information...
1 parent 8959257 commit e9d14040ff0a9b20aa71a840f2197df9ab6045e5 @kapowaz committed Jul 29, 2011
View
@@ -1,2 +1,3 @@
+.DS_Store
config/database.yml
tmp/
View
@@ -1,5 +1,6 @@
source "http://rubygems.org"
+gem "rest-client"
gem "sinatra"
gem "i18n"
gem "data_mapper", ">= 1.1.0"
View
@@ -69,7 +69,10 @@ GEM
fastercsv (1.5.4)
i18n (0.6.0)
json (1.4.6)
+ mime-types (1.16)
rack (1.3.2)
+ rest-client (1.6.3)
+ mime-types (>= 1.16)
sinatra (1.2.6)
rack (~> 1.1)
tilt (>= 1.2.2, < 2.0)
@@ -87,4 +90,5 @@ DEPENDENCIES
dm-mysql-adapter
dm-postgres-adapter
i18n
+ rest-client
sinatra
View
@@ -1,7 +1,6 @@
# encoding: utf-8
require 'rubygems'
-require 'net/http'
-require 'open-uri'
+require 'rest-client'
require 'sinatra'
require 'data_mapper'
require 'active_support/core_ext'
@@ -22,10 +21,10 @@ class Page
def perform
begin
- self.data = open(self.url).string
+ self.data = RestClient.get self.url
self.pending = false
self.save
- rescue
+ rescue => e
# failed to open the url ...
end
end
@@ -40,15 +39,28 @@ def perform
erb :index
end
-post "/url" do
+post "/" do
if params[:url].match /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
- @page[:page] = Page.create :url => params[:url]
+ page = Page.create :url => params[:url]
+ redirect "/pages/#{page.id}"
else
- @page[:error] = "Invalid URL"
+ @page[:requested] = params[:url]
+ @page[:error] = "That's not a valid URL."
end
erb :index
end
+get "/pages/:page_id.json" do |page_id|
+ content_type :json
+ Page.get(page_id.to_i).to_json
+end
+
+get "/pages/:page_id" do |page_id|
+ @page[:page] = Page.get(page_id.to_i)
+ @page[:requested] = @page[:page].url
+ erb :index
+end
+
configure :development do
DataMapper.setup :default, YAML.load(File.new("config/database.yml"))[:development]
end
View
No changes.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,36 @@
+$(document).ready(function() {
+
+ jQuery.fn.requestedPage = function requestedPage(){
+ var page = $(this);
+ var url = page.attr('data-page-url');
+ var page_id = page.attr('data-page-id');
+ var timer = null;
+ var pending = true;
+
+ var pollStatus = function pollStatus(){
+ if (pending) {
+ jQuery.ajax({
+ url: '/pages/' + page_id + '.json',
+ success: function(response, textStatus, jqXHR){
+ if (response.pending) {
+ timer = setTimeout(pollStatus, 5000);
+ } else {
+ pending = false;
+ clearTimeout(timer);
+ page.find('span.status').removeClass('pending').text('and this is what it contained:');
+
+ var pre = $('<pre></pre>').css({display: 'none'});
+ page.after(pre.text(response.data));
+ pre.slideDown('fast');
+ }
+ }
+ });
+ }
+ };
+
+ pollStatus();
+ };
+
+ $('p.requested').requestedPage();
+
+});
Binary file not shown.
@@ -6,20 +6,22 @@ body
width:940px;
margin:0 auto;
padding:50px 10px;
- background-color:black;
+ background-color:rgb(64,64,64);
color:white;
+ text-shadow:0 1px 0 black;
}
body h1
{
font-family:Helvetica Neue, sans-serif;
font-size:48px;
letter-spacing:-0.05em;
+ text-shadow:0 2px 0 black;
}
body form
{
- border-top:1px solid rgba(0,0,0,0.2);
+ border-top:1px solid rgba(255,255,255,0.2);
margin-top:50px;
padding-top:20px;
}
@@ -29,11 +31,55 @@ body form input[type=text]
{
width:850px;
border-radius:3px;
- border:2px solid white;
+ border:none;
font-size:12px;
- height:20px;
- line-height:20px;
+ height:22px;
+ line-height:22px;
+ vertical-align:middle;
+ -webkit-box-shadow:0 -1px 0 rgba(0,0,0,0.5);
}
-body form input[type=text]:focus { border-color:rgb(50,90,255); outline:none; }
-body form input[type=text].error { border-color:red; }
-body form input[type=submit] { width:80px; }
+body form input[type=text].error { border:1px solid red; }
+body form input[type=submit]
+{
+ display:inline-block;
+ width:80px;
+ height:24px;
+ text-transform:uppercase;
+ font-size:12px;
+ text-shadow:0 1px 0 rgba(255,255,255,0.4);
+ border:none;
+ border-radius:3px;
+ background-color:rgb(128,128,128);
+ color:black;
+ vertical-align:middle;
+ cursor:pointer;
+ -webkit-box-shadow:0 -1px 0 rgba(255,255,255,0.5), 0 1px 0 rgba(0,0,0,0.5);
+}
+body form input[type=submit]:hover { cursor:pointer; background-color:rgb(192,192,192); }
+
+body p.requested span.status.pending
+{
+ padding-right:20px;
+ background-image:url(/images/loading.gif);
+ background-repeat:no-repeat;
+ background-position:center right;
+}
+
+body pre
+{
+ width:940px;
+ overflow:auto;
+}
+
+#forkme
+{
+ position:absolute;
+ top:0;
+ right:0;
+ background-image: url('https://a248.e.akamai.net/assets.github.com/img/30f550e0d38ceb6ef5b81500c64d970b7fb0f028/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67');
+ display:block;
+ height:149px;
+ width:149px;
+ overflow:hidden;
+ text-indent:-999px;
+}
View
@@ -1,12 +1,16 @@
<h1><%= @page[:title] %></h1>
<p><%= @page[:subtitle] %></p>
-<form action="/url" method="post">
+<form action="/" method="post">
<label for="input_url">Enter a URL to go grab:</label>
- <input type="text" name="url" id="input_url">
+ <input type="text" name="url" id="input_url" value="<%= @page[:requested] if @page.key? :requested %>" <%= 'class="error"' if @page.key? :error %>>
<input type="submit" value="Grab">
</form>
<% if @page[:page] %>
-<p>You requested the page <pre><%= @page[:page].url %></pre>, <span class="status">and it's currently being grabbed for you.</span></p>
-<% end %>
+<p class="requested" data-page-id="<%= @page[:page].id %>" data-page-url="<%= @page[:page].url %>">You requested the page <strong><%= @page[:page].url %></strong>, <span class="status pending">and it's currently being grabbed for you.</span></p>
+<% elsif @page[:error] %>
+<p class="error"><%= @page[:error] %></p>
+<% end %>
+
+<a href="http://github.com/kapowaz/djmapper" title="Fork me on GitHub" id="forkme">Fork me on GitHub</a>
View
@@ -8,5 +8,7 @@
</head>
<body>
<%= yield %>
+ <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
+ <script type="text/javascript" src="/javascripts/application.js"></script>
</body>
</html>

0 comments on commit e9d1404

Please sign in to comment.