Created
August 20, 2014 18:47
-
-
Save ldodds/20c7715b793423f3c8c4 to your computer and use it in GitHub Desktop.
Socrata facade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'sinatra/base' | |
require 'sinatra/content_for' | |
require 'sinatra/static_assets' | |
require 'sinatra/url_for' | |
require 'rack/linkeddata' | |
require 'json' | |
require 'rack/conneg' | |
require 'uri' | |
require 'dotenv' | |
require 'rest_client' | |
Dotenv.load | |
class Facade < Sinatra::Base | |
helpers do | |
include Sinatra::ContentFor | |
register Sinatra::StaticAssets | |
def dataset_link(id) | |
return "http://" + ENV["SOCRATA_DOMAIN"] + "/datasets/" + id | |
end | |
def dataset_endpoint(id) | |
return "http://" + ENV["SOCRATA_DOMAIN"] + "/resource/" + id | |
end | |
def download_link(id, format) | |
return "http://" + ENV["SOCRATA_DOMAIN"] + "/api/views/" + id + "/rows." + format + "?accessType=DOWNLOAD" | |
end | |
end | |
#Configure application level options | |
configure do |app| | |
set :static, true | |
set :views, File.dirname(__FILE__) + "/../site/views" | |
set :public_dir, File.dirname(__FILE__) + "/../site/public" | |
end | |
use(Rack::Conneg) { |conneg| | |
conneg.set :accept_all_extensions, false | |
conneg.set :fallback, :html | |
conneg.ignore_contents_of(File.join(File.dirname(__FILE__),'/../site/public')) | |
conneg.provide([:json, :ttl, :rdf]) | |
} | |
get "/" do | |
erb :index | |
end | |
get "/datasets/:id" do | |
@dataset = dataset(params[:id]) | |
@size = dataset_size(params[:id]) | |
erb :dataset | |
end | |
def dataset(id) | |
response = RestClient.get "#{ENV["SOCRATA_DOMAIN"]}/views/#{id}.json" | |
return JSON.parse( response.to_str ) | |
end | |
def dataset_size(id) | |
response = RestClient.get "#{ENV["SOCRATA_DOMAIN"]}/resource/#{id}.json", {:params => {"$select" => "count(*)"} } | |
result = JSON.parse( response.to_str ) | |
return result[0]["count"].to_i | |
end | |
not_found do | |
'Not Found' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
ruby "2.1.0" | |
gem 'sinatra' | |
gem 'json' | |
gem 'soda-ruby' | |
gem 'dotenv' | |
gem 'rest_client' | |
gem 'linkeddata' | |
gem 'rack-linkeddata' | |
gem 'sinatra-static-assets' | |
gem 'sinatra-contrib' | |
gem 'emk-sinatra-url-for' | |
gem 'rack-conneg' | |
gem 'rspec' | |
gem "simplecov-rcov" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment