Module: RGeoServer::RestApiClient
- Includes:
- ActiveSupport::Benchmarkable, GeoServerUrlHelpers
- Included in:
- Catalog
- Defined in:
- lib/rgeoserver/rest_api_client.rb
Constant Summary
Constants included from GeoServerUrlHelpers
GeoServerUrlHelpers::URI_FORMATS, GeoServerUrlHelpers::URI_REGEX, GeoServerUrlHelpers::URI_SEQUENCES
Instance Method Summary collapse
-
#add(what, message, method, options = {}) ⇒ Object
Add resource to the catalog.
-
#client(config = {}) ⇒ RestClient
Cached or new client.
-
#do_url(sub_url, method = :get, data = nil, options = {}, client = @client) ⇒ Object
Do an action on an arbitrary URL path within the catalog Default method is GET.
-
#gwc_client(config = {}) ⇒ RestClient
Cached or new client.
- #headers(format) ⇒ Object
-
#modify(what, message, method, options = {}) ⇒ Object
Modify resource in the catalog.
-
#purge(what, options) ⇒ Object
Purge resource from the catalog.
-
#rest_client(c) ⇒ Object
Instantiates a rest client with passed configuration return [RestClient::Resource].
-
#search(what, options = {}) ⇒ Object
Search a resource in the catalog.
Methods included from GeoServerUrlHelpers
Instance Method Details
#add(what, message, method, options = {}) ⇒ Object
Add resource to the catalog
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rgeoserver/rest_api_client.rb', line 93 def add what, , method, = {} h = .delete(:headers) || headers(:xml) request = client[url_for(what, )] request.[:headers] = h $logger.debug "Adding: \n #{}" begin ap({:add_request => request, :add_message => Nokogiri::XML()}) if $DEBUG return request.send method, 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 = {}) ⇒ RestClient
Returns cached or new client.
28 29 30 |
# 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 = {}, client = @client) ⇒ Object
Do an action on an arbitrary URL path within the catalog Default method is GET
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rgeoserver/rest_api_client.rb', line 74 def do_url sub_url, method = :get, data = nil, = {}, client = @client sub_url.slice! client.url fetcher = client[sub_url] fetcher..merge() 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 = {}) ⇒ RestClient
Returns cached or new client.
33 34 35 36 37 38 39 40 41 42 43 44 |
# 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) ⇒ Object
46 47 48 49 |
# 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 = {}) ⇒ Object
Modify resource in the catalog
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/rgeoserver/rest_api_client.rb', line 114 def modify what, , method, = {} h = .delete(:headers) || headers(:xml) request = client[url_for(what, )] request.[:headers] = h $logger.debug "Modifying: \n #{}" begin ap({:modify_request => request, :modify_message => Nokogiri::XML()}) if $DEBUG return request.send method, 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) ⇒ Object
Purge resource from the catalog. Options can include recurse=true or false
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rgeoserver/rest_api_client.rb', line 133 def purge what, request = client[url_for(what, )] $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) ⇒ Object
Instantiates a rest client with passed configuration return [RestClient::Resource]
16 17 18 19 20 21 22 23 24 25 |
# 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 |
#search(what, options = {}) ⇒ Object
Search a resource in the catalog
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rgeoserver/rest_api_client.rb', line 54 def search what, = {} h = .delete(:headers) || headers(:xml) resources = client[url_for(what, )] resources.[:headers] = h begin ap({ :func => { :search => what }, :request => resources }) if $DEBUG return resources.get rescue RestClient::InternalServerError => e $logger.error e.response $logger.flush if $logger.respond_to? :flush raise GeoServerInvalidRequest, "Error listing #{what.inspect}. See $logger for details" end end |