sau-ruby 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +23 -0
- data/README.md +68 -0
- data/lib/sau/region.rb +25 -0
- data/lib/sau/regions.rb +15 -0
- data/lib/sau/version.rb +3 -0
- data/lib/sau.rb +29 -0
- data/spec/sau/region_spec.rb +47 -0
- data/spec/sau/regions_spec.rb +20 -0
- data/spec/sau_spec.rb +42 -0
- data/spec/spec_helper.rb +9 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8ed9d8e9bb5ff133ec469fac6f5c876a333ada49
|
4
|
+
data.tar.gz: 0a7485b1a6c1ae69851c6818770eb040fc343fc5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1c37c1dbedd65c9f700f0d5c8d6bda18f11e9eb8d4f94ea482f0dd288b67c14e773b48bb269c9a6efe512e0b0cd56a72b030c3a816bfc7b1e5b3aa815631051e
|
7
|
+
data.tar.gz: feb7a2c119601cd7ff88d042022ba027b1b656207204d919c3ac9b59aef57ef84295140bec06074532bb23226cc451711198f2f31d86e078df83b324e8de5c91
|
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2015, Vulcan Inc.
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice, this
|
11
|
+
list of conditions and the following disclaimer in the documentation and/or
|
12
|
+
other materials provided with the distribution.
|
13
|
+
|
14
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
15
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
16
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
17
|
+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
18
|
+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
19
|
+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
20
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
21
|
+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
22
|
+
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
23
|
+
OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
## SeaAroundUs API Wrapper
|
2
|
+
Ruby wrapper for the [Sea Around Us API](https://github.com/SeaAroundUs/sau-web-mt).
|
3
|
+
|
4
|
+
|
5
|
+
### Installation
|
6
|
+
```bash
|
7
|
+
$ gem install sau-ruby
|
8
|
+
```
|
9
|
+
|
10
|
+
|
11
|
+
### Example usage
|
12
|
+
```ruby
|
13
|
+
# include the helper library
|
14
|
+
require 'sau-ruby'
|
15
|
+
|
16
|
+
# get list of EEZs
|
17
|
+
eezs = SAU.get_regions('eez')
|
18
|
+
|
19
|
+
# get details for a single EEZ
|
20
|
+
brazil = eezs.get_by_title('Brazil (mainland)')
|
21
|
+
brazil.get_detail
|
22
|
+
|
23
|
+
# alternative way without getting the region list
|
24
|
+
brazil = SAU.get_region_detail('eez', 76)
|
25
|
+
|
26
|
+
# using LMEs for the next example
|
27
|
+
lmes = SAU.get_regions('lme')
|
28
|
+
|
29
|
+
# sample set of parameters for catch data
|
30
|
+
catch_data_params = {
|
31
|
+
measure: 'tonnage',
|
32
|
+
dimension: 'taxon',
|
33
|
+
sciname: true,
|
34
|
+
limit: 10
|
35
|
+
}
|
36
|
+
|
37
|
+
# get catch data for a single LME
|
38
|
+
north_sea = lmes.get_by_title('North Sea')
|
39
|
+
north_sea.get_data(catch_data_params)
|
40
|
+
|
41
|
+
# alternative way without getting the region list
|
42
|
+
north_sea.get_region_data('lme', 22, catch_data_params)
|
43
|
+
```
|
44
|
+
|
45
|
+
|
46
|
+
### Available parameters
|
47
|
+
Regions:
|
48
|
+
* eez
|
49
|
+
* lme
|
50
|
+
* rfmo
|
51
|
+
* fishing-entity
|
52
|
+
|
53
|
+
Measures:
|
54
|
+
* tonnage
|
55
|
+
* value
|
56
|
+
|
57
|
+
Dimensions:
|
58
|
+
* taxon
|
59
|
+
* commercialgroup
|
60
|
+
* functionalgroup
|
61
|
+
* country
|
62
|
+
* sector
|
63
|
+
* catchtype
|
64
|
+
* reporting-status
|
65
|
+
|
66
|
+
Other catch data parameters:
|
67
|
+
* limit (integer)
|
68
|
+
* sciname (boolean)
|
data/lib/sau/region.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module SAU
|
2
|
+
class Region
|
3
|
+
def initialize(props)
|
4
|
+
@props = props
|
5
|
+
end
|
6
|
+
|
7
|
+
def get_detail
|
8
|
+
@props.merge!(SAU.call_api("#{region}/#{id}"))
|
9
|
+
self
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_data(params)
|
13
|
+
url = "#{region}/#{params[:measure]}/#{params[:dimension]}/?region_id=#{id}&"
|
14
|
+
url += params.select { |param| !%i(measure dimension).include?(param) }.map { |k,v| "#{k}=#{v}" }.join('&')
|
15
|
+
@props[:data] = SAU.call_api(url)
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing(meth, *args)
|
20
|
+
return @props[meth] if @props.include?(meth)
|
21
|
+
super(meth, args) unless meth.to_s =~ /=$/
|
22
|
+
@props[meth.to_s[0...-1].to_sym] = args.first
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/sau/regions.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module SAU
|
2
|
+
class Regions
|
3
|
+
attr_reader :regions
|
4
|
+
|
5
|
+
def initialize(regions)
|
6
|
+
@regions = regions
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(meth, *args)
|
10
|
+
super(meth, args) unless meth.to_s =~ /^get_by_(\w+)$/
|
11
|
+
value, prop = args.first, $1
|
12
|
+
@regions.find { |region| region.send(prop.to_sym) == value }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/sau/version.rb
ADDED
data/lib/sau.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.require(:default)
|
3
|
+
require_relative 'sau/regions'
|
4
|
+
require_relative 'sau/region'
|
5
|
+
|
6
|
+
module SAU
|
7
|
+
BASE_URL = 'http://api.seaaroundus.org/api/v1/'
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def get_regions(region_name)
|
11
|
+
regions = call_api("#{region_name}/?nospatial=true")
|
12
|
+
Regions.new(regions.map { |region| region[:region] = region_name; Region.new(region) })
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_region_detail(region_name, region_id)
|
16
|
+
Region.new(region: region_name, id: region_id).get_detail
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_region_data(region_name, region_id, params)
|
20
|
+
Region.new(region: region_name, id: region_id).get_data(params)
|
21
|
+
end
|
22
|
+
|
23
|
+
def call_api(url)
|
24
|
+
response = RestClient.get(BASE_URL + url)
|
25
|
+
raise 'Error: ' + response.to_s unless response.code == 200
|
26
|
+
JSON.parse(response.to_s, symbolize_names: true)[:data]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
module SAU
|
4
|
+
describe Region do
|
5
|
+
describe :get_detail do
|
6
|
+
it 'gets region details' do
|
7
|
+
region = Region.new({ region: TEST_REGION, id: TEST_REGION_ID, foo: 'baz' })
|
8
|
+
stub = stub_request(:get, "#{BASE_URL}#{TEST_REGION}/#{TEST_REGION_ID}").
|
9
|
+
to_return(body: { data: TEST_REGION_DETAIL }.to_json)
|
10
|
+
region = region.get_detail
|
11
|
+
expect(stub).to have_been_requested
|
12
|
+
expect(region.instance_variable_get(:@props)).to eq TEST_REGION_DETAIL.merge({ region: TEST_REGION })
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe :get_data do
|
17
|
+
it 'gets region data for a set of params' do
|
18
|
+
url = "#{BASE_URL}#{TEST_REGION}/#{TEST_DATA_PARAMS[:measure]}" +
|
19
|
+
"/#{TEST_DATA_PARAMS[:dimension]}/?region_id=#{TEST_REGION_ID}&" +
|
20
|
+
"limit=#{TEST_DATA_PARAMS[:limit]}&sciname=#{TEST_DATA_PARAMS[:sciname]}"
|
21
|
+
region = Region.new({ region: TEST_REGION, id: TEST_REGION_ID })
|
22
|
+
stub = stub_request(:get, url).
|
23
|
+
to_return(body: { data: 'testdata' }.to_json)
|
24
|
+
region = region.get_data(TEST_DATA_PARAMS)
|
25
|
+
expect(stub).to have_been_requested
|
26
|
+
expect(region.instance_variable_get(:@props)[:data]).to eq 'testdata'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe :method_missing do
|
31
|
+
let(:region) { Region.new(foo: 'bar') }
|
32
|
+
|
33
|
+
it 'returns a value if the property exists' do
|
34
|
+
expect(region.foo).to eq 'bar'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'assigns a new value if the method ends with =' do
|
38
|
+
region.foo = 'baz'
|
39
|
+
expect(region.foo).to eq 'baz'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'raises a NoMethodError error if the property does not exist' do
|
43
|
+
expect { region.bar }.to raise_error NoMethodError
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
module SAU
|
4
|
+
describe Regions do
|
5
|
+
describe :method_missing do
|
6
|
+
it 'returns regions by field if that field exists' do
|
7
|
+
test_region = Object.new
|
8
|
+
expect(test_region).to receive(:foo).and_return('bar')
|
9
|
+
regions = Regions.new([test_region])
|
10
|
+
expect(regions.get_by_foo('bar')).to be test_region
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'raises a NoMethodError error if the field does not exist' do
|
14
|
+
test_region = Object.new
|
15
|
+
regions = Regions.new([test_region])
|
16
|
+
expect { regions.get_by_foo('bar') }.to raise_error NoMethodError
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/sau_spec.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
module SAU
|
4
|
+
describe :get_regions do
|
5
|
+
it 'gets the regions of a given type' do
|
6
|
+
stub_request(:get, "#{BASE_URL}#{TEST_REGION}/?nospatial=true").
|
7
|
+
to_return(body: { data: [ TEST_REGION_DETAIL ]}.to_json)
|
8
|
+
regions = SAU.get_regions(TEST_REGION)
|
9
|
+
expect(regions).to be_instance_of Regions
|
10
|
+
expect(regions.regions.first).to be_instance_of Region
|
11
|
+
expect(regions.regions.first.region).to eq TEST_REGION
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe :get_region_detail do
|
16
|
+
it 'gets the region detail for a specified type and id' do
|
17
|
+
expect_any_instance_of(Region).to receive(:get_detail)
|
18
|
+
SAU.get_region_detail(TEST_REGION, TEST_REGION_ID)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe :get_region_data do
|
23
|
+
it 'gets the region data for a specified type and id with params' do
|
24
|
+
expect_any_instance_of(Region).to receive(:get_data)
|
25
|
+
SAU.get_region_data(TEST_REGION, TEST_REGION_ID, TEST_DATA_PARAMS)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe :call_api do
|
30
|
+
it 'calls the API' do
|
31
|
+
stub = stub_request(:get, "#{BASE_URL}foobar").to_return(body: { data: 'success' }.to_json)
|
32
|
+
expect(SAU.call_api('foobar')).to eq 'success'
|
33
|
+
expect(stub).to have_been_requested
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'raises an error for non 200 status' do
|
37
|
+
stub = stub_request(:get, "#{BASE_URL}foobar").to_return(status: 500, body: 'error')
|
38
|
+
expect { SAU.call_api('foobar') }.to raise_error(RestClient::InternalServerError)
|
39
|
+
expect(stub).to have_been_requested
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.require(:test)
|
3
|
+
require_relative '../lib/sau'
|
4
|
+
|
5
|
+
TEST_REGION = 'reg'
|
6
|
+
TEST_REGION_ID = 42
|
7
|
+
TEST_REGION_TITLE = 'Test Region'
|
8
|
+
TEST_DATA_PARAMS = { measure: 'mes', dimension: 'dim', limit: 10, sciname: true }
|
9
|
+
TEST_REGION_DETAIL = { id: TEST_REGION_ID, title: TEST_REGION_TITLE, foo: 'bar' }
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sau-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Scott Reis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: webmock
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.21'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.21'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- LICENSE
|
62
|
+
- README.md
|
63
|
+
- lib/sau.rb
|
64
|
+
- lib/sau/region.rb
|
65
|
+
- lib/sau/regions.rb
|
66
|
+
- lib/sau/version.rb
|
67
|
+
- spec/sau/region_spec.rb
|
68
|
+
- spec/sau/regions_spec.rb
|
69
|
+
- spec/sau_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
homepage: https://github.com/seaaroundus/sau-ruby
|
72
|
+
licenses:
|
73
|
+
- SEE LICENSE IN LICENSE
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '2.1'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.4.3
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Ruby wrapper for the Sea Around Us API
|
95
|
+
test_files: []
|