Skip to content

Commit

Permalink
Merge branch 'release-0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Levia committed Jul 16, 2019
2 parents 3e7f724 + bfa9b6d commit a91f201
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist: trusty
language: ruby
before_install:
- sed -i 's/[email protected]:/https:\/\/github.com\//' .gitmodules
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
### 0.2.0

**Features**

* Add new endpoint for ACP countries (BIOPAMA) protected areas

**Bug fixes**

* Make sure Travis build passes by specifying 'trusty' distribution

### 0.1.1

**Add new fields to Protected Areas JSON**
Expand Down
14 changes: 14 additions & 0 deletions api/v3/protected_areas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ class API::V3::ProtectedAreas < Grape::API
@protected_areas = paginate(collection)
end

# == annotations
################
desc "Get ACP countries protected areas."
params { optional :with_geometry, default: false, type: Boolean }
# == body
#########
get :biopama, rabl: "v3/views/protected_areas" do
collection = ProtectedArea.biopama.with_pame_evaluations
collection = collection.without_geometry unless params[:with_geometry]

@with_geometry = params[:with_geometry]
@protected_areas = collection
end

# == annotations
################
desc "Get a protected area via its wdpa_id."
Expand Down
3 changes: 3 additions & 0 deletions models/protected_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class ProtectedArea < ActiveRecord::Base

delegate :jurisdiction, to: :designation, allow_nil: true

scope :biopama, -> { joins(:countries).where("countries.is_biopama IS TRUE").distinct }
scope :with_pame_evaluations, -> { joins(:pame_evaluations).where("pame_evaluations.id IS NOT NULL").distinct }

SEARCHES = {
country: -> (scope, value) { scope.joins(:countries).where("countries.iso_3 = ?", value.upcase) },
marine: -> (scope, value) { scope.where(marine: value) },
Expand Down
22 changes: 22 additions & 0 deletions test/api/v3/protected_areas_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,26 @@ def test_get_protected_areas_search_wants_at_least_one_param
refute last_response.ok?
assert last_response.status == 400
end

def test_get_protected_areas_biopama_returns_only_acp_countries_areas
create(:protected_area, :with_pame_evaluation, name: "Mandalia Plains")
create(:protected_area, :biopama_country, :with_pame_evaluation, name: "Darjeeling")
create(:protected_area, :biopama_country, :with_pame_evaluation, name: "Not Marine")

get_with_rabl "/v3/protected_areas/biopama"

assert last_response.ok?
assert_equal(2, @json_response["protected_areas"].size)
end

def test_get_protected_areas_biopama_returns_only_acp_countries_areas_with_pame_evaluations
create(:protected_area, :biopama_country, name: "Mandalia Plains")
create(:protected_area, :biopama_country, :with_pame_evaluation, name: "Darjeeling")
create(:protected_area, :biopama_country, :with_pame_evaluation, name: "Not Marine")

get_with_rabl "/v3/protected_areas/biopama"

assert last_response.ok?
assert_equal(2, @json_response["protected_areas"].size)
end
end
4 changes: 4 additions & 0 deletions test/factories/pame_evaluations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FactoryGirl.define do
factory :pame_evaluation do
end
end
12 changes: 12 additions & 0 deletions test/factories/protected_areas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,17 @@
association :iucn_category, factory: :iucn_category, name: 'My IUCN category'
association :legal_status, factory: :legal_status, name: 'My legal status'
association :governance, factory: :governance, name: 'My governance'

trait :biopama_country do
after(:create) do |protected_area|
create(:country, protected_areas: [protected_area], is_biopama: true)
end
end

trait :with_pame_evaluation do
after(:create) do |protected_area|
create(:pame_evaluation, protected_area: protected_area)
end
end
end
end

0 comments on commit a91f201

Please sign in to comment.