Class: DataSet

Inherits:
Object
  • Object
show all
Defined in:
lib/gfw_api_client/data_set.rb

Class Method Summary collapse

Class Method Details

.find_set(table_space, concessions = nil, country = nil, region = nil, wdpa_id = nil, use_id = nil, type = nil, period = nil, geojson = nil, api_url = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gfw_api_client/data_set.rb', line 3

def self.find_set(table_space, concessions=nil, country=nil, region=nil, wdpa_id=nil, use_id=nil, type=nil, period=nil, geojson=nil, api_url=nil)

  if api_url
    # Get client credentials for GFW API from params
    gfw_api_url = api_url
  else
    # Get client credentials for GFW API from YML file
    gfw_api_url = if defined?(Rails)
                    YAML::load_file(File.join('config/gfw.yml'))[Rails.env]['gfw_api_url']
                  else
                    'http://staging.gfw-apis.appspot.com'
                  end
  end

  base_url = "#{gfw_api_url}/forest-change/#{table_space}"

  request = case concessions
            when 'iso'
              path =  "#{base_url}/admin/#{country}"
              path += "/#{region}" if region
              path += "?period=#{period}" if period

              Typhoeus::Request.new(path, followlocation: true)
            when 'wdpa'
              path =  "#{base_url}/wdpa/#{wdpa_id}"
              path += "?period=#{period}" if period

              Typhoeus::Request.new(path, followlocation: true)
            when 'use'
              path =  "#{base_url}/use/#{type}/#{use_id}"
              path += "?period=#{period}" if period

              Typhoeus::Request.new(path, followlocation: true)
    			  when 'geojson'
              path =  "#{base_url}?type=geojson&geojson=#{CGI::escape(geojson)}"
              path += "&period=#{period}" if period

              Typhoeus::Request.new(path, followlocation: true)
            end

  request.on_complete do |response|
    response if response.success?
  end

  request.run

end