Class: GiCatDriver::GiCat

Inherits:
Object
  • Object
show all
Defined in:
lib/gi_cat_driver.rb

Constant Summary collapse

ATOM_NAMESPACE =
{ "atom" => "http://www.w3.org/2005/Atom" }
OPENSEARCH_NAMESPACE =
{ "opensearch" => "http://a9.com/-/spec/opensearch/1.1/" }
RELEVANCE_NAMESPACE =
{ "relevance" => "http://a9.com/-/opensearch/extensions/relevance/1.0/" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, username, password) ⇒ GiCat

Returns a new instance of GiCat.



24
25
26
27
28
29
30
31
# File 'lib/gi_cat_driver.rb', line 24

def initialize( url, username, password )
  @base_url = url.sub(/\/+$/, '')
  @admin_username = username
  @admin_password = password

  @standard_headers = { :content_type => "application/xml" }
  @authorization_headers = { :content_type => "*/*", :Accept => "application/xml", :Authorization => self.basic_auth_string }
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



22
23
24
# File 'lib/gi_cat_driver.rb', line 22

def base_url
  @base_url
end

Instance Method Details

#basic_auth_stringObject

Basic Authorization used in the request headers



34
35
36
# File 'lib/gi_cat_driver.rb', line 34

def basic_auth_string
  "Basic " + Base64.encode64("#{@admin_username}:#{@admin_password}").rstrip
end

#create_accessor(profile_name, accessor_configuration) ⇒ Object

Create an accessor (xml feed resource) for the given profile with the provided accessor configuration



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/gi_cat_driver.rb', line 154

def create_accessor( profile_name, accessor_configuration )
  profile_id = find_profile_id( profile_name )
  distributor_id = get_active_profile_distributor_id(profile_id)

  response = Faraday.post do |req|
    req.url "#{@base_url}/services/conf/brokerConfigurations/#{profile_id}/distributors/#{distributor_id}"
    req.headers = @authorization_headers.merge({:enctype=>'multipart/form-data', :content_type=>'application/x-www-form-urlencoded'})
    req.body = accessor_configuration
  end

  # The response contains a comma separated list of the accessor id as well as the harvester id
  (accessor_id, harvester_id) = response.body.split(',', 2)

  update_accessor_configuration( profile_id, accessor_id, accessor_configuration )

  enable_profile profile_name

  return accessor_id
end

#create_profile(profile_name) ⇒ Object

Add a new profile with the given name



44
45
46
47
48
49
50
51
52
53
# File 'lib/gi_cat_driver.rb', line 44

def create_profile( profile_name )
  response = Faraday.post do |req|
    req.url "#{@base_url}/services/conf/brokerConfigurations/newBroker"
    req.body = "inputNewName=#{profile_name}&nameBrokerCopy=%20"
    req.headers = @authorization_headers.merge({'enctype' => 'multipart/form-data',:content_type => 'application/x-www-form-urlencoded'})
  end

  profile_id = response.body
  return profile_id
end

#delete_accessor(profile_name, accessor_name) ⇒ Object

Remove an accessor (xml feed resource) with the given name from the given profile



175
176
177
178
179
180
181
182
183
# File 'lib/gi_cat_driver.rb', line 175

def delete_accessor( profile_name, accessor_name )
  profile_id = find_profile_id(profile_name)
  harvester_id = find_harvester_id(profile_name, accessor_name)

  Faraday.get do |req|
    req.url "#{@base_url}/services/conf/brokerConfigurations/#{profile_id}/harvesters/#{harvester_id}", { :delete => 'true', :random => generate_random_number }
    req.headers = @authorization_headers.merge({:enctype=>'multipart/form-data'})
  end
end

#delete_all_accessors(profile_name) ⇒ Object

Remove all accessors (xml feed resources) from the given profile



186
187
188
189
190
191
192
193
# File 'lib/gi_cat_driver.rb', line 186

def delete_all_accessors( profile_name )
  profile_id = find_profile_id(profile_name)
  harvesters = get_harvest_resources(profile_id)
  harvesters.each do |harvester_id, harvester_title|
    accessor_name = get_active_profile_accessor_name(profile_id, harvester_id)
    delete_accessor(profile_name, accessor_name)
  end
end

#delete_profile(profile_name) ⇒ Object

Remove a profile with the given name and delete each of the related accessors to clear the associated data



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gi_cat_driver.rb', line 57

def delete_profile( profile_name )
  delete_all_accessors(profile_name)

  profile_id = find_profile_id( profile_name )
  response = Faraday.get do |req|
    req.url "#{@base_url}/services/conf/brokerConfigurations/#{profile_id}", { :opts => 'delete', :random => generate_random_number }
    req.headers = @authorization_headers.merge({'enctype'=>'multipart/form-data'})
  end

  profile_id = response.body
  return profile_id
end

#disable_luceneObject

Disable Lucene indexes for GI-Cat search results



108
109
110
# File 'lib/gi_cat_driver.rb', line 108

def disable_lucene
  set_lucene_enabled false
end

#enable_luceneObject

Enable Lucene indexes for GI-Cat search results



103
104
105
# File 'lib/gi_cat_driver.rb', line 103

def enable_lucene
  set_lucene_enabled true
end

#enable_profile(profile_name) ⇒ Object

Enable a profile with the specified name



95
96
97
98
99
100
# File 'lib/gi_cat_driver.rb', line 95

def enable_profile( profile_name )
  Faraday.get do |req|
    req.url "#{@base_url}/services/conf/brokerConfigurations/#{find_profile_id(profile_name)}?opts=active"
    req.headers = @authorization_headers
  end
end

#find_profile_id(profile_name) ⇒ Object

Retrieve the ID for a profile given the name Returns an integer ID reference to the profile



72
73
74
75
76
77
78
79
80
81
# File 'lib/gi_cat_driver.rb', line 72

def find_profile_id( profile_name )
  response = Faraday.get do |req|
    req.url "#{@base_url}/services/conf/brokerConfigurations", :nameRepository => 'gicat'
    req.headers = @authorization_headers
  end

  profile_id = parse_profile_element(profile_name, response.body)

  return profile_id
end

#get_active_profile_idObject

Returns an integer ID reference to the active profile



84
85
86
87
88
89
90
91
92
# File 'lib/gi_cat_driver.rb', line 84

def get_active_profile_id
  response = Faraday.get do |req|
    req.url "#{@base_url}/services/conf/giconf/configuration"
    req.headers = @standard_headers
  end

  profile_id = response.body
  return profile_id
end

#get_total_results(xml_doc) ⇒ Object

Parse the totalResults element in an OpenSearch ESIP XML document



139
140
141
# File 'lib/gi_cat_driver.rb', line 139

def get_total_results( xml_doc )
  return xml_doc.xpath('//atom:feed/opensearch:totalResults', ATOM_NAMESPACE.merge(OPENSEARCH_NAMESPACE)).text.to_i
end

#harvest_all_resources_for_active_configuration(timeout = 3600) ⇒ Object

Harvest all resource in the active profile The default timeout is 3600 seconds (1 hour)



145
146
147
148
149
150
151
# File 'lib/gi_cat_driver.rb', line 145

def harvest_all_resources_for_active_configuration timeout=3600
  harvestersinfo_array = get_harvest_resources(get_active_profile_id)
  harvestersinfo_array.each do |harvester_id, harvester_title|
    harvest_resource_for_active_configuration(harvester_id.to_s, harvester_title)
    confirm_harvest_done(harvester_id, harvester_title, timeout)
  end
end

#is_lucene_enabled?Boolean

Find out whether Lucene indexing is turned on for the current profile It is desirable to use REST to query GI-Cat for the value of this setting but GI-Cat does not yet support this.

Instead, run a query and check that a ‘relevance:score’ element is present. Returns true if Lucene is turned on

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
# File 'lib/gi_cat_driver.rb', line 129

def is_lucene_enabled?
  results = query_esip_opensearch("arctic%20alaskan%20shrubs")

  result_scores = results.xpath('//atom:feed/atom:entry/relevance:score', ATOM_NAMESPACE.merge(RELEVANCE_NAMESPACE))
  result_scores.map { |score| score.text }

  return result_scores.count > 0
end

#is_running?Boolean

Check whether GI-Cat is accessible

Returns:

  • (Boolean)


39
40
41
# File 'lib/gi_cat_driver.rb', line 39

def is_running?
  Faraday.get(@base_url + "/").status == 200
end

#publish_interface(profile_name, interface_configuration) ⇒ Object

Publish an interface to access GI-Cat data for the given profile name The interface_configuration is a hash that defines a ‘profiler’ and ‘path’



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/gi_cat_driver.rb', line 197

def publish_interface( profile_name, interface_configuration )
  profile_id = find_profile_id(profile_name)

  response = Faraday.post do |req|
    req.url "#{@base_url}/services/conf/brokerConfigurations/#{profile_id}/profilers/"
    req.headers = @authorization_headers.merge({:enctype=>'multipart/form-data', :content_type=>'application/x-www-form-urlencoded'})
    req.body = interface_configuration
  end

  return response.body
end

#query_esip_opensearch(search_term) ⇒ Object

Perform an ESIP OpenSearch query using a search term against GI-Cat and returns a Nokogiri XML document



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gi_cat_driver.rb', line 113

def query_esip_opensearch( search_term )
  query_string = EsipOpensearchQueryBuilder::get_query_string({ :st => search_term })

  begin
    file = open("#{@base_url}/services/opensearchesip#{query_string}")
  rescue => e
    raise "Failed to query GI-Cat ESIP OpenSearch interface."
  end

  return Nokogiri::XML(file)
end

#unpublish_interface(profile_name, interface_name) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/gi_cat_driver.rb', line 209

def unpublish_interface( profile_name, interface_name )
  profile_id = find_profile_id(profile_name)

  Faraday.get do |req|
    req.url "#{@base_url}/services/conf/brokerConfigurations/#{profile_id}/profilers/#{interface_name}", { :delete => 'true', :random => generate_random_number }
    req.headers = @authorization_headers.merge({:enctype=>'multipart/form-data', :content_type=>'application/x-www-form-urlencoded'})
  end
end