Class: SL::ScriptSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/searchlink/script_plugin.rb

Overview

Script Search

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ScriptSearch

Returns a new instance of ScriptSearch.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/searchlink/script_plugin.rb', line 8

def initialize(config)
  @filename = config['filename']
  @path = config['path']

  %w[trigger searches name script].each do |key|
    raise PluginError.new(%(configuration missing key "#{key}"), plugin: @filename) unless config.key?(key)
  end

  @trigger = config['trigger']
  @searches = config['searches']
  @name = config['name']
  @script = find_script(config['script'])

  unless File.executable?(@script)
    raise PluginError.new(%(script "#{File.basename(@script)}" not executable\nrun `chmod a+x #{@script.shorten_path}` to correct),
                          plugin: @filename)
  end

  class << self
    def settings
      {
        trigger: @trigger,
        searches: @searches
      }
    end

    def search(search_type, search_terms, link_text)
      type = Shellwords.escape(search_type)
      terms = Shellwords.escape(search_terms)
      text = Shellwords.escape(link_text)

      stdout = `#{[@script, type, terms, text].join(' ')} 2>&1`

      unless $CHILD_STATUS.success?
        raise PluginError.new(%("#{File.basename(@script)}" returned error #{$CHILD_STATUS.exitstatus}\n#{stdout}),
                              plugin: @filename)
      end

      begin
        res = JSON.parse(stdout)
      rescue JSON::ParserError
        res = YAML.safe_load(stdout)
      end

      unless res.is_a?(Hash)
        raise PluginError.new(%(invalid results from "#{File.basename(@script)}", must be YAML or JSON string),
                              plugin: @filename)
      end

      %w[url title link_text].each do |key|
        unless res.key?(key)
          raise PluginError.new(%("#{File.basename(@script)}" output missing key "#{key}"), plugin: @filename)
        end
      end

      [res['url'], res['title'], res['link_text']]
    end
  end

  SL::Searches.register @name, :search, self
end

Instance Method Details

#find_script(script) ⇒ Object

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/searchlink/script_plugin.rb', line 70

def find_script(script)
  return File.expand_path(script) if File.exist?(File.expand_path(script))

  base = File.expand_path('~/.config/searchlink/plugins')
  first = File.join(base, script)
  return first if File.exist?(first)

  base = File.expand_path('~/.config/searchlink')
  second = File.join(base, script)
  return second if File.exist?(second)

  raise PluginError.new(%(Script plugin script "#{script}" not found), plugin: @filename)
end