gi_cat_driver 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.3-p374@gi_cat_driver"
8
+
9
+ #
10
+ # Uncomment the following lines if you want to verify rvm version per project
11
+ #
12
+ # rvmrc_rvm_version="1.10.2" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+ #
18
+
19
+ #
20
+ # Uncomment following line if you want options to be set only for given project.
21
+ #
22
+ # PROJECT_JRUBY_OPTS=( --1.9 )
23
+ #
24
+ # The variable PROJECT_JRUBY_OPTS requires the following to be run in shell:
25
+ #
26
+ # chmod +x ${rvm_path}/hooks/after_use_jruby_opts
27
+ #
28
+
29
+ #
30
+ # First we attempt to load the desired environment directly from the environment
31
+ # file. This is very fast and efficient compared to running through the entire
32
+ # CLI and selector. If you want feedback on which environment was used then
33
+ # insert the word 'use' after --create as this triggers verbose mode.
34
+ #
35
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
36
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
37
+ then
38
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
39
+
40
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
41
+ then
42
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
43
+ fi
44
+ else
45
+ # If the environment file has not yet been created, use the RVM CLI to select.
46
+ if ! rvm --create use "$environment_id"
47
+ then
48
+ echo "Failed to create RVM environment '${environment_id}'."
49
+ return 1
50
+ fi
51
+ fi
52
+
53
+ #
54
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
55
+ # it be automatically loaded. Uncomment the following and adjust the filename if
56
+ # necessary.
57
+ #
58
+ # filename=".gems"
59
+ # if [[ -s "$filename" ]]
60
+ # then
61
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
62
+ # fi
63
+
64
+ # If you use bundler, this might be useful to you:
65
+ # if [[ -s Gemfile ]] && ! command -v bundle >/dev/null
66
+ # then
67
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
68
+ # gem install bundler
69
+ # fi
70
+ # if [[ -s Gemfile ]] && command -v bundle
71
+ # then
72
+ # bundle install
73
+ # fi
74
+
75
+ if [[ $- == *i* ]] # check for interactive shells
76
+ then
77
+ echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
78
+ else
79
+ echo "Using: $GEM_HOME" # don't use colors in interactive shells
80
+ fi
81
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gi_cat_driver.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Stuart Reed
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # GI-Cat Driver
2
+
3
+ The GI-Cat driver is a ruby wrapper for GI-Cat services. Remotely configure administration options for a GI-Cat instance.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gi_cat_driver'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gi_cat_driver
18
+
19
+ ## Usage
20
+
21
+ To start using the gem create a new instance
22
+
23
+ ```ruby
24
+ gi_cat = GiCatDriver::GiCat.new('http://www.company.com/api/gi-cat', 'admin', 'password')
25
+ ```
26
+ Please note you must provide a URL to a running GI-Cat instance as well as the administrator username and password.
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new('spec')
5
+ task :default => :spec # to make it default
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gi_cat_driver/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gi_cat_driver"
8
+ spec.version = GiCatDriver::VERSION
9
+ spec.authors = ["Stuart Reed"]
10
+ spec.email = ["stuart.reed@nsidc.org"]
11
+ spec.description = %q{Configure and control deployed instances of GI-Cat.}
12
+ spec.summary = %q{Configure and control deployed instances of GI-Cat.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rest-client"
22
+ spec.add_dependency "nokogiri"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,29 @@
1
+
2
+ # The EsipOpensearchQueryBuilder uses a hash of relevant key/value pairs to construct a string of parameters for the EsipOpensearchService
3
+ module EsipOpensearchQueryBuilder
4
+
5
+ class QueryBuilder
6
+ def assemble_query(params)
7
+ return "?" + params.collect{ |k, v| "#{k}=#{v}" }.join("&")
8
+ end
9
+ end
10
+
11
+ def self.get_query_string( query_params={} )
12
+ all_params = {
13
+ :si => '',
14
+ :ct => '',
15
+ :st => '',
16
+ :bbox => '',
17
+ :rel => '',
18
+ :loc => '',
19
+ :ts => '',
20
+ :te => '',
21
+ :lac => '',
22
+ :luc => '',
23
+ :outputFormat => ''
24
+ }.merge(query_params)
25
+
26
+ builder = QueryBuilder.new()
27
+ return builder.assemble_query(all_params)
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module GiCatDriver
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,97 @@
1
+ require "gi_cat_driver/version"
2
+ require "esip_opensearch_query_builder"
3
+ require "open-uri"
4
+ require "rest-client"
5
+ require "base64"
6
+ require 'nokogiri'
7
+
8
+ module GiCatDriver
9
+ class GiCat
10
+
11
+ ATOM_NAMESPACE = { "atom" => "http://www.w3.org/2005/Atom" }
12
+ RELEVANCE_NAMESPACE = { "relevance" => "http://a9.com/-/opensearch/extensions/relevance/1.0/" }
13
+
14
+ def initialize( url, username, password )
15
+ @base_url = url.sub(/\/+$/, '') # strip trailing slashes
16
+
17
+ @admin_username = username
18
+ @admin_password = password
19
+ end
20
+
21
+ def base_url
22
+ @base_url
23
+ end
24
+
25
+ def basic_auth_string
26
+ "Basic " + Base64.encode64("#{@admin_username}:#{@admin_password}").rstrip
27
+ end
28
+
29
+ def standard_headers
30
+ {
31
+ :content_type => "application/xml",
32
+ :Authorization => basic_auth_string
33
+ }
34
+ end
35
+
36
+ def is_running?
37
+ open(@base_url).status[0] == "200"
38
+ end
39
+
40
+ def find_profile_id( profile_name )
41
+ get_profiles_request = "#{@base_url}/services/conf/brokerConfigurations?nameRepository=gicat"
42
+ modified_headers = standard_headers.merge({
43
+ :content_type => "*/*",
44
+ :Accept => 'application/xml'
45
+ })
46
+ xml_doc = RestClient.get(get_profiles_request, modified_headers)
47
+ profile = parse_profile_element(profile_name, xml_doc)
48
+
49
+ return (profile.empty? ? nil : profile.attr('id').value)
50
+ end
51
+
52
+ def parse_profile_element( profile_name, xml_doc )
53
+ configs = Nokogiri.XML(xml_doc)
54
+
55
+ return configs.css("brokerConfiguration[name=#{profile_name}]")
56
+ end
57
+
58
+ def enable_profile( profile_name )
59
+ profile_id = find_profile_id(profile_name)
60
+ raise "The specified profile could not be found." if profile_id.nil?
61
+ activate_profile_request = "#{@base_url}/services/conf/brokerConfigurations/#{profile_id}?opts=active"
62
+
63
+ RestClient.get(activate_profile_request, standard_headers)
64
+ end
65
+
66
+ def get_active_profile_id
67
+ active_profile_request = "#{@base_url}/services/conf/giconf/configuration"
68
+
69
+ return RestClient.get(active_profile_request, standard_headers)
70
+ end
71
+
72
+ def enable_lucene
73
+ set_lucene_enabled true
74
+ end
75
+
76
+ def set_lucene_enabled( enabled )
77
+ enable_lucene_request = "#{@base_url}/services/conf/brokerConfigurations/#{get_active_profile_id}/luceneEnabled"
78
+ RestClient.put(enable_lucene_request,
79
+ enabled.to_s,
80
+ standard_headers)
81
+
82
+ activate_profile_request = "#{@base_url}/services/conf/brokerConfigurations/#{get_active_profile_id}?opts=active"
83
+ RestClient.get(activate_profile_request,
84
+ standard_headers)
85
+ end
86
+
87
+ def is_lucene_enabled?
88
+ query_string = EsipOpensearchQueryBuilder::get_query_string({ :st => "snow" })
89
+ results = Nokogiri::XML(open("#{@base_url}/services/opensearchesip#{query_string}"))
90
+
91
+ result_scores = results.xpath('//atom:feed/atom:entry/relevance:score', ATOM_NAMESPACE.merge(RELEVANCE_NAMESPACE))
92
+ result_scores.map { |score| score.text }
93
+
94
+ return result_scores.count > 0
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ require 'esip_opensearch_query_builder'
3
+
4
+ describe EsipOpensearchQueryBuilder do
5
+
6
+ describe "get_query_string returns ESIP OpenSearch URLs" do
7
+ it "Returns a URL with empty parameters when called without arguments" do
8
+ query = EsipOpensearchQueryBuilder::get_query_string()
9
+ query.should eq "?si=&ct=&st=&bbox=&rel=&loc=&ts=&te=&lac=&luc=&outputFormat="
10
+ end
11
+
12
+ it "Returns a URL with a bounding box when called with a bbox argument" do
13
+ query = EsipOpensearchQueryBuilder::get_query_string( :bbox => 'abcd')
14
+ query.should eq "?si=&ct=&st=&bbox=abcd&rel=&loc=&ts=&te=&lac=&luc=&outputFormat="
15
+ end
16
+
17
+ it "Returns a URL with a search term when called with a st argument" do
18
+ query = EsipOpensearchQueryBuilder::get_query_string( :st => 'snow')
19
+ query.should eq "?si=&ct=&st=snow&bbox=&rel=&loc=&ts=&te=&lac=&luc=&outputFormat="
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ require 'gi_cat_driver'
3
+
4
+ describe GiCatDriver do
5
+ before(:each) do
6
+ @base_url = ENV["URL"] or throw "Error: Must provide URL environment variable for GI-Cat!"
7
+ @gi_cat = GiCatDriver::GiCat.new(@base_url, "admin", "abcd123$")
8
+ end
9
+
10
+ it "Initializes with a base url that points to an instance of GI-Cat" do
11
+ expected = @base_url.sub(/\/+$/, '')
12
+ @gi_cat.base_url.should eq(expected)
13
+ end
14
+
15
+ describe "Standard requests" do
16
+ it "Is able to send requests to GI-Cat" do
17
+ @gi_cat.is_running?.should be_true
18
+ end
19
+
20
+ it "Sends requests with basic header information" do
21
+ @gi_cat.standard_headers[:content_type].should eq "application/xml"
22
+ end
23
+
24
+ it "Can authorize access using the Authorization header" do
25
+ @gi_cat.standard_headers[:Authorization].should eq "Basic YWRtaW46YWJjZDEyMyQ="
26
+ end
27
+ end
28
+
29
+ describe "Profile management" do
30
+ it "Retrieves a profile id given the name" do
31
+ @gi_cat.find_profile_id("NORWEGIAN_CISL_NSIDC_EOL").should eq "1"
32
+ end
33
+
34
+ it "Throws an error if the profile cannot be found" do
35
+ @gi_cat.find_profile_id("notaprofile").should be_nil
36
+ end
37
+
38
+ it "Enables a profile given the name" do
39
+ @gi_cat.enable_profile("NORWEGIAN_CISL_NSIDC_EOL")
40
+ @gi_cat.get_active_profile_id.should eq "1"
41
+ end
42
+
43
+ it "Does not enable a profile if the profile cannot be found" do
44
+ expect { @gi_cat.enable_profile("notaprofile") }.to raise_error("The specified profile could not be found.")
45
+ end
46
+ end
47
+
48
+ describe "Lucene Indexing" do
49
+ it "Enables Lucene for the active profile" do
50
+ @gi_cat.enable_lucene
51
+ @gi_cat.is_lucene_enabled?.should be_true
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gi_cat_driver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stuart Reed
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: nokogiri
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Configure and control deployed instances of GI-Cat.
95
+ email:
96
+ - stuart.reed@nsidc.org
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - .rvmrc
104
+ - Gemfile
105
+ - LICENSE.txt
106
+ - README.md
107
+ - Rakefile
108
+ - gi_cat_driver.gemspec
109
+ - lib/esip_opensearch_query_builder.rb
110
+ - lib/gi_cat_driver.rb
111
+ - lib/gi_cat_driver/version.rb
112
+ - spec/esip_opensearch_query_builder_spec.rb
113
+ - spec/gi_cat_driver_spec.rb
114
+ - spec/spec_helper.rb
115
+ homepage: ''
116
+ licenses:
117
+ - MIT
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ segments:
129
+ - 0
130
+ hash: -2265981651223281896
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ segments:
138
+ - 0
139
+ hash: -2265981651223281896
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 1.8.24
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: Configure and control deployed instances of GI-Cat.
146
+ test_files:
147
+ - spec/esip_opensearch_query_builder_spec.rb
148
+ - spec/gi_cat_driver_spec.rb
149
+ - spec/spec_helper.rb