Module: Ripl::Play

Defined in:
lib/ripl/play.rb

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gems_to_install(str) ⇒ Object



70
71
72
73
74
75
# File 'lib/ripl/play.rb', line 70

def gems_to_install(str)
  gems = str.scan(/require\s*['"]([^'"\s]+)['"]/).flatten
  gems.reject {|e| requireable(e) }.map {|e|
    e.include?('/') ? e[/^[^\/]+/] : e
  }.uniq.map {|e| e[/^active_/] ? e.sub('_', '') : e }
end

.install_gems(str) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/ripl/play.rb', line 59

def install_gems(str)
  gems = gems_to_install(str)
  return if gems.empty?
  print "Can I install the following gems: #{gems.join(', ')} ? ([y]/n)"
  if $stdin.gets.to_s[/^n/]
    abort "Please install these gems manually: #{gems.join(' ')}"
  else
    system(ENV['GEM'] || 'gem', 'install', *gems)
  end
end

.requireable(lib) ⇒ Object



77
78
79
80
81
82
# File 'lib/ripl/play.rb', line 77

def requireable(lib)
  require lib
  true
rescue LoadError
  false
end

Instance Method Details

#before_loopObject



6
7
8
9
10
11
# File 'lib/ripl/play.rb', line 6

def before_loop
  super
  play_back
  config[:play_quiet] = false
  require 'ripl/readline'
end

#get_inputObject



17
18
19
20
# File 'lib/ripl/play.rb', line 17

def get_input
  puts(prompt + @play_input)
  @play_input
end

#play_backObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ripl/play.rb', line 22

def play_back
  if !$stdin.tty?
    play_back_string($stdin.read)
    $stdin.reopen '/dev/tty'
  elsif config[:play][/^http/]
    play_back_url(config[:play])
  elsif File.exists? config[:play]
    play_back_string(File.read(config[:play]))
  else
    abort "ripl can't play `#{config[:play]}'"
  end
end

#play_back_string(str) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/ripl/play.rb', line 50

def play_back_string(str)
  Ripl::Play.install_gems(str) if config[:play_install]
  str.split("\n").each {|input|
    @play_input = input
    loop_once
  }
end

#play_back_url(url) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ripl/play.rb', line 35

def play_back_url(url)
  require 'open-uri'
  require 'net/http'

  if url[/gist.github.com\/[a-z\d]+$/]
    url += '.txt'
  elsif url[/github.com.*blob/]
    url.sub!('blob', 'raw')
  end

  play_back_string open(url).string
rescue SocketError
  abort "ripl can't play `#{url}'"
end


13
14
15
# File 'lib/ripl/play.rb', line 13

def print_result(*)
  super unless config[:play_quiet]
end