catsay 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/catsay +7 -0
- data/cats/default.erb +9 -0
- data/cats/test.erb +1 -0
- data/lib/cat.rb +22 -0
- data/lib/catsay.rb +144 -0
- data/lib/exceptions.rb +5 -0
- metadata +52 -0
data/bin/catsay
ADDED
data/cats/default.erb
ADDED
data/cats/test.erb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
A kitty meows, "<%= @message %>"
|
data/lib/cat.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Catsay
|
2
|
+
|
3
|
+
# Cat class does the meowing.
|
4
|
+
class Cat
|
5
|
+
|
6
|
+
# create a new instance of cat
|
7
|
+
# :template specifies which template to use
|
8
|
+
# the default template is :default
|
9
|
+
def initialize(kwargs = {})
|
10
|
+
@template = kwargs[:template]
|
11
|
+
return self
|
12
|
+
end
|
13
|
+
|
14
|
+
# renders the message using an erb template
|
15
|
+
# returns the rendered template
|
16
|
+
def meow(message)
|
17
|
+
@message = message
|
18
|
+
ERB.new(@template).result(binding)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end # module Catsay
|
data/lib/catsay.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
# this is real ugly
|
4
|
+
includes = %w{cat exceptions}
|
5
|
+
|
6
|
+
includes.each do |inc|
|
7
|
+
require File.expand_path(File.join(File.dirname(__FILE__), inc))
|
8
|
+
end
|
9
|
+
|
10
|
+
module Catsay
|
11
|
+
|
12
|
+
# handle CLI stuff
|
13
|
+
class CLI
|
14
|
+
class << self
|
15
|
+
|
16
|
+
# - parses command-line arguments
|
17
|
+
# - finds the cat and makes it meow
|
18
|
+
# (or gives an error)
|
19
|
+
def run!
|
20
|
+
@options = parse_arguments
|
21
|
+
|
22
|
+
if @options[:verbose]
|
23
|
+
p @options
|
24
|
+
end
|
25
|
+
|
26
|
+
cat = @options[:cat]
|
27
|
+
|
28
|
+
output_handle.puts Cat.new(:template => template).meow(message)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
# returns a file handle for the input
|
34
|
+
# if there is one or returns nil
|
35
|
+
def input_handle
|
36
|
+
if @options[:message] != nil
|
37
|
+
nil
|
38
|
+
elsif @options[:input]
|
39
|
+
begin
|
40
|
+
File.open(@options[:input])
|
41
|
+
rescue Errno::ENOENT
|
42
|
+
$stderr.puts "no such file #{@options[:input]}"
|
43
|
+
exit -1
|
44
|
+
end
|
45
|
+
else
|
46
|
+
$stdin
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# check if output is a string
|
51
|
+
# if so, open it because that's probably a filename
|
52
|
+
# otherwise, just return $stdout
|
53
|
+
def output_handle
|
54
|
+
if @options[:output].is_a? String
|
55
|
+
File.open(@options[:output], 'w')
|
56
|
+
else
|
57
|
+
$stdout
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Given a symbol for a cat template, check if it
|
62
|
+
# exists. If it does, return the cat template, otherwise
|
63
|
+
# exits with an error
|
64
|
+
def template
|
65
|
+
begin
|
66
|
+
heisenberg?(@options[:cat])
|
67
|
+
rescue DeadKitty
|
68
|
+
$stderr.puts "I haven't met a kitty named \"#{@options[:cat]}\" yet."
|
69
|
+
exit -1
|
70
|
+
else
|
71
|
+
File.read(template_path_for(@options[:cat]))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# checks if a template exists given a template id
|
76
|
+
# if not, raises DeadKitty exception.
|
77
|
+
def heisenberg?(cat)
|
78
|
+
if File.exists? template_path_for(cat)
|
79
|
+
cat
|
80
|
+
else
|
81
|
+
raise DeadKitty
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# returns the file path given a template id
|
86
|
+
# the template id should be the name of the file
|
87
|
+
# in the cats/ directory minus the cats/ and
|
88
|
+
# .erb extension
|
89
|
+
def template_path_for(template_id)
|
90
|
+
File.join('cats', "#{template_id}.erb")
|
91
|
+
end
|
92
|
+
|
93
|
+
# fetches the input by first looking for
|
94
|
+
# an input handle and then looking for
|
95
|
+
# options[:text] (text specified after arguments)
|
96
|
+
def message
|
97
|
+
if input_handle.nil?
|
98
|
+
@options[:message]
|
99
|
+
else
|
100
|
+
input_handle.read
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# parses the command-line arguments
|
105
|
+
def parse_arguments
|
106
|
+
|
107
|
+
options = Hash.new
|
108
|
+
|
109
|
+
OptionParser.new do |opts|
|
110
|
+
opts.banner = "usage: catsay ..."
|
111
|
+
|
112
|
+
opts.on('-c', '--cat [TEMPLATE]', 'Chooses the cat.') do |cat|
|
113
|
+
options[:cat] = cat.to_sym || :default
|
114
|
+
end
|
115
|
+
|
116
|
+
opts.on('-o', '--out [OUTFILE]', 'Output file (default=/dev/stdout)') do |output|
|
117
|
+
options[:output] = output
|
118
|
+
end
|
119
|
+
|
120
|
+
opts.on('-i', '--in [INFILE]', 'Input file (default=/dev/stdin)') do |input|
|
121
|
+
options[:input] = input
|
122
|
+
end
|
123
|
+
|
124
|
+
opts.on('-e', '--verbose', 'Annoying kitty') do |verbose|
|
125
|
+
options[:verbose] = verbose
|
126
|
+
end
|
127
|
+
end.parse!
|
128
|
+
|
129
|
+
# check if there is a message, otherwise set message to nil
|
130
|
+
message = ARGV.join(' ')
|
131
|
+
options[:message] = message == "" ? nil : message
|
132
|
+
|
133
|
+
# check if there is a cat template, otherwise set to :default
|
134
|
+
options[:cat] = options[:cat] || :default
|
135
|
+
|
136
|
+
return options
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
end # class << self
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
end
|
data/lib/exceptions.rb
ADDED
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: catsay
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Austin G. Davis-Richardson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-03 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Like cowsay but with cats
|
15
|
+
email: harekrishna@gmail.com
|
16
|
+
executables:
|
17
|
+
- catsay
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- bin/catsay
|
22
|
+
- lib/cat.rb
|
23
|
+
- lib/catsay.rb
|
24
|
+
- lib/exceptions.rb
|
25
|
+
- cats/default.erb
|
26
|
+
- cats/test.erb
|
27
|
+
homepage: https://github.com/audy/catsay
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.23
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: Cats in your terminal
|
52
|
+
test_files: []
|