Sha256: 797768c301c56ec4a7d120f92182a8edeaa5f8cf3628811f15d1829b8a8ede66

Contents?: true

Size: 1012 Bytes

Versions: 1

Compression:

Stored size: 1012 Bytes

Contents

require 'net/http'
require 'json'

module ArcREST
  # adds metadata method
  class Server
    REGEX = %r{^\/arcgis\/rest\/services}i
    BAD_ENDPOINT = 'Invalid ArcGIS endpoint'.freeze

    attr_reader :url, :json, :version

    def initialize(url)
      @url = url
      @uri = uri
      @server_uri = server_uri
      @json = json(@uri)
      @version = version
    end

    def json(uri, options = {})
      JSON.parse get(uri, options)
    end

    def version
      json(server_uri)['currentVersion'] # subclasses use server uri
    end

    protected

    def uri
      raise ArgumentError, BAD_ENDPOINT if (URI(@url).path =~ REGEX) != 0
      URI @url
    end

    def server_uri
      URI::HTTP.build(host: @uri.host, path: '/arcgis/rest/services')
    end

    def add_json_param_to(hash)
      { f: 'pjson' }.merge(hash) # 'pjson' guarantees id's unique
    end

    def get(uri, options = {})
      uri.query = URI.encode_www_form(add_json_param_to(options))
      Net::HTTP.get uri
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arcrest-0.0.1 lib/arcrest/server.rb