Module: Wolf::Mouth
Instance Method Summary collapse
- #build_query(args) ⇒ Object
- #eat(argv, options, fetch_options) ⇒ Object
- #eat_at_home(files, options) ⇒ Object
- #eat_out(query, options, fetch_options) ⇒ Object
- #load_rc(file) ⇒ Object
- #menu(result) ⇒ Object
- #open(uri) ⇒ Object
- #swallow(result, options = {}) ⇒ Object
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, , ) load_rc '~/.wolfrc' [:load] ? eat_at_home(argv, ) : eat_out(build_query(argv), , ) 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, ) Hirb.enable files.each {|file| Mouth.swallow Wolfram::Result.new(File.read(@file = file)), } 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, , ) # Can't display other formats except as raw xml [:xml] = true if [:format] && !.key?(:xml) if [:open] open Wolfram.query(query, :query_uri => "http://www.wolframalpha.com/input/").uri(:i => query) elsif [:xml] puts Wolfram.fetch(query, ).xml else Hirb.enable result = Wolfram.fetch(query, ) swallow result, (result) if [: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.(file)) rescue StandardError, SyntaxError, LoadError => e warn "Wolf Error while loading #{file}:\n"+ "#{e.class}: #{e.}\n #{e.backtrace.join("\n ")}" end |
#menu(result) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wolf/mouth.rb', line 56 def (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, 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, ={}) if result.success puts Stomach.digest(result, ) else warn "No results found" end puts "\nURI: #{result.uri}", "Fetched in #{result.timing}s", "Found #{result.pods.size} pods" if [:verbose] end |