gsa-feeds 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2007-11-16
2
+
3
+ * Initial release
4
+ * worksforme
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Jesse Newland
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.
@@ -0,0 +1,22 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ config/hoe.rb
7
+ config/requirements.rb
8
+ lib/gsa_feeds.rb
9
+ lib/multipart.rb
10
+ lib/gsa_feeds/version.rb
11
+ lib/gsa_feeds/base.rb
12
+ lib/gsa_feeds/record.rb
13
+ log/debug.log
14
+ script/destroy
15
+ script/generate
16
+ script/txt2html
17
+ setup.rb
18
+ tasks/deployment.rake
19
+ tasks/environment.rake
20
+ tasks/website.rake
21
+ test/test_gsa_feeds.rb
22
+ test/test_helper.rb
@@ -0,0 +1,4 @@
1
+ GSA Feeds
2
+ =========
3
+
4
+ A Ruby wrapper for the Google Search Appliance Feeds Protocol. This protocol exists to push content and metadata to the search appliance for processing, indexing, and serving as search results. Feeds are available in GSA software versions 4.2 and later.
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,71 @@
1
+ require 'gsa_feeds/version'
2
+
3
+ AUTHOR = 'Jesse Newlnad' # can also be an array of Authors
4
+ EMAIL = "jnewland@gmail.com"
5
+ DESCRIPTION = "Wrap the Google Search Appliance Feeds API"
6
+ GEM_NAME = 'gsa-feeds' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'gsa-feeds' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "jnewland"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+
31
+ REV = nil
32
+ # UNCOMMENT IF REQUIRED:
33
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34
+ VERS = GsaFeeds::VERSION::STRING + (REV ? ".#{REV}" : "")
35
+ RDOC_OPTS = ['--quiet', '--title', 'gsa_feeds documentation',
36
+ "--opname", "index.html",
37
+ "--line-numbers",
38
+ "--main", "README",
39
+ "--inline-source"]
40
+
41
+ class Hoe
42
+ def extra_deps
43
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44
+ @extra_deps
45
+ end
46
+ end
47
+
48
+ # Generate all the Rake tasks
49
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
50
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
51
+ p.author = AUTHOR
52
+ p.description = DESCRIPTION
53
+ p.email = EMAIL
54
+ p.summary = DESCRIPTION
55
+ p.url = HOMEPATH
56
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
57
+ p.test_globs = ["test/**/test_*.rb"]
58
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
59
+
60
+ # == Optional
61
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
62
+ p.extra_deps = [['builder', '>= 2.1.1']] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
63
+
64
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
65
+
66
+ end
67
+
68
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
69
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
70
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
71
+ hoe.rsync_args = '-av --delete --ignore-errors'
@@ -0,0 +1,17 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
16
+
17
+ require 'gsa_feeds'
@@ -0,0 +1,8 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'multipart'
3
+ require 'builder'
4
+ module GsaFeeds
5
+ TIMEOUT = 60
6
+ end
7
+ require 'gsa_feeds/base'
8
+ require 'gsa_feeds/record'
@@ -0,0 +1,95 @@
1
+ class GsaFeeds::Base
2
+ def initialize(hostname, datasource = 'web', feedtype = 'incremental', timeout = false)
3
+ @hostname = hostname
4
+ @timeout = timeout || GsaFeeds::TIMEOUT
5
+ @records = []
6
+ @datasource = datasource
7
+ @feedtype = feedtype
8
+ end
9
+ attr_accessor :records, :datasource, :feedtype, :timeout, :hostname
10
+ alias_method :to_a, :records
11
+
12
+ def clear!
13
+ @records = []
14
+ end
15
+
16
+ def add_record(record)
17
+ @records << record
18
+ end
19
+
20
+ def build_record(url, options = {}, metadata = nil, content = nil)
21
+ @records << GsaFeeds::Record.new(url, options, metadata, content)
22
+ end
23
+
24
+ def commit!
25
+ push_records(@records)
26
+ clear!
27
+ true
28
+ end
29
+
30
+ def to_xml
31
+ build_xml(@records)
32
+ end
33
+
34
+ private
35
+
36
+ # def post_form(query, headers, url = "http://#{@hostname}:19900/xmlfeed")
37
+ # url = URI.parse(url)
38
+ # Net::HTTP.start(url.host, url.port) do |con|
39
+ # con.read_timeout = @timeout
40
+ # return con.post(url.path, query, headers)
41
+ # end
42
+ # end
43
+
44
+ def push_records(records)
45
+ params = Hash.new
46
+
47
+ params["data"] = build_xml(records)
48
+ params["datasource"] = @datasource
49
+ params["feedtype"] = @feedtype
50
+
51
+ url = URI.parse("http://#{@hostname}:19900/xmlfeed")
52
+ res = Net::HTTP.multi_post_form(url, params)
53
+
54
+ # res holds the response to the POST
55
+ case res
56
+ when Net::HTTPSuccess
57
+ return true
58
+ else
59
+ raise "Unknown error #{res}: #{res.inspect}\n#{res.body}"
60
+ end
61
+ end
62
+
63
+ def build_xml(records)
64
+ string = ''
65
+ builder = Builder::XmlMarkup.new(:target=>string, :indent=>2)
66
+ builder.instruct!
67
+ builder.declare! :DOCTYPE, :gsafeed, :PUBLIC, "-//Google//DTD GSA Feeds//EN", ""
68
+ builder.gsafeed do |feed|
69
+ feed.header do |header|
70
+ header.datasource @datasource
71
+ header.feedtype @feedtype
72
+ end
73
+ feed.group do |r|
74
+ records.each do |record_data|
75
+ if (!record_data.content.nil? || !record_data.metadata.nil?)
76
+ r.record(record_data.to_h) do |record_details|
77
+ unless record_data.metadata.nil?
78
+ record_details.metadata do |meta_tags|
79
+ record_data.metadata.each do |meta_info|
80
+ meta_tags.meta(meta_info)
81
+ end
82
+ end
83
+ end
84
+ record_details.content record_data.content unless record_data.content.nil?
85
+ end
86
+ else
87
+ r.record(record_data.to_h)
88
+ end
89
+ end
90
+ end
91
+ end
92
+ string
93
+ end
94
+
95
+ end
@@ -0,0 +1,18 @@
1
+ class GsaFeeds::Record
2
+ def initialize(url, options = {}, metadata = nil, content = nil)
3
+ options = {:mimetype => 'text/html'}.merge(options)
4
+ @hash = options.merge({:url => url})
5
+ @metadata = metadata
6
+ @content = content
7
+ end
8
+ attr_accessor :hash, :metadata, :content
9
+
10
+ #automap the metadata hash into an array name / content pairs
11
+ def metadata
12
+ @metadata.map { |key, value| {:name => key, :content => value } }
13
+ end
14
+
15
+ def to_h
16
+ @hash
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module GsaFeeds #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,47 @@
1
+ #kastner is effing awesome. that is all.
2
+ class Net::HTTP
3
+ def self.multi_post_form(url, params)
4
+ req = Post.new(url.path)
5
+ req.multipart_params = params
6
+ req.basic_auth url.user, url.password if url.user
7
+ new(url.host, url.port).start {|http|
8
+ http.request(req)
9
+ }
10
+ end
11
+ end
12
+
13
+ module Net
14
+ class HTTP
15
+ class Post
16
+ def multipart_params=(param_hash={})
17
+ boundary_token = [Array.new(8) {rand(256)}].join
18
+ self.content_type = "multipart/form-data; boundary=#{boundary_token}"
19
+ boundary_marker = "--#{boundary_token}\r\n"
20
+
21
+ self.body = param_hash.map { |param_name, param_value|
22
+ boundary_marker + text_to_multipart(param_name, param_hash.delete(param_name).to_s) unless param_value.respond_to?(:read)
23
+ }.join('')
24
+
25
+ self.body += param_hash.map { |param_name, param_value|
26
+ boundary_marker + file_to_multipart(param_name, param_value)
27
+ }.join('')
28
+
29
+ self.body += "--#{boundary_token}--\r\n"
30
+ end
31
+
32
+ protected
33
+
34
+ def file_to_multipart(key,file)
35
+ filename = File.basename(file.path || file.base_uri.to_s)
36
+ mime_type = "application/octet-stream"
37
+ part = %Q|Content-Disposition: form-data; name=\"#{key}\"; filename="#{filename}"\r\n|
38
+ part += "Content-Transfer-Encoding: binary\r\n"
39
+ part += "Content-Type: #{mime_type}\r\n\r\n#{file.read}\r\n"
40
+ end
41
+
42
+ def text_to_multipart(key,value)
43
+ "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n#{value}\r\n"
44
+ end
45
+ end
46
+ end
47
+ end
File without changes
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.join(File.dirname(__FILE__), '..')
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.join(File.dirname(__FILE__), '..')
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ begin
5
+ require 'newgem'
6
+ rescue LoadError
7
+ puts "\n\nGenerating the website requires the newgem RubyGem"
8
+ puts "Install: gem install newgem\n\n"
9
+ exit(1)
10
+ end
11
+ require 'redcloth'
12
+ require 'syntax/convertors/html'
13
+ require 'erb'
14
+ require File.dirname(__FILE__) + '/../lib/gsa_feeds/version.rb'
15
+
16
+ version = GsaFeeds::VERSION::STRING
17
+ download = 'http://rubyforge.org/projects/gsa_feeds'
18
+
19
+ class Fixnum
20
+ def ordinal
21
+ # teens
22
+ return 'th' if (10..19).include?(self % 100)
23
+ # others
24
+ case self % 10
25
+ when 1: return 'st'
26
+ when 2: return 'nd'
27
+ when 3: return 'rd'
28
+ else return 'th'
29
+ end
30
+ end
31
+ end
32
+
33
+ class Time
34
+ def pretty
35
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
36
+ end
37
+ end
38
+
39
+ def convert_syntax(syntax, source)
40
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
41
+ end
42
+
43
+ if ARGV.length >= 1
44
+ src, template = ARGV
45
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.rhtml')
46
+
47
+ else
48
+ puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
49
+ exit!
50
+ end
51
+
52
+ template = ERB.new(File.open(template).read)
53
+
54
+ title = nil
55
+ body = nil
56
+ File.open(src) do |fsrc|
57
+ title_text = fsrc.readline
58
+ body_text = fsrc.read
59
+ syntax_items = []
60
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
61
+ ident = syntax_items.length
62
+ element, syntax, source = $1, $2, $3
63
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
64
+ "syntax-temp-#{ident}"
65
+ }
66
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
67
+ body = RedCloth.new(body_text).to_html
68
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
69
+ end
70
+ stat = File.stat(src)
71
+ created = stat.ctime
72
+ modified = stat.mtime
73
+
74
+ $stdout << template.result(binding)