Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use ajax to unwatch

  • Loading branch information...
commit 028cc8c5e21c91cad78d81ad53314b48fb5a5479 1 parent 99616d8
@zhengjia authored
View
17 lib/unwatch.rb
@@ -32,8 +32,10 @@ def access_token
def send_request(uri)
begin
JSON.parse(access_token.get(uri))
- rescue OAuth2::HTTPError
+ # OAuth2::AccessDenied: the repository isn't watched by the user
+ rescue OAuth2::HTTPError, OAuth2::AccessDenied
session[:access_token] = nil
+ status 503
halt %(<p>#{$!}</p><p><a href="/auth/github">Retry</a></p>)
end
end
@@ -68,21 +70,25 @@ def has_access
get "/" do
if session[:access_token]
- redirect '/unwatch'
+ redirect '/list'
end
erb :index
end
- get '/unwatch' do
+ get '/list' do
has_access
get_init_data
erb :unwatch
end
- get '/unwatch/:username/:repo' do
+ post '/unwatch/:username/:repo' do
if params[:username] && params[:repo]
unwatch_repo(params[:username], params[:repo])
- redirect '/unwatch'
+ repo_str = "#{params[:username]}/#{params[:repo]}"
+ "Successfully unwatched #{repo_str}"
+ else
+ status 503
+ halt "Failed to unwatch #{repo_str}"
end
end
@@ -97,6 +103,7 @@ def has_access
if session[:access_token]
redirect '/'
else
+ status 401
halt %(<p>Please authorize on github.</p><p><a href="/auth/github">Retry</a></p>)
end
end
View
20 public/unwatch.js
@@ -1,3 +1,23 @@
$(document).ready(function(){
$("table.tablesorter").tablesorter();
+
+ $(".unwatchLink").click(function(){
+ var that = this
+ $.ajax({
+ type: "POST",
+ dataType: "text",
+ url: "/unwatch/" + $(this).data('username') + '/' + $(this).data('repo'),
+ statusCode: {
+ 200: function(msg){
+ $("#notice").html(msg);
+ $(that).parent().parent().hide();
+ },
+ 503: function(msg){
+ $("#notice").html("Error occured");
+ }
+ }
+ });
+ return false;
+ })
+
})
View
14 test/unwatch.rb
@@ -10,7 +10,7 @@ def test_index_unauthorized
def test_index_authorized
get '/', {}, 'rack.session' => { :access_token => 'dummy' }
assert_equal 302, last_response.status
- assert_equal "http://unwatch.heroku.com/unwatch", last_response.location
+ assert_equal "http://unwatch.heroku.com/list", last_response.location
end
def test_redirect_to_github_for_authentication
@@ -41,7 +41,7 @@ def test_redirect_uri
def test_github_callback_unauthorized
get '/auth/github/callback'
- assert_equal 200, last_response.status
+ assert_equal 401, last_response.status
assert_match "Retry", last_response.body
end
@@ -60,13 +60,13 @@ def get_token(code)
assert_equal "http://unwatch.heroku.com/", last_response.location
end
- def test_unwatch_unauthorized
- get '/unwatch'
+ def test_list_unauthorized
+ get '/list'
assert_equal 302, last_response.status
assert_equal "http://unwatch.heroku.com/", last_response.location
end
- def test_unwatch_authorized
+ def test_list_authorized
mock_app(Unwatch) do
helpers do
def get_init_data
@@ -75,7 +75,7 @@ def get_init_data
end
end
end
- get '/unwatch', {}, 'rack.session' => { :access_token => 'dummy' }
+ get '/list', {}, 'rack.session' => { :access_token => 'dummy' }
assert_equal 200, last_response.status
end
@@ -87,7 +87,7 @@ def unwatch_repo(username, repo)
end
end
get '/unwatch/zhengjia/unwatch'
- assert_equal 302, last_response.status
+ assert_equal 200, last_response.status
end
end
View
5 views/unwatch.erb
@@ -7,6 +7,7 @@
<script src="/unwatch.js"></script>
</head>
<body>
+ <div id="notice" style="width: 100%; background-color: #EAE9E4"></div>
<div>Username: <%= @username %></div>
<div>
<table class="tablesorter">
@@ -24,13 +25,13 @@
<tbody>
<% @watched["repositories"].each do |repository| %>
<tr>
- <td><%= repository["name"]%></td>
+ <td><a href='<%= "http://github.com/#{repository["owner"]}/#{repository["name"]}" %>'><%= repository["name"]%></td>
<td><%= repository["description"]%></td>
<td><%= repository["owner"]%></td>
<td><%= repository["watchers"]%></td>
<td><%= repository["forks"]%></td>
<td><%= Time.parse(repository["pushed_at"]).strftime("%m/%d/%Y") %></td>
- <td><a href=<%= "/unwatch/#{repository["owner"]}/#{repository['name']}" %>>Unwatch</td>
+ <td><a href='#' data-username='<%= repository["owner"]%>' data-repo='<%= repository["name"] %>' class="unwatchLink">Unwatch</td>
</tr>
<% end %>
</tbody>
Please sign in to comment.
Something went wrong with that request. Please try again.