class FC2Json

Attributes

object_identifier_field_name[RW]
records_per_request[RW]
service_url[RW]
where_clause[RW]

Public Class Methods

new(service_url, object_identifier_field_name = nil, records_per_request = nil, where_clause = nil) click to toggle source
# File lib/fc2json.rb, line 13
def initialize (service_url, object_identifier_field_name = nil, records_per_request = nil, where_clause = nil)
        @service_url = service_url
        @object_identifier_field_name = object_identifier_field_name || "FID"
        @records_per_request = (records_per_request || 2000).to_i
        @where_clause = where_clause || "1 = 1"
end

Public Instance Methods

get() click to toggle source
# File lib/fc2json.rb, line 20
def get
        ids = object_identifiers
        json = {}
        request_index = 0
        continue = true

        while continue do

                continue = false if ids[request_index * @records_per_request + (@records_per_request - 1)].nil?

                where = where_clause_for(ids, request_index)

                request_result = request_features(where)
                if request_result["exceededTransferLimit"]
                        raise ArgumentError, "The batch size of #{@records_per_request} exceeds the capability provided by the service used on this query. Review the service's batch size limit and adjust your query parameters."
                end

                if json.empty?
                        json = request_result
                else
                        json["features"].concat request_result["features"]
                end

                request_index += 1

                continue = false if continue && json["features"].count == ids.count
        end

        # Esri treats where clauses that returns no data as invalid. We return an empty hash instead.
        if json["error"]
                return {} if json["error"]["details"] == ["'where' parameter is invalid"]
        end
        json

end

Private Instance Methods

object_identifiers() click to toggle source
# File lib/fc2json.rb, line 70
def object_identifiers
        request_url = "#{self.service_url}/query?where=#{CGI.escape(self.where_clause)}&f=pjson&returnIdsOnly=true"
        (JSON.parse Nokogiri::HTML(open(request_url)))["objectIds"]
end
request_features(where_clause) click to toggle source
# File lib/fc2json.rb, line 58
def request_features(where_clause)
        JSON.parse(Nokogiri::HTML(open("#{@service_url}/query?where=#{CGI.escape(where_clause)}&f=pjson&outFields=*")))
end
where_clause_for(dataset, index) click to toggle source
# File lib/fc2json.rb, line 62
def where_clause_for(dataset, index)
        if dataset[index * @records_per_request + (@records_per_request - 1)].nil?
                "#{@object_identifier_field_name} >= #{dataset[index * @records_per_request]} AND #{@object_identifier_field_name} <= #{dataset.last}"
        else
                "#{@object_identifier_field_name} >= #{dataset[index * @records_per_request]} AND #{@object_identifier_field_name} <= #{dataset[index * @records_per_request + (@records_per_request - 1)]}"
        end
end