rails-gsa 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +24 -0
  3. data/lib/rails-gsa.rb +154 -0
  4. metadata +81 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Rohit Sharma - rohit0981989@gmail.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,24 @@
1
+ == RailsGSA
2
+
3
+ Rails gem for integrating GSA(Google Search Appliance) with your rails application.
4
+
5
+ == Usage
6
+
7
+ Suppose you have access to a GSA with url "http://testgsa.com", and you want to search for a query string "hello". You can do this using 2 methods:
8
+ 1) Send request to the XML API of GSA(XML Response Object)
9
+ 2) Send request to the Cluster API of GSA(JSON Response Object)
10
+
11
+ If we follow the first way, we need to do the following wherever required:
12
+
13
+ RailsGSA.search(:gsa_url => "http://testgsa.com", :search_term => "hello", :output => "xml")
14
+
15
+ The above line of code does the following
16
+ 1) Sends a request to the GSA with the specified search term.
17
+ 2) Parse the XML response given by the GSA.
18
+ 3) Create and return a Hash object of the resultset generated after parsing the XML response.
19
+
20
+ For the second method, you just need to change the value of the :output parameter to "json", as below:
21
+
22
+ RailsGSA.search(:gsa_url => "http://testgsa.com", :search_term => "hello", :output => "json")
23
+
24
+ This will also return you a Hash object which is created by parsing the JSON response of the GSA.
data/lib/rails-gsa.rb ADDED
@@ -0,0 +1,154 @@
1
+ require 'http_requestor'
2
+ require 'nokogiri'
3
+ require 'json'
4
+
5
+
6
+ module RailsGSA
7
+
8
+ class NoInternetConnectionError < Exception; end
9
+ def self.default_options
10
+ @default_options ||= {:gsa_url => "",
11
+ :search_term => "",
12
+ :output => "json",
13
+ :access => "p",
14
+ :client => "default_frontend",
15
+ :proxystylesheet => "default_frontend",
16
+ :site => "default_collection",
17
+ :start => 0,
18
+ :num => 10
19
+ }
20
+ end
21
+
22
+ def self.search(args = {})
23
+ default_options
24
+ @default_options.merge!(args)
25
+ raise ArgumentError, "GSA URL missing. Please provide valid arguments." if @default_options[:gsa_url].empty? || @default_options[:gsa_url].nil?
26
+ return perform_search
27
+ end
28
+
29
+ protected
30
+ def self.perform_search
31
+ @http = HTTP::Requestor.new(@default_options[:gsa_url])
32
+ if @default_options[:output] == "json"
33
+ json_response = @http.post(json_search_url).body
34
+ response_object = JSON.parse(json_response)
35
+ return ((response_object.empty? || response_object.nil?) ? {} : response_object)
36
+ elsif @default_options[:output] == "xml"
37
+ return xml_parsed_to_search_results(xml_search_url)
38
+ end
39
+ end
40
+
41
+ def self.json_search_url
42
+ url = URI.escape("/cluster?q=#{@default_options[:search_term]}&coutput=json&" +
43
+ "access=#{@default_options[:access]}&output=xml_no_dtd&client=#{@default_options[:client]}&proxystylesheet=#{@default_options[:proxystylesheet]}&" +
44
+ "site=#{@default_options[:site]}&start=#{@default_options[:start]}&num=#{@default_options[:num]}"
45
+ )
46
+ return url
47
+ end
48
+
49
+ def self.xml_search_url
50
+ url = URI.escape("/search?q=#{@default_options[:search_term]}&output=xml&client=#{@default_options[:client]}&" +
51
+ "start=#{@default_options[:start]}&num=#{@default_options[:num]}&filter=0"
52
+ )
53
+ return url
54
+ end
55
+
56
+ def self.xml_parsed_to_search_results(url)
57
+ if url.include?("cache")
58
+ new_url = url+"&proxystylesheet=my_frontend"
59
+ return {:cached_page => @http.get(new_url).body}
60
+ else
61
+ new_output = ""
62
+ output = @http.get(url).body
63
+ output.each_line{|line| new_output += line.chop}
64
+ doc = Nokogiri::XML(new_output)
65
+ search_result_nodes = doc.xpath('//GSP/RES')
66
+
67
+ all_params = {}
68
+ results = {}
69
+ results[:actual_results] = {}
70
+
71
+ doc.xpath('//GSP/PARAM').each do |p|
72
+ case p['name']
73
+ when 'q'
74
+ all_params[:query] = p['value']
75
+ when 'site'
76
+ all_params[:site] = p['value']
77
+ when 'client'
78
+ all_params[:client] = p['value']
79
+ when 'output'
80
+ all_params[:output] = p['value']
81
+ when 'start'
82
+ all_params[:start] = p['value']
83
+ end
84
+ end
85
+
86
+ results[:all_params] = all_params
87
+ results[:filtered] = false
88
+ results[:total_results] = 0
89
+ results[:search_time] = doc.xpath('//GSP/TM')[0].text
90
+
91
+ unless doc.xpath('//GSP/RES').empty? || doc.xpath('//GSP/RES').nil?
92
+ results[:from] = doc.xpath('//GSP/RES')[0]['SN']
93
+ results[:to] = doc.xpath('//GSP/RES')[0]['EN']
94
+ end
95
+
96
+ unless search_result_nodes.empty? || search_result_nodes.nil?
97
+ index = 0
98
+ search_result_nodes.children.each do |child|
99
+ case child.name
100
+ when 'M'
101
+ results[:total_results] = child.text
102
+ when 'NB'
103
+ results[:top_nav] = {}
104
+ child.children.each do |top_nav|
105
+ case top_nav.name
106
+ when 'PU'
107
+ results[:top_nav][:previous] = top_nav.text
108
+ when 'NU'
109
+ results[:top_nav][:next] = top_nav.text
110
+ end
111
+ end
112
+ when 'R'
113
+ key = "result_#{index}".to_sym
114
+ index+=1
115
+ results[:actual_results][key] = {}
116
+ results[:actual_results][key][:indented] = (child['L'].to_i > 1)
117
+ child.children.each do |ar|
118
+ case ar.name
119
+ when 'U'
120
+ results[:actual_results][key][:display_link] = ar.text
121
+ when 'UE'
122
+ results[:actual_results][key][:link_for_title] = ar.text
123
+ when 'T'
124
+ results[:actual_results][key][:title] = ar.text
125
+ when 'FS'
126
+ results[:actual_results][key][:date] = ar['VALUE']
127
+ when 'S'
128
+ results[:actual_results][key][:description] = ar.text
129
+ when 'HAS'
130
+ ar.children.each do |c|
131
+ case c.name
132
+ when 'C'
133
+ add_to_query = "#{c['CID']}:#{results[:actual_results][key][:link_for_title]}"
134
+ final_cached_url = "#{@default_options[:gsa_url]}/search?q=cache:#{add_to_query}+#{all_params[:query]}&site=#{all_params[:site]}&client=#{all_params[:client]}&output=#{all_params[:output]}&proxystylesheet=my_frontend"
135
+ results[:actual_results][key][:cache_link] = final_cached_url
136
+ results[:actual_results][key][:size] = c['SZ']
137
+ end
138
+ end
139
+ when 'HN'
140
+ results[:actual_results][key][:more_results] = {}
141
+ results[:actual_results][key][:more_results][:text] = "More Results from #{ar.text}"
142
+ sitesearch_url = "/search?q=#{all_params[:query]}&site=#{all_params[:site]}&client=#{all_params[:client]}&output=#{all_params[:output]}&as_sitesearch=#{ar.text}"
143
+ results[:actual_results][key][:more_results][:link] = "#{root_url.chop}#{sitesearch_url}"
144
+ end
145
+ end
146
+ when 'FI'
147
+ results[:filtered] = true
148
+ end
149
+ end
150
+ end
151
+ return results
152
+ end
153
+ end
154
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-gsa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rohit Sharma
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-23 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: http-requestor
16
+ requirement: &29919252 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *29919252
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &29767380 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.7.5
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *29767380
36
+ - !ruby/object:Gem::Dependency
37
+ name: nokogiri
38
+ requirement: &29767104 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 1.5.5
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *29767104
47
+ description: Integrate GSA(Google Search Appliance) with your ruby/rails application.
48
+ email: rohit0981989@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/rails-gsa.rb
54
+ - MIT-LICENSE
55
+ - README.rdoc
56
+ homepage: http://github.com/rohit9889/rails-gsa
57
+ licenses:
58
+ - MIT
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.5
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Integrate GSA(Google Search Appliance) with your ruby/rails application.
81
+ test_files: []