Skip to content
This repository
Browse code

Initial implementation of layers. Not saving yet

  • Loading branch information...
commit 2ef85ea25c10440b74c05fc49dcd074e91bb844d 1 parent f04437e
Renzo Sanchez-Silva authored
1  lib/rgeoserver.rb
@@ -14,6 +14,7 @@ module RGeoServer
14 14 autoload :DataStore, "rgeoserver/datastore"
15 15 autoload :CoverageStore, "rgeoserver/coveragestore"
16 16 autoload :WmsStore, "rgeoserver/wmsstore"
  17 + autoload :Layer, "rgeoserver/layer"
17 18
18 19 require 'restclient'
19 20 require 'nokogiri'
30 lib/rgeoserver/catalog.rb
@@ -41,7 +41,7 @@ def get_workspaces &block
41 41 ResourceInfo.list Workspace, self, workspaces, {}, &block
42 42 end
43 43
44   - # @param [String] workspace
  44 + # @param [String] workspace name
45 45 # @return [<RGeoServer::Workspace]
46 46 def get_workspace workspace
47 47 response = self.search :workspaces => workspace
@@ -52,7 +52,10 @@ def get_workspace workspace
52 52
53 53 # @return [<RGeoServer::Workspace]
54 54 def get_default_workspace
55   - return Workspace.new self, :name => 'default'
  55 + dw = Workspace.new self, :name => 'default'
  56 + w = Workspace.new self, :name => w.name
  57 + raise "No default workspace is available in the catalog" unless w.new?
  58 + w
56 59 end
57 60
58 61 def set_default_workspace
@@ -65,9 +68,28 @@ def reassign_workspace store, workspace
65 68 pass
66 69 end
67 70
68   - # List of feature types
  71 +
  72 + # List of available layers
  73 + # @return [Array<RGeoServer::Layer>]
  74 + def get_layers &block
  75 + response = self.search :layers => nil
  76 + doc = Nokogiri::XML(response)
  77 + layers = doc.xpath(Layer.root_xpath).collect{|l| l.text.to_s }
  78 + ResourceInfo.list Layer, self, layers, {}, &block
  79 + end
  80 +
  81 + # @param [String] layer name
  82 + # @return [<RGeoServer::Layer]
  83 + def get_layer layer
  84 + response = self.search :layers => layer
  85 + doc = Nokogiri::XML(response)
  86 + name = doc.at_xpath(Layer.member_xpath)
  87 + return Workspace.new self, :name => name.text if name
  88 + end
  89 +
  90 +
  91 + # List of available namespaces
69 92 # @return [Array<RGeoServer::Namespace>]
70   - # TODO: Implement when the stable release includes it
71 93 def get_namespaces
72 94 raise NotImplementedError
73 95 end
2  lib/rgeoserver/coverage.rb
@@ -75,7 +75,7 @@ def profile_xml_to_hash profile_xml
75 75 h = {
76 76 "coverage_store" => @coverage_store.name,
77 77 "workspace" => @workspace.name,
78   - "name" => doc.at_xpath('//name/text()').text.strip,
  78 + "name" => doc.at_xpath('//name/text()').text.strip,,
79 79 "nativeName" => doc.at_xpath('//nativeName/text()').to_s,
80 80 "title" => doc.at_xpath('//title/text()').to_s,
81 81 "supportedFormats" => doc.xpath('//supportedFormats/string/text()').collect{ |t| t.to_s }
6 lib/rgeoserver/featuretype.rb
@@ -92,9 +92,9 @@ def catalog= val
92 92 def profile_xml_to_hash profile_xml
93 93 doc = profile_xml_to_ng profile_xml
94 94 h = {
95   - :name => doc.at_xpath('//name').text.strip,
96   - :workspace => @workspace.name,
97   - :nativeName => doc.at_xpath('//nativeName').text.to_s
  95 + "name" => doc.at_xpath('//name').text.strip,
  96 + "workspace" => @workspace.name,
  97 + "nativeName" => doc.at_xpath('//nativeName').to_s
98 98 }.freeze
99 99 h
100 100 end
108 lib/rgeoserver/layer.rb
... ... @@ -0,0 +1,108 @@
  1 +
  2 +module RGeoServer
  3 +
  4 + class Layer < ResourceInfo
  5 +
  6 + OBJ_ATTRIBUTES = {:enabled => 'enabled', :catalog => 'catalog', :name => 'name', :default_style => 'default_style', :alternative_style => 'alternative_style' }
  7 + OBJ_DEFAULT_ATTRIBUTES = {:enabled => 'true', :catalog => nil, :name => nil, :default_style => nil, :alternative_styles => [] }
  8 +
  9 + define_attribute_methods OBJ_ATTRIBUTES.keys
  10 + update_attribute_accessors OBJ_ATTRIBUTES
  11 +
  12 + @@r = Confstruct::Configuration.new(:route => 'layers', :resource_name => 'layer')
  13 +
  14 + def self.resource_name
  15 + @@r.resource_name
  16 + end
  17 +
  18 + def self.create_method
  19 + :post
  20 + end
  21 +
  22 + def self.update_method
  23 + :put
  24 + end
  25 +
  26 + def self.root_xpath
  27 + "//#{@@r.route}/#{@@r.resource_name}"
  28 + end
  29 +
  30 + def self.member_xpath
  31 + "//#{resource_name}"
  32 + end
  33 +
  34 + def route
  35 + @@r.route
  36 + end
  37 +
  38 + def message
  39 + builder = Nokogiri::XML::Builder.new do |xml|
  40 + xml.layer {
  41 + xml.enabled @enabled if enabled_changed?
  42 + xml.name @name
  43 + }
  44 + end
  45 + return builder.doc.to_xml
  46 + end
  47 +
  48 + # @param [RGeoServer::Catalog] catalog
  49 + # @param [Hash] options
  50 + def initialize catalog, options
  51 + super({})
  52 + _run_initialize_callbacks do
  53 + @catalog = catalog
  54 + @name = options[:name].strip
  55 + end
  56 + @route = route
  57 + end
  58 +
  59 + def resource
  60 + unless profile['resource'].empty?
  61 + data_type = profile['resource']['type']
  62 + workspace = profile['resource']['workspace']
  63 + name = profile['resource']['name']
  64 + store = profile['resource']['store']
  65 +
  66 + case data_type
  67 + when 'coverage'
  68 + return RGeoServer::Coverage.new @catalog, :workspace => workspace, :coverage_store => store, :name => name
  69 + when 'featureType'
  70 + return RGeoServer::FeatureType.new @catalog, :workspace => workspace, :data_store => store, :name => name
  71 + else
  72 + nil
  73 + end
  74 + else
  75 + nil
  76 + end
  77 + end
  78 +
  79 + def profile_xml_to_hash profile_xml
  80 + doc = profile_xml_to_ng profile_xml
  81 + name = doc.at_xpath('//name/text()').text.strip
  82 + link = doc.at_xpath('//resource//atom:link/@href', "xmlns:atom"=>"http://www.w3.org/2005/Atom").text.strip
  83 + workspace, _, store = link.match(/workspaces\/(.*?)\/(.*?)\/(.*?)\/(.*?)\/#{name}.xml$/).to_a[1,3]
  84 +
  85 + h = {
  86 + "name" => name,
  87 + "path" => doc.at_xpath('//path/text()').to_s,
  88 + "defaultstyle" => doc.at_xpath('//defaultStyle/name/text()').to_s,
  89 + "styles" => doc.xpath('//styles/style/name/text()').collect{ |s| s.to_s},
  90 + "type" => doc.at_xpath('//type/text()').to_s,
  91 + "enabled" => doc.at_xpath('//enabled/text()').to_s,
  92 + "attribution" => {
  93 + "logoWidth" => doc.at_xpath('//attribution/logoWidth/text()').to_s,
  94 + "logoHeight" => doc.at_xpath('//attribution/logoHeight/text()').to_s
  95 + },
  96 + "resource" => {
  97 + "type" => doc.at_xpath('//resource/@class').to_s,
  98 + "name" => doc.at_xpath('//resource/name').to_s,
  99 + "store" => store,
  100 + "workspace" => workspace
  101 + },
  102 + "metadata" => doc.xpath('//metadata/entry').inject({}){ |h, e| h.merge(e['key']=> e.text.to_s) }
  103 + }.freeze
  104 + h
  105 + end
  106 +
  107 + end
  108 +end
23 lib/rgeoserver/resource.rb
@@ -131,28 +131,9 @@ def profile= profile_xml
131 131 def profile_xml_to_ng profile_xml
132 132 Nokogiri::XML(profile_xml).xpath(self.class.member_xpath)
133 133 end
134   -
  134 +
135 135 def profile_xml_to_hash profile_xml
136   - doc = profile_xml_to_ng profile_xml
137   - h = {'name' => doc.at_xpath('//name').text.strip, 'enabled' => @enabled }
138   - doc.xpath('//atom:link/@href', "xmlns:atom"=>"http://www.w3.org/2005/Atom" ).each{ |l|
139   - target = l.text.match(/([a-zA-Z]+)\.xml$/)[1]
140   - if !target.nil? && target != l.parent.parent.name.to_s.downcase
141   - begin
142   - h[l.parent.parent.name.to_s] << target
143   - rescue
144   - h[l.parent.parent.name.to_s] = []
145   - end
146   - else
147   - h[l.parent.parent.name.to_s] = begin
148   - response = @catalog.fetch_url l.text
149   - Nokogiri::XML(response).xpath('//name/text()').collect{ |a| a.text }
150   - rescue RestClient::ResourceNotFound
151   - []
152   - end.freeze
153   - end
154   - }
155   - h
  136 + raise NotImplementedError
156 137 end
157 138
158 139 end
5 lib/rgeoserver/wmsstore.rb
@@ -99,6 +99,11 @@ def catalog= val
99 99 catalog_will_change! unless val == @catalog
100 100 @catalog = val
101 101 end
  102 +
  103 + def profile_xml_to_hash profile_xml
  104 + doc = profile_xml_to_ng profile_xml
  105 + return {'name' => doc.at_xpath('//name').text.strip, 'enabled' => @enabled }
  106 + end
102 107
103 108 end
104 109 end
23 lib/rgeoserver/workspace.rb
@@ -68,5 +68,28 @@ def wms_stores &block
68 68 self.class.list WmsStore, @catalog, profile['wmsStores'], {:workspace => self}, check_remote = true, &block
69 69 end
70 70
  71 + def profile_xml_to_hash profile_xml
  72 + doc = profile_xml_to_ng profile_xml
  73 + h = {'name' => doc.at_xpath('//name').text.strip, 'enabled' => @enabled }
  74 + doc.xpath('//atom:link/@href', "xmlns:atom"=>"http://www.w3.org/2005/Atom" ).each{ |l|
  75 + target = l.text.match(/([a-zA-Z]+)\.xml$/)[1]
  76 + if !target.nil? && target != l.parent.parent.name.to_s.downcase
  77 + begin
  78 + h[l.parent.parent.name.to_s] << target
  79 + rescue
  80 + h[l.parent.parent.name.to_s] = []
  81 + end
  82 + else
  83 + h[l.parent.parent.name.to_s] = begin
  84 + response = @catalog.fetch_url l.text
  85 + Nokogiri::XML(response).xpath('//name/text()').collect{ |a| a.text }
  86 + rescue RestClient::ResourceNotFound
  87 + []
  88 + end.freeze
  89 + end
  90 + }
  91 + h
  92 + end
  93 +
71 94 end
72 95 end
10 spec/lib/integration_test_spec.rb
@@ -7,6 +7,7 @@
7 7 @catalog = RGeoServer.catalog
8 8 end
9 9
  10 +
10 11 context "Workspaces" do
11 12 it "should list workspaces" do
12 13 @catalog.get_workspaces.each{ |obj|
@@ -73,6 +74,14 @@
73 74 end
74 75 end
75 76
  77 + context "Layers" do
  78 + it "shoult list layers" do
  79 + @catalog.get_layers.each { |l|
  80 + l.profile.should_not be_empty
  81 + }
  82 + end
  83 + end
  84 +
76 85 context "Stores" do
77 86 before :all do
78 87 @ws = RGeoServer::Workspace.new @catalog, :name => 'test_workspace_for_stores'
@@ -114,6 +123,7 @@
114 123 ds.delete
115 124 end
116 125 end
  126 +
117 127 context "CoverageStores" do
118 128 it "should create a coverage store under existing workspace, update and delete it right after" do
119 129 cs = RGeoServer::CoverageStore.new @catalog, :workspace => @ws, :name => 'test_coverage_store'

0 comments on commit 2ef85ea

Please sign in to comment.
Something went wrong with that request. Please try again.