Class: Hdo::StortingImporter::FusionTable
- Inherits:
-
Object
- Object
- Hdo::StortingImporter::FusionTable
- Defined in:
- lib/hdo/storting_importer/fusion_table.rb
Overview
Simple query public Fusion Tables (onle need API key for auth). If we need to write to the table programatically, see gist.github.com/3265043.
Instance Method Summary collapse
- #columns_for(table_id) ⇒ Object
- #fix_request_failure(&blk) ⇒ Object
-
#initialize(api_key) ⇒ FusionTable
constructor
A new instance of FusionTable.
- #query(sql, opts = {}) ⇒ Object
Constructor Details
#initialize(api_key) ⇒ FusionTable
Returns a new instance of FusionTable.
9 10 11 |
# File 'lib/hdo/storting_importer/fusion_table.rb', line 9 def initialize(api_key) @api_key = api_key end |
Instance Method Details
#columns_for(table_id) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/hdo/storting_importer/fusion_table.rb', line 35 def columns_for(table_id) resp = fix_request_failure do RestClient.get("https://www.googleapis.com/fusiontables/v1/tables/#{table_id}/columns", :params => {:key => @api_key}) end data = MultiJson.decode(resp) data['items'] end |
#fix_request_failure(&blk) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/hdo/storting_importer/fusion_table.rb', line 44 def fix_request_failure(&blk) yield rescue RestClient::RequestFailed => ex raise unless ex.respond_to?(:http_body) raise "#{ex.}: #{ex.http_body}" end |
#query(sql, opts = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hdo/storting_importer/fusion_table.rb', line 13 def query(sql, opts = {}) resp = fix_request_failure do RestClient.get("https://www.googleapis.com/fusiontables/v1/query", :params => {:sql => sql, :key => @api_key}) end data = MultiJson.decode(resp) if opts[:rows] data.fetch('rows') else cols = data.fetch('columns') rows = data.fetch('rows') rows.map do |row| res = {} cols.each_with_index { |col, idx| res[col] = row[idx] } res end end end |