Permalink
Browse files

Get list of tables in the Dashboard from the API

  • Loading branch information...
1 parent b9db5c7 commit 0a5a5eb18e3c1dd48fd325a8ab0c01c5c079e459 @ferblape ferblape committed May 10, 2011
View
@@ -26,10 +26,10 @@ gem "yajl-ruby", :require => "yajl"
group :development do
gem "capistrano"
gem "capistrano-ext"
- gem "passenger"
end
group :test, :development do
+ gem "passenger"
gem "ruby-debug19", :require => "ruby-debug", :platforms => :mri_19
gem "mocha"
gem "steak"
View
@@ -58,12 +58,12 @@ GEM
xml-simple
xml-simple
builder (2.1.2)
- capistrano (2.5.21)
+ capistrano (2.6.0)
highline
net-scp (>= 1.0.0)
net-sftp (>= 2.0.0)
net-ssh (>= 2.0.14)
- net-ssh-gateway (>= 1.0.0)
+ net-ssh-gateway (>= 1.1.0)
capistrano-ext (1.2.1)
capistrano (>= 1.0.0)
capybara (0.4.1.2)
@@ -90,7 +90,7 @@ GEM
fastthread (1.0.7)
ffi (1.0.7)
rake (>= 0.8.7)
- google-spreadsheet-ruby (0.1.2)
+ google-spreadsheet-ruby (0.1.3)
nokogiri (>= 1.4.3.1)
oauth (>= 0.3.6)
highline (1.6.1)
@@ -103,7 +103,7 @@ GEM
rake (>= 0.8.1)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
- mail (2.2.17)
+ mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
@@ -116,7 +116,7 @@ GEM
net-sftp (2.0.5)
net-ssh (>= 2.0.9)
net-ssh (2.1.4)
- net-ssh-gateway (1.0.1)
+ net-ssh-gateway (1.1.0)
net-ssh (>= 1.99.1)
nofxx-georuby (1.9.2)
nokogiri (1.4.4)
@@ -143,8 +143,8 @@ GEM
activesupport (= 3.0.7)
bundler (~> 1.0)
railties (= 3.0.7)
- rails_warden (0.5.2)
- warden
+ rails_warden (0.5.5)
+ warden (>= 1.0.0)
railties (3.0.7)
actionpack (= 3.0.7)
activesupport (= 3.0.7)
@@ -159,15 +159,15 @@ GEM
redis-namespace (>= 0.10.0)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
- rgeo (0.2.8)
+ rgeo (0.2.9)
rgeo-geojson (0.2.1)
rgeo (>= 0.2.8)
roo (1.9.3)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
- rspec-core (2.5.1)
+ rspec-core (2.5.2)
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
@@ -188,36 +188,36 @@ GEM
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
rubyzip (0.9.4)
- selenium-webdriver (0.1.4)
+ selenium-webdriver (0.2.0)
childprocess (>= 0.1.7)
ffi (>= 1.0.7)
json_pure
rubyzip
- sequel (3.22.0)
+ sequel (3.23.0)
sequel_column_type_array (0.0.2)
- sinatra (1.2.3)
+ sinatra (1.2.6)
rack (~> 1.1)
- tilt (< 2.0, >= 1.2.2)
+ tilt (>= 1.2.2, < 2.0)
spreadsheet (0.6.5.4)
ruby-ole (>= 1.0)
steak (1.1.0)
rspec (>= 1.3)
thor (0.14.6)
- tilt (1.2.2)
+ tilt (1.3)
timecop (0.3.5)
treetop (1.4.9)
polyglot (>= 0.3.1)
- tzinfo (0.3.26)
+ tzinfo (0.3.27)
vegas (0.1.8)
rack (>= 1.0.0)
- warden (1.0.3)
- rack (>= 1.0.0)
+ warden (1.0.4)
+ rack (>= 1.0)
webrat (0.7.3)
nokogiri (>= 1.2.0)
rack (>= 1.0)
rack-test (>= 0.5.3)
xml-simple (1.0.15)
- xpath (0.1.3)
+ xpath (0.1.4)
nokogiri (~> 1.3)
yajl-ruby (0.8.2)
@@ -9,14 +9,28 @@ def index
current_page = params[:page].nil? ? 1 : params[:page].to_i
per_page = 10
unless params[:public]
- @tags = Tag.load_user_tags(current_user.id, :limit => 5)
- @tables = Table.filter(:user_id => current_user.id).order(:id).reverse.
- paginate(current_page, per_page, current_user.tables_count)
+ @tags = Tag.load_user_tags(current_user.id, :limit => 5)
+ resp = access_token.get("/v1/tables.json?page=#{current_page}&per_page=#{per_page}")
+ if resp.code.to_i == 200
+ @tables = JSON.parse(resp.body)
+ @pagination = {
+ :current_page => current_page,
+ :per_page => per_page,
+ :page_count => (current_user.tables_count.to_f / per_page).ceil,
+ :next_page => current_page < (current_user.tables_count.to_f / per_page).ceil ? current_page + 1 : nil,
+ :previous_page => current_page == 1 ? nil : current_page - 1,
+ :page_range => (1..(current_user.tables_count.to_f / per_page).ceil)
+ }
+ else
+ render_500 and return
+ end
else
- @tags = Tag.load_public_tags(current_user.id, :limit => 5)
- @tables = Table.filter(~{:user_id => current_user.id} & {:privacy => Table::PUBLIC}).order(:id).reverse.
- paginate(current_page, per_page)
- render :template => 'admin/tables/index_public' and return
+ render_404 and return
+ # TODO:
+ # @tags = Tag.load_public_tags(current_user.id, :limit => 5)
+ # @tables = Table.filter(~{:user_id => current_user.id} & {:privacy => Table::PUBLIC}).order(:id).reverse.
+ # paginate(current_page, per_page)
+ # render :template => 'admin/tables/index_public' and return
end
end
@@ -8,16 +8,20 @@ class Api::Json::TablesController < Api::ApplicationController
after_filter :record_query_threshold
def index
+ limit = params[:per_page].nil? || params[:per_page].to_i > 100 ? 100 : params[:per_page].to_i
+ offset = params[:page].nil? || params[:page].to_i < 0 ? 0 : limit*(params[:page].to_i - 1)
@tables = Table.fetch("select *, array_to_string(array(select tags.name from tags where tags.table_id = user_tables.id),',') as tags_names
from user_tables
- where user_tables.user_id = ? order by id DESC", current_user.id).all
+ where user_tables.user_id = ? order by id DESC
+ limit ? offset ?", current_user.id, limit, offset).all
render :json => @tables.map{ |table|
{
:id => table.id,
:name => table.name,
:privacy => table_privacy_text(table),
:tags => table[:tags_names],
- :schema => table.schema
+ :schema => table.schema,
+ :updated_at => table.updated_at
}
}.to_json,
:callback => params[:callback]
@@ -23,6 +23,32 @@ def self.ssl_allowed(*splat)
true
end
end
+
+ def access_token
+ return nil unless logged_in?
+ access_token = nil
+ consumer = OAuth::Consumer.new(current_user.client_application.key, current_user.client_application.secret, :site => APP_CONFIG[:api_host])
+ if !session[:access_token].blank? && !session[:access_token_secret].blank?
+ access_token = OAuth::AccessToken.new(consumer, session[:access_token], session[:access_token_secret])
+ end
+ unless access_token
+ request_token = consumer.get_request_token
+ uri = URI.parse(request_token.authorize_url)
+ http = Net::HTTP.new(uri.host, uri.port)
+ if Rails.env.production?
+ http.use_ssl = true
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ end
+ request = Net::HTTP::Post.new(uri.request_uri, {'authorize' => '1', 'oauth_token' => request_token.token})
+ res = http.request(request)
+ url = URI.parse(res.header['location'])
+ verifier = url.query.split('&').select{ |q| q =~ /^oauth_verifier/}.first.split('=')[1]
+ access_token = request_token.get_access_token(:oauth_verifier => verifier)
+ session[:access_token] = access_token.token
+ session[:access_token_secret] = access_token.secret
+ end
+ access_token
+ end
protected
@@ -40,6 +66,14 @@ def render_404
end
end
end
+
+ def render_500
+ respond_to do |format|
+ format.html do
+ render :file => "public/500.html", :status => 500, :layout => false
+ end
+ end
+ end
def render_api_endpoint
respond_to do |format|
@@ -71,7 +105,11 @@ def not_authorized
end
def table_privacy_text(table)
- table.private? ? 'PRIVATE' : 'PUBLIC'
+ if table.is_a?(Table)
+ table.private? ? 'PRIVATE' : 'PUBLIC'
+ elsif table.is_a?(Hash)
+ table["privacy"]
+ end
end
helper_method :table_privacy_text
@@ -31,9 +31,15 @@ def tag_width(count, min, max)
end
def paginate(collection)
- return if collection.empty? || collection.is_a?(Array)
- if collection.page_count > 1
- render(:partial => 'shared/paginate', :locals => {:collection => collection}).html_safe
+ return if collection.empty?
+ if collection.is_a?(Hash)
+ if collection[:page_count] > 1
+ render(:partial => 'shared/paginate', :locals => {:collection => collection}).html_safe
+ end
+ else
+ if collection.page_count > 1
+ render(:partial => 'shared/paginate', :locals => {:collection => collection}).html_safe
+ end
end
end
@@ -57,29 +57,29 @@
<h3><strong><%= pluralize(current_user.tables_count, 'table', 'tables') %></strong> in your account</h3>
</div>
<div class="middle_white_medium">
- <% if @tables.current_page_record_count == 0 %>
+ <% if @tables.empty? %>
<div class="no_tables">
<h5>Hey! You don't have any tables</h5>
<p>Why don't you start by <a href="#create_table" class="new_table">creating a new table</a>?</p>
</div>
<% else %>
<ul class="your_tables">
<% @tables.each_with_index do |table, i| %>
- <li<%= ' class="last"'.html_safe if i == @tables.current_page_record_count - 1%>>
+ <li<%= ' class="last"'.html_safe if i == @tables.size - 1%>>
<div class="meta_info">
<span class="info">
- <h4><%= link_to(table.name, table, :class=>'tableTitle') %> </h4>
+ <h4><%= link_to(table["name"], table_path(table["id"]), :class=>'tableTitle') %> </h4>
<p class="status <%= table_privacy_text(table).downcase %>"><%= table_privacy_text(table) %></p>
- <p class="delete"> | <a table-name="<%= table.name %>" href="#" class="delete">delete table</a></p>
- <p class="columns"><strong><%= pluralize(table.rows_counted,'row','rows') rescue '' %> / <%= table.schema.size rescue '' %> columns:</strong>
- <%= table.schema.map{ |e| e.first }[1...4].join(', ') %>...</p>
+ <p class="delete"> | <a table-name="<%= table["name"] %>" href="#" class="delete">delete table</a></p>
+ <p class="columns"><strong><%= pluralize(table["rows_counted"].to_i,'row','rows') rescue '' %> / <%= table["schema"].size rescue '' %> columns:</strong>
+ <%= table["schema"].map{ |e| e.first }[1...4].join(', ') %>...</p>
</span>
<span class="tags">
- <% unless table.updated_at.blank? %>
- <p class="last_op"><%= time_ago_in_words(table.updated_at) %> ago</p>
+ <% unless table["updated_at"].blank? %>
+ <p class="last_op"><%= time_ago_in_words(table["updated_at"]) %> ago</p>
<% end %>
- <% unless table.tags.blank? %>
- <% table.tags.split(',').each do |tag_name| %>
+ <% unless table["tags"].blank? %>
+ <% table["tags"].split(',').each do |tag_name| %>
<a href="#"><%= tag_name %></a>
<% end %>
<% end %>
@@ -91,7 +91,7 @@
<% end %>
</div>
<div class="bottom_white_medium"></div>
- <%= paginate(@tables) %>
+ <%= paginate(@pagination ) %>
</div>
<div class="right">
@@ -1,15 +1,15 @@
<div class="paginate">
- <% if collection.prev_page %>
- <%= link_to('Previous', params.merge({:page => collection.prev_page}), {:class => 'previous'}) %>
+ <% if collection[:previous_page] %>
+ <%= link_to('Previous', params.merge({:page => collection[:previous_page]}), {:class => 'previous'}) %>
<% end %>
<ul>
- <% collection.page_range.each do |i| %>
- <li<%= ' class="selected"'.html_safe if i == collection.current_page %>>
+ <% collection[:page_range].each do |i| %>
+ <li<%= ' class="selected"'.html_safe if i == collection[:current_page] %>>
<%= link_to(i, params.merge({:page => i})) %>
</li>
<% end %>
</ul>
- <% if collection.next_page %>
- <%= link_to('Next', params.merge({:page => collection.next_page}), {:class => 'next'}) %>
+ <% if collection[:next_page] %>
+ <%= link_to('Next', params.merge({:page => collection[:next_page]}), {:class => 'next'}) %>
<% end %>
</div>
View
@@ -56,3 +56,4 @@ class Application < Rails::Application
require 'cartodb/connection_pool'
require 'cartodb/queries_threshold'
$pool = CartoDB::ConnectionPool.new
+
@@ -22,15 +22,10 @@
config.before(:each) do
Rails.cache.clear
+ Capybara.reset_sessions!
end
config.after(:each) do
- case page.driver.class
- when Capybara::Driver::RackTest
- page.driver.rack_mock_session.clear_cookies
- when Capybara::Driver::Selenium
- page.driver.cleanup!
- end
Capybara.use_default_driver
end
end
@@ -34,6 +34,13 @@
r.body[1]['schema'].should == default_schema
r.body.map{ |t| t['name'] }.should_not include("another_table_3")
end
+
+ get_json api_tables_url(:page => 1, :per_page => 2)
+ parse_json(response) do |r|
+ r.status.should be_success
+ r.body.size.should == 2
+ end
+
end
scenario "Create a new table without schema" do
Oops, something went wrong.

0 comments on commit 0a5a5eb

Please sign in to comment.