Module: Wolf::Mouth

Extended by:
Mouth
Included in:
Mouth
Defined in:
lib/wolf/mouth.rb

Instance Method Summary collapse

Instance Method Details

#build_query(args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wolf/mouth.rb', line 27

def build_query(args)
  cmd = args.shift
  cmd_alias = ALIASES[cmd]  || ALIASES[cmd.to_sym]
  if cmd_alias.to_s.include?('%s')
    cmd_alias % args
  else
    cmd = cmd_alias || cmd
    ([cmd] + args).join(' ')
  end
rescue ArgumentError
  abort "Wolf Error: Wrong number of arguments"
end

#eat(argv, options, fetch_options) ⇒ Object



5
6
7
8
9
# File 'lib/wolf/mouth.rb', line 5

def eat(argv, options, fetch_options)
  load_rc '~/.wolfrc'
  options[:load] ? eat_at_home(argv, options) :
    eat_out(build_query(argv), options, fetch_options)
end

#eat_at_home(files, options) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/wolf/mouth.rb', line 11

def eat_at_home(files, options)
  Hirb.enable
  files.each {|file|
    Mouth.swallow Wolfram::Result.new(File.read(@file = file)), options
  }
rescue Errno::ENOENT
  abort "Wolf Error: File '#{@file}' does not exist"
end

#eat_out(query, options, fetch_options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wolf/mouth.rb', line 40

def eat_out(query, options, fetch_options)
  # Can't display other formats except as raw xml
  options[:xml] = true if fetch_options[:format] && !options.key?(:xml)
  if options[:open]
    open Wolfram.query(query,
      :query_uri => "http://www.wolframalpha.com/input/").uri(:i => query)
  elsif options[:xml]
    puts Wolfram.fetch(query, fetch_options).xml
  else
    Hirb.enable
    result = Wolfram.fetch(query, fetch_options)
    swallow result, options
    menu(result) if options[:menu]
  end
end

#load_rc(file) ⇒ Object



20
21
22
23
24
25
# File 'lib/wolf/mouth.rb', line 20

def load_rc(file)
  load file if File.exists?(File.expand_path(file))
rescue StandardError, SyntaxError, LoadError => e
  warn "Wolf Error while loading #{file}:\n"+
    "#{e.class}: #{e.message}\n    #{e.backtrace.join("\n    ")}"
end


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/wolf/mouth.rb', line 56

def menu(result)
  choices = []
  result.pods.select {|e| e.states.size > 0 }.each {|e|
    choices += e.states.map {|s| [e.title, s.name] } }
  puts "\n** LINKS **"
  choice = Hirb::Menu.render(choices, :change_fields => ['Section', 'Choice'],
    :prompt =>"Choose one link to requery: ", :description => false,
    :directions => false)[0]
  if choice && (pod = result[choice[0]]) && state = pod.states.find {|e| e.name == choice[1] }
    swallow state.refetch, options
  else
    abort "Wolf Error: Unable to find this link to requery it"
  end
end

#open(uri) ⇒ Object



81
82
83
# File 'lib/wolf/mouth.rb', line 81

def open(uri)
  system('open', uri)
end

#swallow(result, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/wolf/mouth.rb', line 71

def swallow(result, options={})
  if result.success
    puts Stomach.digest(result, options)
  else
    warn "No results found"
  end
  puts "\nURI: #{result.uri}", "Fetched in #{result.timing}s",
    "Found #{result.pods.size} pods" if options[:verbose]
end