Search code examples
rubygoogle-fusion-tables

How do I automatically add rows to a Fusion Table (espeically one with geographic/map info) using Ruby?


PriceOfWeed.com is a website where users can submit how much they pay for marijuana. If you click a state, you can see how many users have submitted their transactions, and the average prices paid.

Here's how I assume it works:

  1. User submits how much they paid using the form at the top of the site.
  2. That price is inserted into the site owners' Fusion Table.
  3. The Fusion Table map now reflects the updated info.

What I am trying to understand is how this process could work in Ruby -- specifically, step 2. I'd like to use Fusion Tables and Ruby to make a cool map with dynamically updated data, but I have yet to find simple instructions/examples on how to do it.

Does anyone know how I can use Ruby to automatically add rows to my own Fusion Table?


Solution

  • I have used the fusion_tables gem to use Google Fusion Tables for generating US State-wise & County-wise mapped information.

    It is pretty simple to work with. The README.md file explains the steps involved in working with the fusion tables.

    Here's how the overall structure works: 1. Create the base fusion table to store the required data. 2. Merge it with the publicly available fusion table containing the geographic info (State-wise/county-wise, etc.), and create a map view. 3. With the fusion_tables gem based ruby code, insert records into the base fusion table. The map gets updated with the new info.

    The code to insert records would be as follows:

    require 'fusion_tables'
    
    # Connect to service    
    @ft = GData::Client::FusionTables.new      
    @ft.clientlogin(username, password) # Make sure this user has read/write access to the Fusion Table.
    
    tables = @ft.show_tables
    required_table = tables.select{ |t| t.name == "Base_table" } # assuming the base table name is "Base_table"
    
    data = [ { "column_1" => <First Column 1 data>, "column_2" = <First column 2 data>...},
             { "column_1" => <Second Column 1 data>, "column_2" = <Second column 2 data>...},
             ...
           ]
    
     required_table.insert data