Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Rossi committed Jan 19, 2016
0 parents commit 7e55d29
Show file tree
Hide file tree
Showing 46 changed files with 808 additions and 0 deletions.
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0
24 changes: 24 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
source "https://rubygems.org"

# Frameworks
gem "grape", "~> 0.14.0"
gem "grape-rabl", "~> 0.4.1"
gem "grape-kaminari", "~> 0.1.8"
gem "sinatra", "~> 1.4.6"

# Database
gem "pg", "~> 0.18.4"
gem "activerecord", "~> 4.2.5"
gem "activerecord-postgis-adapter", "~> 3.1.3"

# Support
gem "rake", "~> 10.5.0"
gem "activesupport", "~> 4.2.5"

# Testing
gem "minitest"
gem "minitest-around", "~> 0.3.2"
gem "database_cleaner", "~> 1.5.1"
gem "factory_girl", "~> 4.0"
gem "rack-test", "~> 0.6.3"

142 changes: 142 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
GEM
remote: https://rubygems.org/
specs:
actionpack (4.2.5)
actionview (= 4.2.5)
activesupport (= 4.2.5)
rack (~> 1.6)
rack-test (~> 0.6.2)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (4.2.5)
activesupport (= 4.2.5)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activemodel (4.2.5)
activesupport (= 4.2.5)
builder (~> 3.1)
activerecord (4.2.5)
activemodel (= 4.2.5)
activesupport (= 4.2.5)
arel (~> 6.0)
activerecord-postgis-adapter (3.1.3)
activerecord (~> 4.2)
rgeo-activerecord (>= 4.0.4)
activesupport (4.2.5)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
axiom-types (0.1.1)
descendants_tracker (~> 0.0.4)
ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1)
builder (3.2.2)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
database_cleaner (1.5.1)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
equalizer (0.0.11)
erubis (2.7.0)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
grape (0.14.0)
activesupport
builder
hashie (>= 2.1.0)
multi_json (>= 1.3.2)
multi_xml (>= 0.5.2)
rack (>= 1.3.0)
rack-accept
rack-mount
virtus (>= 1.0.0)
grape-kaminari (0.1.8)
grape
kaminari
grape-rabl (0.4.1)
grape
i18n
rabl
tilt
hashie (3.4.3)
i18n (0.7.0)
ice_nine (0.11.1)
json (1.8.3)
kaminari (0.16.3)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mini_portile2 (2.0.0)
minitest (5.8.3)
minitest-around (0.3.2)
minitest (~> 5.0)
multi_json (1.11.2)
multi_xml (0.5.5)
nokogiri (1.6.7.1)
mini_portile2 (~> 2.0.0.rc2)
pg (0.18.4)
rabl (0.11.8)
activesupport (>= 2.3.14)
rack (1.6.4)
rack-accept (0.4.5)
rack (>= 0.4)
rack-mount (0.8.3)
rack (>= 1.0.0)
rack-protection (1.5.3)
rack
rack-test (0.6.3)
rack (>= 1.0)
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.7)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.2)
loofah (~> 2.0)
rake (10.5.0)
rgeo (0.5.2)
rgeo-activerecord (4.0.5)
activerecord (~> 4.2)
rgeo (~> 0.3)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
thread_safe (0.3.5)
tilt (2.0.2)
tzinfo (1.2.2)
thread_safe (~> 0.1)
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)

PLATFORMS
ruby

DEPENDENCIES
activerecord (~> 4.2.5)
activerecord-postgis-adapter (~> 3.1.3)
activesupport (~> 4.2.5)
database_cleaner (~> 1.5.1)
factory_girl (~> 4.0)
grape (~> 0.14.0)
grape-kaminari (~> 0.1.8)
grape-rabl (~> 0.4.1)
minitest
minitest-around (~> 0.3.2)
pg (~> 0.18.4)
rack-test (~> 0.6.3)
rake (~> 10.5.0)
sinatra (~> 1.4.6)

BUNDLED WITH
1.11.2
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs += ["test", "."]
t.pattern = "test/**/*_test.rb"
end

task :default => [:test]
15 changes: 15 additions & 0 deletions api/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module API; end
module API::V3; end

Dir["#{File.dirname(__FILE__)}/**/*.rb"].each {|f| require f}

module API
class Routes < Grape::API
format :json
formatter :json, Grape::Formatter::Rabl

version "v3"
mount API::V3::ProtectedAreas
mount API::V3::Countries
end
end
24 changes: 24 additions & 0 deletions api/v3/countries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'models/country'

class API::V3::Countries < Grape::API
include Grape::Kaminari

paginate per_page: 25, max_per_page: 50
params { optional :with_geometry, default: false, type: Boolean }
get "/countries", rabl: "v3/views/countries" do
collection = Country
collection = collection.without_geometry unless params[:with_geometry]

@with_geometry = params[:with_geometry]
@countries = paginate(collection)
end

params { optional :with_geometry, default: true, type: Boolean }
get "/countries/:iso_3", rabl: "v3/views/country" do
@with_geometry = params[:with_geometry]
@country = Country.find_by_iso_3(
params[:iso_3]
) or error!(:not_found, 404)
end
end

23 changes: 23 additions & 0 deletions api/v3/protected_areas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'models/protected_area'

class API::V3::ProtectedAreas < Grape::API
include Grape::Kaminari

paginate per_page: 25, max_per_page: 50
params { optional :with_geometry, default: false, type: Boolean }
get "/protected_areas", rabl: "v3/views/protected_areas" do
collection = ProtectedArea
collection = collection.without_geometry unless params[:with_geometry]

@with_geometry = params[:with_geometry]
@protected_areas = paginate(collection)
end

params { optional :with_geometry, default: true, type: Boolean }
get "/protected_areas/:wdpa_id", rabl: "v3/views/protected_area" do
@with_geometry = params[:with_geometry]
@protected_area = ProtectedArea.find_by_wdpa_id(
params[:wdpa_id]
) or error!(:not_found, 404)
end
end
43 changes: 43 additions & 0 deletions api/v3/search_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class API::V3::Search < Grape::API
def points
results = Search.search(
params[:q],
search_options(size: ProtectedArea.count)
).results

render json: results.with_coords
end

def by_point
dirty_query = """
SELECT p.id, p.wdpa_id, p.name, p.the_geom_latitude, p.the_geom_longitude
FROM protected_areas p
WHERE ST_DWithin(p.the_geom, ST_GeomFromText('POINT(? ?)',4326), 0.0000001)
LIMIT 1;
""".squish

query = ActiveRecord::Base.send(:sanitize_sql_array, [
dirty_query, params[:lon].to_f, params[:lat].to_f
])

results = db.execute(query)

render json: results
end

private

def db
ActiveRecord::Base.connection
end

def search_options extra_options
options = {filters: {'type' => 'protected_area'}.merge(filters || {})}
options[:page] = params[:page].to_i if params[:page].present?
options.merge(extra_options)
end

def filters
params.stringify_keys.slice(*Search::ALLOWED_FILTERS)
end
end
2 changes: 2 additions & 0 deletions api/v3/views/countries.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
collection @countries, root: :countries, object_root: false
extends "v3/views/country"
21 changes: 21 additions & 0 deletions api/v3/views/country.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
object @country

# Basic
attributes :name, :iso_3
attributes :iso_3 => :id

# Geometry
attribute :geojson, if: -> (_) { @with_geometry }

# Relations
child :country_statistic => :statistics do
attributes :pa_area, :percentage_cover_pas, :eez_area,
:ts_area, :pa_land_area, :pa_marine_area, :percentage_pa_land_cover,
:percentage_pa_eez_cover, :percentage_pa_ts_cover, :land_area, :percentage_pa_cover,
:pa_eez_area, :pa_ts_area, :percentage_pa_marine_cover, :marine_area,
:polygons_count, :points_count
end

child :region do
attributes :name, :iso
end
27 changes: 27 additions & 0 deletions api/v3/views/protected_area.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
object @protected_area

# Basic
attribute :wdpa_id => :id
attributes :name, :original_name, :wdpa_id

node :marine do |pa|
pa.marine == "t"
end

# Geometry
attribute :geojson, if: -> (_) { @with_geometry }

# Relations
child :countries, object_root: false do
attributes :name, :iso_3
attribute :iso_3 => :id
end
child :sublocations, object_root: false do
attributes :id, :english_name
end
child :iucn_category, object_root: false do
attributes :id, :name
end
child :designation, object_root: false do
attributes :id, :name
end
3 changes: 3 additions & 0 deletions api/v3/views/protected_areas.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
collection @protected_areas, root: :protected_areas, object_root: false

extends "v3/views/protected_area"
22 changes: 22 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}")

require 'sinatra'
require 'grape'
require 'grape-rabl'
require 'grape-kaminari'
require 'active_record'
require 'active_support'

require 'config/rabl'
require 'config/active_record'

Dir["#{File.dirname(__FILE__)}/models/**/*.rb"].each {|f| require f}

require 'api/routes'
require 'web/routes'

use Rack::Config do |env|
env['api.tilt.root'] = "#{File.dirname(__FILE__)}/api"
end

run Rack::Cascade.new [Web::Routes, API::Routes]
8 changes: 8 additions & 0 deletions config/active_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$environment = ENV["API_ENV"] || "development"
db_config = YAML.load_file("config/database.yml")[$environment]

ActiveRecord::Base.default_timezone = :utc
ActiveRecord::Base.establish_connection(db_config)

use ActiveRecord::ConnectionAdapters::ConnectionManagement

Loading

0 comments on commit 7e55d29

Please sign in to comment.