gi_cat_driver 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gi_cat_driver (0.2.0)
4
+ gi_cat_driver (0.2.2)
5
5
  faraday (~> 0.8.7)
6
6
  nokogiri (~> 1.5.6)
7
7
 
data/README.md CHANGED
@@ -43,6 +43,9 @@ Annotated source code documentation is available at http://nsidc.github.com/gi_c
43
43
  Rubydoc API documentation is available at http://rubydoc.info/gems/gi_cat_driver/
44
44
 
45
45
  ## Version History
46
+ * 0.2.2
47
+ * Added new methods to create and delete profiles
48
+ * Fixed harvester request headers and now work properly
46
49
  * 0.2.1
47
50
  * Fixed harvester requests to use Faraday
48
51
  * 0.2.0
@@ -1,3 +1,3 @@
1
1
  module GiCatDriver
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/gi_cat_driver.rb CHANGED
@@ -39,11 +39,35 @@ module GiCatDriver
39
39
  Faraday.get(@base_url + "/").status == 200
40
40
  end
41
41
 
42
+ # Add a new profile with the given name
43
+ def create_profile( profile_name )
44
+ response = Faraday.post do |req|
45
+ req.url "#{@base_url}/services/conf/brokerConfigurations/newBroker"
46
+ req.body = "inputNewName=#{profile_name}&nameBrokerCopy=%20"
47
+ req.headers = AUTHORIZATION_HEADERS.merge({'enctype' => 'multipart/form-data',:content_type => 'application/x-www-form-urlencoded'})
48
+ end
49
+
50
+ profile_id = response.body
51
+ return profile_id
52
+ end
53
+
54
+ # Remove a profile with the given name
55
+ def delete_profile( profile_name )
56
+ profile_id = find_profile_id( profile_name )
57
+ response = Faraday.get do |req|
58
+ req.url "#{@base_url}/services/conf/brokerConfigurations/#{profile_id}", { :opts => 'delete', :random => generate_random_number }
59
+ req.headers = AUTHORIZATION_HEADERS.merge({'enctype'=>'multipart/form-data'})
60
+ end
61
+
62
+ profile_id = response.body
63
+ return profile_id
64
+ end
65
+
42
66
  # Retrieve the ID for a profile given the name
43
67
  # Returns an integer ID reference to the profile
44
68
  def find_profile_id( profile_name )
45
69
  response = Faraday.get do |req|
46
- req.url @base_url + '/services/conf/brokerConfigurations', :nameRepository => 'gicat'
70
+ req.url "#{@base_url}/services/conf/brokerConfigurations", :nameRepository => 'gicat'
47
71
  req.headers = AUTHORIZATION_HEADERS
48
72
  end
49
73
 
@@ -126,8 +150,11 @@ module GiCatDriver
126
150
  # Retrive the distributor id given a profile id
127
151
  def get_active_profile_distributor_id(id)
128
152
  active_profile_request = "#{@base_url}/services/conf/brokerConfigurations/#{id}"
129
- response = Faraday.get(active_profile_request, STANDARD_HEADERS)
130
- id = Nokogiri::XML(response.body).css("component id").text
153
+ response = Faraday.get do |req|
154
+ req.url active_profile_request
155
+ req.headers = AUTHORIZATION_HEADERS
156
+ end
157
+ id = Nokogiri.XML(response.body).css("component id").text
131
158
  return id
132
159
  end
133
160
 
@@ -135,7 +162,10 @@ module GiCatDriver
135
162
  def get_harvest_resources(profile_id)
136
163
  id = get_active_profile_distributor_id(profile_id)
137
164
  harvest_resource_request = "#{@base_url}/services/conf/brokerConfigurations/#{profile_id}/distributors/#{id}"
138
- response = Faraday.get(harvest_resource_request, STANDARD_HEADERS)
165
+ response = Faraday.get do |req|
166
+ req.url harvest_resource_request
167
+ req.headers = AUTHORIZATION_HEADERS
168
+ end
139
169
  doc = Nokogiri::XML(response.body)
140
170
  harvestersinfo_array = {}
141
171
  doc.css("component").each do |component|
@@ -146,53 +176,38 @@ module GiCatDriver
146
176
 
147
177
  # Harvest from specified resource
148
178
  def harvest_resource_for_active_configuration(harvesterid, harvestername = "n/a")
149
- Faraday.get(
150
- "#{@base_url}/services/conf/brokerConfigurations/#{self.get_active_profile_id}/harvesters/#{harvesterid}/start",
151
- AUTHORIZATION_HEADERS) do |response, request, result, &block|
152
- case response.code
153
- when 200
154
- puts "#{Time.now}: Initiate harvesting GI-Cat resource #{harvestername}. Please wait a couple minutes for the process to complete."
155
- else
156
- raise "Failed to initiate harvesting GI-Cat resource #{harvestername}."
157
- response.return!(request, result, &block)
158
- end
159
- end
160
- end
179
+ response = Faraday.get do |req|
180
+ req.url "#{@base_url}/services/conf/brokerConfigurations/#{self.get_active_profile_id}/harvesters/#{harvesterid}/start"
181
+ req.headers = AUTHORIZATION_HEADERS
182
+ end
161
183
 
162
- # Parsing and handle the harvest status
163
- def harvest_status(status, harvestername="default")
164
- timestamp = Time.now
165
- puts "#{Time.now}: Harvest #{harvestername} status: #{status}"
166
- case status
167
- when /Harvesting completed/
168
- return :completed
169
- when /Harvesting error/
170
- return :error
184
+ if response.status == 200
185
+ puts "#{Time.now}: Initiate harvesting GI-Cat resource #{harvestername}. Please wait a couple minutes for the process to complete."
171
186
  else
172
- return :pending
187
+ raise "Failed to initiate harvesting GI-Cat resource #{harvestername}."
188
+ response.return!(request, result, &block)
173
189
  end
174
190
  end
175
191
 
176
- # Run till the harvest of a resource is completed
177
- def harvest_request_is_done(harvesterid, harvestername="n/a")
192
+ # Run a loop to request GI-Cat for the status of a harvest
193
+ def harvest_request_is_done(harvester_id, harvester_name="n/a")
178
194
  while(1) do
179
- sleep 10 # Wait for ten seconds
180
-
181
- rnum=rand
182
- request = @base_url + "/services/conf/giconf/status?id=#{harvesterid}&rand=#{rnum}"
183
-
184
- response = Faraday.get request, STANDARD_HEADERS
185
- responsexml = Nokogiri::XML::Reader(response.body)
186
- responsexml.each do |node|
187
- if node.name == "status" && !node.inner_xml.empty?
188
- status = harvest_status(node.inner_xml, harvestername)
189
- if status.eql? :error
190
- fail "Error harvesting the resource #{harvestername}"
191
- elsif status.eql? :completed
192
- puts "Succesfully harvested #{harvestername}"
193
- return
194
- end
195
- end
195
+ sleep 1 # Wait for one second
196
+
197
+ # rnum=rand # Generate unique request to attempt to bypass cache
198
+ request = @base_url + "/services/conf/giconf/status?id=#{harvester_id}"
199
+
200
+ response = Faraday.get do |req|
201
+ req.url request
202
+ req.headers = AUTHORIZATION_HEADERS
203
+ end
204
+
205
+ status = parse_status_xml(response.body, harvester_name)
206
+ if status.eql? :error
207
+ fail "Error harvesting the resource #{harvester_name}"
208
+ elsif status.eql? :completed
209
+ puts "Succesfully harvested #{harvester_name}"
210
+ break
196
211
  end
197
212
  end
198
213
  end
@@ -219,5 +234,27 @@ module GiCatDriver
219
234
  return profile.attr('id').value
220
235
  end
221
236
 
237
+ def parse_status_xml( xml_doc, harvester_name )
238
+ xml = Nokogiri.XML(xml_doc)
239
+
240
+ status = xml.css("status").text
241
+ timestamp = Time.now
242
+
243
+ puts "#{Time.now}: Harvest #{harvester_name} status: #{status}"
244
+
245
+ case status
246
+ when /Harvesting completed/
247
+ return :completed
248
+ when /Harvesting error/
249
+ return :error
250
+ else
251
+ return :pending
252
+ end
253
+
254
+ end
255
+
256
+ def generate_random_number
257
+ return rand
258
+ end
222
259
  end
223
260
  end
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?><distributors xmlns="http://imaa.cnr.it/sdi/services/7.0/configurationMessages/schema"><distributor id="UUID-682b22a3-8285-48ed-9dc1-4b0f88881d49"><component><id>UUID-a5d11731-9a58-4818-8018-c085cea8b6e3</id><type>Harvester</type><title>CISL OAI</title></component></distributor></distributors>
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?><statusHarvesterGiConf xmlns="http://imaa.cnr.it/sdi/services/7.0/configurationMessages/schema"><id>UUID-a5d11731-9a58-4818-8018-c085cea8b6e3</id><status>Harvesting completed</status><progress>0</progress></statusHarvesterGiConf>
@@ -22,8 +22,10 @@ describe GiCatDriver do
22
22
  expect(running).to be_true
23
23
  a_request(:get, @base_url).should have_been_made.once
24
24
  end
25
+ end
25
26
 
26
- it "can retrieve the active profile id" do
27
+ describe "profile configurations" do
28
+ it "retrieves the active profile id" do
27
29
  request_url = @base_url + "services/conf/giconf/configuration"
28
30
  stub_request(:get, request_url)
29
31
  .with(:headers => {
@@ -38,9 +40,7 @@ describe GiCatDriver do
38
40
  a_request(:get, request_url).should have_been_made.once
39
41
  expect(profile_id).to eq "1"
40
42
  end
41
- end
42
43
 
43
- describe "authorized requests" do
44
44
  it "can retrieve a profile id given a profile name" do
45
45
  request_url = "http://admin:pass@www.somecompany.com/services/conf/brokerConfigurations"
46
46
  stub_request(:get,request_url)
@@ -110,6 +110,38 @@ describe GiCatDriver do
110
110
  expect(@gi_cat.get_active_profile_id).to eq "1"
111
111
  end
112
112
 
113
+ it "adds a profile" do
114
+ stub_request(:get, "http://admin:pass@www.somecompany.com/services/conf/brokerConfigurations").
115
+ with(:headers => {'Accept'=>'application/xml', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'*/*', 'User-Agent'=>'Ruby'}, :query => {:nameRepository => "gicat"}).
116
+ to_return(:status => 200, :body => "", :headers => {})
117
+
118
+ stub_request(:post, "http://admin:pass@www.somecompany.com/services/conf/brokerConfigurations/newBroker").
119
+ with(:headers => {'Accept'=>'application/xml', 'Content-Type'=>'application/x-www-form-urlencoded', 'Enctype'=>'multipart/form-data', 'User-Agent'=>'Ruby'}).
120
+ to_return(:status => 200, :body => "5", :headers => {})
121
+
122
+ profile_id = @gi_cat.create_profile "new-profile"
123
+
124
+ profile_id.should eq "5"
125
+ end
126
+
127
+ it "deletes a profile" do
128
+ stub_request(:get, "http://admin:pass@www.somecompany.com/services/conf/brokerConfigurations").
129
+ with(:headers => {'Accept'=>'application/xml', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'*/*', 'User-Agent'=>'Ruby'}, :query => {:nameRepository => "gicat"}).
130
+ to_return(:status => 200, :body => File.new("spec/fixtures/brokerConfigurations.xml"), :headers => {})
131
+
132
+ @gi_cat.stub(:generate_random_number).and_return("1")
133
+
134
+ stub_request(:get, "http://admin:pass@www.somecompany.com/services/conf/brokerConfigurations/1?opts=delete&random=1").
135
+ with(:headers => {'Accept'=>'application/xml', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'*/*', 'User-Agent'=>'Ruby'}).
136
+ to_return(:status => 200, :body => "1", :headers => {})
137
+
138
+ profile_id = @gi_cat.delete_profile "some_profile"
139
+
140
+ profile_id.should eq "1"
141
+ end
142
+ end
143
+
144
+ describe "Lucene indexing" do
113
145
  it "enables Lucene for the active profile" do
114
146
  enable_lucene_url = "http://admin:pass@www.somecompany.com/services/conf/brokerConfigurations/1/luceneEnabled"
115
147
  stub_request(:put,enable_lucene_url)
@@ -138,8 +170,8 @@ describe GiCatDriver do
138
170
  }, :query => {:opts => "active"})
139
171
  .to_return(:status => 200, :body => File.new("spec/fixtures/brokerConfigurations.xml"), :headers => {})
140
172
 
141
- enable_conf_url = "http://www.somecompany.com/services/opensearchesip?bbox=&ct=&gdc=&lac=&loc=&luc=&outputFormat=&rel=&si=&st=arctic%20alaskan%20shrubs&te=&ts="
142
- stub_request(:get,enable_conf_url)
173
+ query_url = "http://www.somecompany.com/services/opensearchesip?bbox=&ct=&gdc=&lac=&loc=&luc=&outputFormat=&rel=&si=&st=arctic%20alaskan%20shrubs&te=&ts="
174
+ stub_request(:get, query_url)
143
175
  .with(:headers => {
144
176
  'Accept'=>'*/*',
145
177
  'User-Agent'=>'Ruby'
@@ -150,5 +182,95 @@ describe GiCatDriver do
150
182
  @gi_cat.is_lucene_enabled?.should be_true
151
183
  end
152
184
 
185
+ it "disables Lucene for the active profile" do
186
+ disable_lucene_url = "http://admin:pass@www.somecompany.com/services/conf/brokerConfigurations/1/luceneEnabled"
187
+ stub_request(:put,disable_lucene_url)
188
+ .with(:headers => {
189
+ 'Accept'=>'application/xml',
190
+ 'User-Agent'=>'Ruby',
191
+ 'Content-Type'=>'*/*'
192
+ }, :body => "false").to_return(:status => 200, :body => "", :headers => {})
193
+
194
+ conf_request_url = "http://www.somecompany.com/services/conf/giconf/configuration"
195
+ stub_request(:get,conf_request_url)
196
+ .with(:headers => {
197
+ 'Accept'=>'*/*',
198
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
199
+ 'User-Agent'=>'Ruby',
200
+ 'Content-Type'=>'application/xml'
201
+ }).to_return(:status => 200, :body => "1", :headers => {})
202
+
203
+ enable_conf_url = "http://admin:pass@www.somecompany.com/services/conf/brokerConfigurations/1"
204
+ stub_request(:get,enable_conf_url)
205
+ .with(:headers => {
206
+ 'Accept'=>'application/xml',
207
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
208
+ 'User-Agent'=>'Faraday v0.8.7',
209
+ 'Content-Type'=>'*/*'
210
+ }, :query => {:opts => "active"})
211
+ .to_return(:status => 200, :body => File.new("spec/fixtures/brokerConfigurations.xml"), :headers => {})
212
+
213
+ query_url = "http://www.somecompany.com/services/opensearchesip?bbox=&ct=&gdc=&lac=&loc=&luc=&outputFormat=&rel=&si=&st=arctic%20alaskan%20shrubs&te=&ts="
214
+ stub_request(:get, query_url)
215
+ .with(:headers => {
216
+ 'Accept'=>'*/*',
217
+ 'User-Agent'=>'Ruby'
218
+ }).to_return(:status => 200, :body => File.new("spec/fixtures/opensearchesip_lucene_disabled.xml"), :headers => {})
219
+
220
+ @gi_cat.disable_lucene
221
+
222
+ @gi_cat.is_lucene_enabled?.should be_false
223
+ end
224
+ end
225
+
226
+ describe "resource harvesting into the database" do
227
+ it "harvests all resources for the active profile" do
228
+ conf_request_url = "http://www.somecompany.com/services/conf/giconf/configuration"
229
+ stub_request(:get,conf_request_url)
230
+ .with(:headers => {
231
+ 'Accept'=>'*/*',
232
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
233
+ 'User-Agent'=>'Ruby',
234
+ 'Content-Type'=>'application/xml'
235
+ }).to_return(:status => 200, :body => "1", :headers => {})
236
+
237
+ request_url = "http://admin:pass@www.somecompany.com/services/conf/brokerConfigurations/1"
238
+ stub_request(:get,request_url)
239
+ .with(:headers => {
240
+ 'Accept'=>'application/xml',
241
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
242
+ 'User-Agent'=>'Ruby',
243
+ 'Content-Type'=>'*/*'
244
+ })
245
+ .to_return(:status => 200, :body => File.new("spec/fixtures/brokerConfigurations.xml"), :headers => {})
246
+
247
+ get_resources_url = "http://admin:pass@www.somecompany.com/services/conf/brokerConfigurations/1/distributors/UUID-6c89ab7d-82aa-446c-902e-0b1f6e412a45"
248
+ stub_request(:get, get_resources_url)
249
+ .with(:headers => {
250
+ 'Accept'=>'application/xml',
251
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
252
+ 'User-Agent'=>'Ruby',
253
+ 'Content-Type'=>'*/*'
254
+ })
255
+ .to_return(:status => 200, :body => File.new("spec/fixtures/distributors.xml"), :headers => {})
256
+
257
+ start_harvest_url = "http://admin:pass@www.somecompany.com/services/conf/brokerConfigurations/1/harvesters/UUID-a5d11731-9a58-4818-8018-c085cea8b6e3/start"
258
+ stub_request(:get, start_harvest_url)
259
+ .with(:headers => {
260
+ 'Accept'=>'application/xml',
261
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
262
+ 'Content-Type'=>'*/*',
263
+ 'User-Agent'=>'Ruby'
264
+ })
265
+ .to_return(:status => 200, :body => "", :headers => {})
266
+
267
+ harvest_status_url = "http://admin:pass@www.somecompany.com/services/conf/giconf/status?id=UUID-a5d11731-9a58-4818-8018-c085cea8b6e3"
268
+ stub_request(:get, harvest_status_url).
269
+ with(:headers => {'Accept'=>'application/xml', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type' => '*/*', 'User-Agent' => 'Ruby'}).
270
+ to_return(:status => 200, :body => File.new("spec/fixtures/status.xml"), :headers => [])
271
+
272
+ @gi_cat.harvest_all_resources_for_active_configuration
273
+ end
153
274
  end
275
+
154
276
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gi_cat_driver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-18 00:00:00.000000000 Z
13
+ date: 2013-04-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
@@ -194,8 +194,10 @@ files:
194
194
  - lib/gi_cat_driver/version.rb
195
195
  - spec/esip_opensearch_query_builder_spec.rb
196
196
  - spec/fixtures/brokerConfigurations.xml
197
+ - spec/fixtures/distributors.xml
197
198
  - spec/fixtures/opensearchesip_lucene_disabled.xml
198
199
  - spec/fixtures/opensearchesip_lucene_enabled.xml
200
+ - spec/fixtures/status.xml
199
201
  - spec/gi_cat_driver_spec.rb
200
202
  - spec/spec_helper.rb
201
203
  homepage: ''
@@ -226,7 +228,9 @@ summary: Configure and control deployed instances of GI-Cat.
226
228
  test_files:
227
229
  - spec/esip_opensearch_query_builder_spec.rb
228
230
  - spec/fixtures/brokerConfigurations.xml
231
+ - spec/fixtures/distributors.xml
229
232
  - spec/fixtures/opensearchesip_lucene_disabled.xml
230
233
  - spec/fixtures/opensearchesip_lucene_enabled.xml
234
+ - spec/fixtures/status.xml
231
235
  - spec/gi_cat_driver_spec.rb
232
236
  - spec/spec_helper.rb