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/" }
STANDARD_HEADERS =
{ :content_type => "application/xml" }
AUTHORIZATION_HEADERS =
{ :content_type => "*/*", :Accept => "application/xml", :Authorization => self.basic_auth_string }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, username, password) ⇒ GiCat

Returns a new instance of GiCat.



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

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

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



25
26
27
# File 'lib/gi_cat_driver.rb', line 25

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



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

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



172
173
174
175
176
177
178
179
180
# File 'lib/gi_cat_driver.rb', line 172

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_profile(profile_name) ⇒ Object

Remove a profile with the given name



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

def delete_profile( 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



105
106
107
# File 'lib/gi_cat_driver.rb', line 105

def disable_lucene
  set_lucene_enabled false
end

#enable_luceneObject

Enable Lucene indexes for GI-Cat search results



100
101
102
# File 'lib/gi_cat_driver.rb', line 100

def enable_lucene
  set_lucene_enabled true
end

#enable_profile(profile_name) ⇒ Object

Enable a profile with the specified name



92
93
94
95
96
97
# File 'lib/gi_cat_driver.rb', line 92

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



69
70
71
72
73
74
75
76
77
78
# File 'lib/gi_cat_driver.rb', line 69

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



81
82
83
84
85
86
87
88
89
# File 'lib/gi_cat_driver.rb', line 81

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



136
137
138
# File 'lib/gi_cat_driver.rb', line 136

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 = 1500) ⇒ Object

Harvest all resource in the active profile The default timeout is 1500 seconds (25 minutes)



142
143
144
145
146
147
148
# File 'lib/gi_cat_driver.rb', line 142

def harvest_all_resources_for_active_configuration timeout=1500
  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)


126
127
128
129
130
131
132
133
# File 'lib/gi_cat_driver.rb', line 126

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’



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

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



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/gi_cat_driver.rb', line 110

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



196
197
198
199
200
201
202
203
# File 'lib/gi_cat_driver.rb', line 196

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