userinput 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7931090fde842e07e7bb83ca88dcef175c348f0c
4
- data.tar.gz: ce4cb1f5fc13933887acac8b53d300042bef9b05
3
+ metadata.gz: 1c1149e9754ddf19bb5d121000a9022a88464096
4
+ data.tar.gz: 6dae20c6c1bcd14207c548311c82da96a78eacf0
5
5
  SHA512:
6
- metadata.gz: f69f1f71afd3d61a7ec05578867d5c6ef731639a6b58c5a978cc2b68073c10c6781b740641c4414ccdfd80066a0691bd7b3327c120f353fc37f094696b6085fc
7
- data.tar.gz: e519c52e115d34fa9711377911da156d2f5cc14570f3be3b17b7dba4946340bc95b038ee89e59bba5a3d1b8b57f0e8e3f306c5aaa455c44e3f2115f44687e5dc
6
+ metadata.gz: 160c87160ef2843da4bdc7813ae1f827d20b6b85875400953c537cfd56a2672a8ea54da40c93d67739de8f69ca8815b9b7930e8c7beac92879d905c99dd7ab96
7
+ data.tar.gz: 55c032fbcc1daea95606db9389485c88661eec605c8c186c02f25fdc17bec0d261ff342bc3251d60748d4cf2e441a519730535b7ea8b5d9b3d8a9616f528b777
data/README.md CHANGED
@@ -8,10 +8,83 @@ userinput
8
8
  [![Build Status](https://img.shields.io/travis/akerl/userinput.svg)](https://travis-ci.org/akerl/userinput)
9
9
  [![MIT Licensed](https://img.shields.io/badge/license-MIT-green.svg)](https://tldrlegal.com/license/mit-license)
10
10
 
11
- SHORT_DESCRIPTION
11
+ A simple user input library
12
12
 
13
13
  ## Usage
14
14
 
15
+ User interaction is handled by the `UserInput::Prompt` object. As a convenience, this can be created using the `UserInput.new` method:
16
+
17
+ ```
18
+ > require 'userinput'
19
+ => true
20
+ > prompt = UserInput.new(message: 'Username')
21
+ => #<UserInput::Prompt:0x007fdfba1681f8 @attempts=nil, @message="Username", @default=nil, @secret=false, @validation=nil>
22
+ > prompt.ask
23
+ Username? leet_dude
24
+ => "leet_dude"
25
+ ```
26
+
27
+ You can optionally provide a default:
28
+
29
+ ```
30
+ > require 'userinput'
31
+ => true
32
+ > prompt = UserInput.new(default: '_other')
33
+ => #<UserInput::Prompt:0x007fb7eb163f00 @attempts=nil, @message="", @default="_other", @secret=false, @validation=nil>
34
+ > prompt.ask
35
+ ? [_other] answer
36
+ => "answer"
37
+ > prompt.ask
38
+ ? [_other]
39
+ => "_other"
40
+ ```
41
+
42
+ If you provide a validation Regexp or a code block, the input will be validated using that:
43
+
44
+ ```
45
+ > require 'userinput'
46
+ => true
47
+ > prompt = UserInput.new(validation: /[0-9]+/)
48
+ => #<UserInput::Prompt:0x007f99909637b0 @attempts=nil, @message="", @default=nil, @secret=false, @validation=/[0-9]+/>
49
+ > prompt.ask
50
+ ? _str
51
+ ? 23
52
+ => "23"
53
+ > other_prompt = UserInput.new { |x| x == '_correct' }
54
+ => #<UserInput::Prompt:0x007f9990920eb0 @attempts=nil, @message="", @default=nil, @secret=false, @validation=#<Proc:0x007f99909211d0@(irb):4>>
55
+ > other_prompt.ask
56
+ ? _wrong
57
+ ? 23
58
+ ? _correct
59
+ => "_correct"
60
+ ```
61
+
62
+ Providing a number of attempts will cause an ArgumentError to be raised after that many failed inputs:
63
+
64
+ ```
65
+ > require 'userinput'
66
+ => true
67
+ > prompt = UserInput.new(attempts: 3, validation: /[0-9]+/)
68
+ => #<UserInput::Prompt:0x007f9f6a950270 @attempts=3, @message="", @default=nil, @secret=false, @validation=/[0-9]+/>
69
+ > prompt.ask
70
+ ? _str
71
+ ? _other
72
+ ? _attempt
73
+ ArgumentError: No valid input provided
74
+ ```
75
+
76
+ For sensitive input, pass the `secret` parameter, which will try to disable terminal printing of the user's input:
77
+
78
+ ```
79
+ > require 'userinput'
80
+ => true
81
+ > prompt = UserInput.new(message: 'Password', secret: true)
82
+ => #<UserInput::Prompt:0x007fb8ab9220a0 @attempts=nil, @message="Password", @default=nil, @secret=true, @validation=nil>
83
+ > prompt.ask
84
+ Password?
85
+ => "_password"
86
+ ```
87
+
15
88
  ## Installation
16
89
 
17
90
  gem install userinput
@@ -60,6 +60,7 @@ module UserInput
60
60
  def _ask
61
61
  input = STDIN.gets.chomp
62
62
  input = @default if input.empty? && !@default.nil?
63
+ puts if @secret
63
64
  input
64
65
  end
65
66
 
@@ -54,6 +54,14 @@ describe UserInput do
54
54
  expect(prompt.ask).to eql 'correct'
55
55
  end
56
56
  end
57
+ context 'when provided an unsupported validation method' do
58
+ it 'raises a RuntimeError' do
59
+ prompt = UserInput::Prompt.new(validation: 28)
60
+ allow(STDIN).to receive(:gets).and_return("_str\n")
61
+ expect(prompt).to receive(:print).with('? ')
62
+ expect { prompt.ask }.to raise_error RuntimeError
63
+ end
64
+ end
57
65
 
58
66
  it 'raises an error if max attempts is reached' do
59
67
  prompt = UserInput::Prompt.new(attempts: 2) { |x| false }
@@ -62,5 +70,12 @@ describe UserInput do
62
70
  expect { prompt.ask }.to raise_error ArgumentError
63
71
  end
64
72
  end
73
+
74
+ it 'disables echo for secret input' do
75
+ prompt = UserInput::Prompt.new(secret: true)
76
+ allow(STDIN).to receive(:gets).and_return("_str\n")
77
+ expect(prompt).to receive(:print).with('? ')
78
+ expect(prompt.ask).to eql '_str'
79
+ end
65
80
  end
66
81
  end
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'userinput'
3
- s.version = '0.0.1'
3
+ s.version = '0.0.2'
4
4
  s.date = Time.now.strftime("%Y-%m-%d")
5
5
 
6
- s.summary = ''
7
- s.description = ""
6
+ s.summary = 'Simple user input library'
7
+ s.description = 'Provides a prompt object for requesting user input'
8
8
  s.authors = ['Les Aker']
9
9
  s.email = 'me@lesaker.org'
10
10
  s.homepage = 'https://github.com/akerl/userinput'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: userinput
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Les Aker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-29 00:00:00.000000000 Z
11
+ date: 2014-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - ~>
81
81
  - !ruby/object:Gem::Version
82
82
  version: 1.3.2
83
- description: ''
83
+ description: Provides a prompt object for requesting user input
84
84
  email: me@lesaker.org
85
85
  executables: []
86
86
  extensions: []
@@ -121,7 +121,7 @@ rubyforge_project:
121
121
  rubygems_version: 2.0.14
122
122
  signing_key:
123
123
  specification_version: 4
124
- summary: ''
124
+ summary: Simple user input library
125
125
  test_files:
126
126
  - spec/spec_helper.rb
127
127
  - spec/userinput_spec.rb