module RGeoServer::RestApiClient

Public Instance Methods

add(what, message, method, options = {}) click to toggle source

Add resource to the catalog @param [String] what @param [String] message @param [Symbol] method @param [Hash] options

# File lib/rgeoserver/rest_api_client.rb, line 93
def add what, message, method, options = {}
  h = options.delete(:headers) || headers(:xml)
  request = client[url_for(what, options)]
  request.options[:headers] = h
  $logger.debug "Adding: \n #{message}"
  begin
    ap({:add_request => request, :add_message => Nokogiri::XML(message)}) if $DEBUG
    return request.send method, message
  rescue RestClient::InternalServerError => e
    $logger.error e.response
    $logger.flush if $logger.respond_to? :flush
    raise GeoServerInvalidRequest, "Error adding #{what.inspect}. See logger for details"
  end

end
client(config = {}) click to toggle source

@return [RestClient] cached or new client

# File lib/rgeoserver/rest_api_client.rb, line 28
def client config = {}
  @client ||= rest_client(config.merge(self.config[:restclient]).merge(self.config).with_indifferent_access)
end
do_url(sub_url, method = :get, data = nil, options = {}) click to toggle source

Do an action on an arbitrary URL path within the catalog Default method is GET @param [String] sub_url @param [String] method @param [String] data payload @param [Hash] options for request

# File lib/rgeoserver/rest_api_client.rb, line 74
def do_url sub_url, method = :get, data = nil, options = {}, client = @client
  sub_url.slice! client.url
  fetcher = client[sub_url]
  fetcher.options.merge(options)
  begin
    return fetcher.get if method == :get
    fetcher.send method, data
  rescue RestClient::InternalServerError => e
    $logger.error e.response
    $logger.flush if $logger.respond_to? :flush
    raise GeoServerInvalidRequest, "Error fetching URL: #{sub_url}. See $logger for details"
  end
end
gwc_client(config = {}) click to toggle source

@return [RestClient] cached or new client

# File lib/rgeoserver/rest_api_client.rb, line 33
def gwc_client config = {}
  unless @gwc_client.is_a? RestClient::Resource
    c = config.merge(self.config[:restclient]).merge(self.config)
    if c[:geowebcache_url].nil? or c[:geowebcache_url] == 'builtin'
      c[:url] = c[:url].gsub(%r{/rest$}, '/gwc/rest') # switch to built-in GeoServer GWC
    else
      c[:url] = c[:geowebcache_url]
    end
    @gwc_client = rest_client(c)
  end
  @gwc_client
end
headers(format) click to toggle source
# File lib/rgeoserver/rest_api_client.rb, line 46
def headers format
  sym = :xml || format.to_sym
  {:accept => sym, :content_type=> sym}
end
modify(what, message, method, options = {}) click to toggle source

Modify resource in the catalog @param [String] what @param [String] message @param [Symbol] method @param [Hash] options

# File lib/rgeoserver/rest_api_client.rb, line 114
def modify what, message, method, options = {}
  h = options.delete(:headers) || headers(:xml)
  request = client[url_for(what, options)]
  request.options[:headers] = h
  $logger.debug "Modifying: \n #{message}"
  begin
    ap({:modify_request => request, :modify_message => Nokogiri::XML(message)}) if $DEBUG
    return request.send method, message
  rescue RestClient::InternalServerError => e
    $logger.error e.response
    $logger.flush if $logger.respond_to? :flush
    raise GeoServerInvalidRequest, "Error modifying #{what.inspect}. See $logger for details"
  end

end
purge(what, options) click to toggle source

Purge resource from the catalog. Options can include recurse=true or false @param [OrderedHash] what @param [Hash] options

# File lib/rgeoserver/rest_api_client.rb, line 133
def purge what, options
  request = client[url_for(what, options)]
  $logger.debug "Purge: \n #{request}"
  begin
    ap({:purge_request => request}) if $DEBUG
    return request.delete
  rescue RestClient::InternalServerError => e
    $logger.error e.response
    $logger.flush if $logger.respond_to? :flush
    raise GeoServerInvalidRequest, "Error deleting #{what.inspect}. See $logger for details"
  end
end
rest_client(c) click to toggle source

Instantiates a rest client with passed configuration @param [Hash] c configuration return [RestClient::Resource]

# File lib/rgeoserver/rest_api_client.rb, line 16
def rest_client c
  ap({:rest_client => c}) if $DEBUG
  raise ArgumentError, "#rest_client requires :url" if c[:url].nil?
  RestClient::Resource.new(c[:url],
      :user => c[:user],
      :password => c[:password],
      :headers => c[:headers],
      :timeout => (c[:timeout] || 300).to_i,
      :open_timeout => (c[:open_timeout] || 60).to_i)
end