Skip to content

Commit b8ebabe

Browse files
committed
cli spec; reorg repl a bit
1 parent ff75adc commit b8ebabe

File tree

5 files changed

+45
-56
lines changed

5 files changed

+45
-56
lines changed

bin/tryruby

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env ruby
22
require 'tryruby'
33

4-
TryRuby::CLI.run
4+
TryRuby::CLI.new(ARGV).run
55

66
# add -Ilib to run during development

lib/try_ruby/cli.rb

+6-38
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,18 @@ module TryRuby
22
class CLI
33
attr_accessor :options
44

5-
def initialize(options = {})
6-
self.options = options
5+
def initialize(args)
6+
self.options = parse_options(args)
77
end
88

9-
# def create_config_file
10-
# `open 'https://api.put.io/v2/oauth2/authenticate?client_id=183&response_type=code&redirect_uri=#{Getit::REDIRECT}'`
11-
# # stdin ask for code from the url
12-
# print "Please paste what comes after the 'code=': "
13-
# code = STDIN.gets
14-
15-
# # request access_token with code
16-
# uri = URI.parse("https://api.put.io/v2/oauth2/access_token?client_id=#{Getit::ID}&client_secret=#{Getit::SECRET}&grant_type=authorization_code&redirect_uri=#{Getit::REDIRECT}&code=#{code}")
17-
# http = Net::HTTP.new(uri.host, uri.port)
18-
# http.use_ssl = true
19-
# http.verify_mode = OpenSSL::SSL::VERIFY_NONE
20-
21-
# request = Net::HTTP::Get.new(uri.request_uri)
22-
23-
# response = http.request(request)
24-
25-
# new_access_token = ''
26-
27-
# if response.code.to_i == 200
28-
# new_access_token = JSON.parse(response.body)['access_token']
29-
# end
30-
31-
# # write file
32-
# @config = {
33-
# 'access_token' => 'access_token',
34-
# 'last_accessed_at' => Time.now.to_i
35-
# }
36-
37-
# write_config_file
38-
# end
39-
409
def run
4110
TryRuby::REPL.new(options).start
4211
end
4312

44-
def self.run
45-
new(parse_options).run
46-
end
13+
private
4714

48-
def self.parse_options
15+
# @private
16+
def parse_options(args)
4917
{}.tap do |options|
5018

5119
OptionParser.new do |opts|
@@ -79,7 +47,7 @@ def self.parse_options
7947
puts opts
8048
exit
8149
end
82-
end.parse!
50+
end.parse!(args)
8351

8452
end
8553
end

lib/try_ruby/repl.rb

+16-17
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,34 @@ def start
1313
display script.intro.formatted
1414

1515
while command = Readline.readline("> ", true)
16-
# print instructions if code == 'help'
17-
1816
case command
1917
when "help"
2018
display "Soon, I will have help for you."
2119
when "next", "continue"
2220
display script.next.formatted
2321
when "prev", "previous", "back"
2422
display script.previous.formatted
25-
when "exit"
26-
display "Goodbye, I hope you had fun!\n"
27-
exit 0
23+
when "exit", "quit"
24+
break
2825
else
29-
begin
30-
result = eval(command)
26+
process(command)
27+
end
28+
end
3129

32-
display "=> #{result}"
30+
display "Goodbye, I hope you had fun!\n"
31+
end
3332

34-
display(script.next.formatted) if script.continue?
35-
rescue SyntaxError
36-
display "Oops, seems to have been some error. Care to try again?"
33+
def process(command)
34+
begin
35+
result = eval(command)
3736

38-
next
39-
rescue NameError
40-
display "Oops, you tried to use a method or variable that doesn't exist. Care to try again?"
37+
display "=> #{result}"
4138

42-
next
43-
end
44-
end
39+
display(script.next.formatted) if script.continue?
40+
rescue SyntaxError
41+
display "Oops, seems to have been some error. Care to try again?"
42+
rescue NameError
43+
display "Oops, you tried to use a method or variable that doesn't exist. Care to try again?"
4544
end
4645
end
4746

spec/lib/try_ruby/cli_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
3+
describe TryRuby::CLI do
4+
context "a cli" do
5+
let(:cli) {TryRuby::CLI.new([])}
6+
7+
it 'runs the repl with parsed options' do
8+
repl = stub(:start)
9+
TryRuby::REPL.stubs(:new).returns(repl)
10+
11+
cli.run
12+
13+
expect(repl).to have_received(:start)
14+
expect(TryRuby::REPL).to have_received(:new).with({})
15+
end
16+
end
17+
end

spec/lib/try_ruby/repl_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'spec_helper'
2+
3+
describe TryRuby::REPL do
4+
5+
end

0 commit comments

Comments
 (0)