Module: Aef::LaunchyOpenSearch

Defined in:
lib/launchy_opensearch/launchy_opensearch.rb

Overview

Offers static methods for all steps in parsing usefull information out of the OpenSearch XML format and modifying the configuration of Launchy’s Weby plugin

Constant Summary collapse

VERSION =
'1.2.1'

Class Method Summary collapse

Class Method Details

.extract_config_hash(config_hash) ⇒ Object

Reads relevant parts out of the weby section of a launchy config hash.

Returns an Array of Hashes with the keys :name, :base, :query and :default



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/launchy_opensearch/launchy_opensearch.rb', line 111

def self.extract_config_hash(config_hash)
  engines = {}
  config_hash['weby'].each do |entry, value|
    entry.match(/^sites\\([0-9]{1,2})\\(.*)$/)
    if $1 and $2
      engines[$1.to_i] ||= {}
      engines[$1.to_i][$2.to_sym] = value
    end
  end
  
  engines_array = []
  engines.sort.each do |key, content|
    engines_array << content
  end
  engines_array
end

.launchy_config_pathObject

Determines the location of Launchy’s configuration Ini file on different platforms



34
35
36
37
38
39
40
# File 'lib/launchy_opensearch/launchy_opensearch.rb', line 34

def self.launchy_config_path
  if Sys::Uname.sysname.downcase.include?("windows")
    File.join(ENV['APPDATA'], 'Launchy', 'Launchy.ini')
  else
    File.join(ENV['HOME'], '.launchy', 'launchy.ini')
  end
end

.parse_opensearch(content) ⇒ Object

Parses an OpenSearch XML document

Returns a Hash with the keys :name, :base, :query and :default



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/launchy_opensearch/launchy_opensearch.rb', line 45

def self.parse_opensearch(content)
  opensearch = Hpricot.XML(content)

  uri = URI.parse(opensearch.at('os:Url').get_attribute('template').gsub(/\{searchTerms\}/, ':PLACEHOLDER'))

  {
    :name => opensearch.at('os:ShortName').inner_html,
    :base => "#{uri.scheme}://#{uri.host}/",
    :query => "\"#{uri.path[1..-1]}?#{uri.query}\"".gsub(/:PLACEHOLDER/, '%s'),
    :default => 'false'
  }
end

.parse_opensearch_file(file) ⇒ Object

Reads and parses a single OpenSearch file from the filesystem.

Returns a Hash with the keys :name, :base, :query and :default



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/launchy_opensearch/launchy_opensearch.rb', line 61

def self.parse_opensearch_file(file)
  if file.is_a?(String)
    content = File.read(file)
  elsif file.respond_to?(:read)
    content = file.read
  else
    raise "Expected file path as string or an object responding to read. Got #{file.class.name}"
  end
  
  parse_opensearch(content)
end

.parse_opensearch_files(file_list) ⇒ Object

Reads multiple OpenSearch files from filesystem

Returns an Array of Hashes with the keys :name, :base, :query and :default



76
77
78
79
80
81
82
83
84
# File 'lib/launchy_opensearch/launchy_opensearch.rb', line 76

def self.parse_opensearch_files(file_list)
  engines = []
  file_list.each do |file|
    if File.readable?(file)
      engines << parse_opensearch_file(file)
    end
  end
  engines
end

.patch_config_hash(config_hash, engines) ⇒ Object

Replaces the site entries of the Weby section of a launchy config hash with an engines array as returned by extract_config_hash

Attention: This method modifies the config hash given as argument. It is returned as result only for convenience.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/launchy_opensearch/launchy_opensearch.rb', line 133

def self.patch_config_hash(config_hash, engines)
  new_section = {}
  config_hash['weby'].each do |entry, value|
    unless entry.match(/^sites\\/)
      new_section[entry] = value
    end
  end
  engines.each_with_index do |settings, i|
    settings.each {|key, value|
      new_section["sites\\#{i + 1}\\#{key}"] = value
    }
  end
  new_section['sites\\size'] = engines.size.to_s
  config_hash['weby'] = new_section
  config_hash
end

.read_config_hash(path) ⇒ Object

Reads an Ini file from filesystem into a launchy config hash.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/launchy_opensearch/launchy_opensearch.rb', line 87

def self.read_config_hash(path)
  # Ini class doesn't like empty lines and can only read from files.
  original = File.read(path)

  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('1.9')
    cleaned = original.lines.map{|line| line.chomp.squeeze(' ')}.reject{|line| line == ''}.join("\n")
  else
    cleaned = original.map{|line| line.chomp.squeeze(' ')}.reject{|line| line == ''}.join("\n")
  end

  temp_file = Tempfile.open('launchy')
  temp_file.write(cleaned)
  temp_file.close

  config = Ini.read_from_file(temp_file.path)

  temp_file.unlink

  config
end

.write_config_hash(config_hash, path) ⇒ Object

Writes a launchy config hash back to filesystem in INI format



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

def self.write_config_hash(config_hash, path)
  if File.exists?(path) and File.readable?(path) and File.file?(path)
    original = File.read(path)

    File.open("#{path}.bak", 'w') do |f|
      f.write(original)
    end
  end

  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('1.9')
    # Facets ini parser doesn't seems to use the .lines enumerator yet
    Ini.write_to_file(path, config_hash)
  else
    Ini.write_to_file(path, config_hash, "Written by LaunchyOpenSearch #{Time.now}\n")
  end
end