usaidwat 1.4.2 → 1.4.3

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: f60bbafb7dc38ce3ba607557393b5be840110def
4
- data.tar.gz: 265fc8c863875b82178991a44f3e11cb040e00e7
3
+ metadata.gz: 9f12ca3f12a32eeee431b4f589c7d4454f5b941e
4
+ data.tar.gz: 9a295db51fbef5c135b1145ddc9330581668b9b9
5
5
  SHA512:
6
- metadata.gz: 0601d41a7aefeda88672eba637015ad703de34618cf41535938d4f8d83e72a077bbebfef0317938be94d88498acc95395ee2f46244d6a91fc301fc5a49d4d0d9
7
- data.tar.gz: b20001b8c0a9fee51df926b464d90b51922a244028d9124119f2803e573de2935ba22551c338ffb71e13c177348bd89fb68c2574c688d0e4fb853dfc7dfb8cb7
6
+ metadata.gz: f86b9208cf4d10cc156daa37f12423be5325d6be89eaf91346e1437523d25ac8b18ad16b0cf3cdff3996aa8368aac9e9112db7e2c473862df3cdb47c313f7326
7
+ data.tar.gz: a1eb4808dc6f614b2d477a9d247743755923ccccfa68535168b912dfc457f6a050d0c02b8096e25c160a8910efa5c274a0d075023b0c00f9a90bd806177ae56d
data/lib/usaidwat.rb CHANGED
@@ -1,5 +1,14 @@
1
+ def require_all(path)
2
+ glob = File.join(File.dirname(__FILE__), path, '*.rb')
3
+ Dir[glob].each do |f|
4
+ require f
5
+ end
6
+ end
7
+
1
8
  require "usaidwat/algo"
2
- require "usaidwat/application"
3
9
  require "usaidwat/client"
10
+ require "usaidwat/command"
4
11
  require "usaidwat/formatter"
5
12
  require "usaidwat/version"
13
+
14
+ require_all "usaidwat/commands"
@@ -19,7 +19,7 @@ module USaidWat
19
19
  user.comments(100)
20
20
  rescue NoMethodError
21
21
  raise NoSuchUserError, username
22
- rescue TypeError
22
+ rescue TypeError, Net::HTTP::Persistent::Error
23
23
  raise ReachabilityError, "Reddit unreachable"
24
24
  end
25
25
 
@@ -47,7 +47,7 @@ module USaidWat
47
47
  user.posts
48
48
  rescue NoMethodError
49
49
  raise NoSuchUserError, username
50
- rescue TypeError
50
+ rescue TypeError, Net::HTTP::Persistent::Error
51
51
  raise ReachabilityError, "Reddit unreachable"
52
52
  end
53
53
 
@@ -61,7 +61,7 @@ module USaidWat
61
61
  user.about[key]
62
62
  rescue NoMethodError
63
63
  raise NoSuchUserError, username
64
- rescue TypeError
64
+ rescue TypeError, Net::HTTP::Persistent::Error
65
65
  raise ReachabilityError, "Reddit unreachable"
66
66
  end
67
67
  end
@@ -0,0 +1,48 @@
1
+ require 'sysexits'
2
+ require 'usaidwat/client'
3
+ require 'usaidwat/pager'
4
+
5
+ require 'timecop' if ENV['USAIDWAT_ENV'] == 'cucumber'
6
+
7
+ module USaidWat
8
+ module Application
9
+ class Command
10
+ include Pager
11
+ include Sysexits
12
+
13
+ attr_reader :client
14
+
15
+ class << self
16
+ def subclasses
17
+ @subclasses ||= []
18
+ end
19
+
20
+ def inherited(base)
21
+ subclasses << base
22
+ super
23
+ end
24
+ end
25
+
26
+ def initialize(prog)
27
+ @client = cucumber? ? USaidWat::Client::TestRedditor : USaidWat::Client::Redditor
28
+ Timecop.freeze(Time.parse(ENV['USAIDWAT_CURRENT_TIME'])) if cucumber_time?
29
+ end
30
+
31
+ def quit(message, code=:ok)
32
+ stream = code == :ok ? $stdout : $stderr
33
+ stream.puts message
34
+ exit code
35
+ end
36
+
37
+ protected
38
+
39
+ def cucumber?
40
+ ENV['USAIDWAT_ENV'] == 'cucumber'
41
+ end
42
+
43
+ def cucumber_time?
44
+ cucumber? && !ENV['USAIDWAT_CURRENT_TIME'].nil?
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,30 @@
1
+ require 'usaidwat/client'
2
+ require 'usaidwat/command'
3
+
4
+ module USaidWat
5
+ module Application
6
+ class Info < Command
7
+ def initialize(prog)
8
+ prog.command(:info) do |c|
9
+ c.action do |args, options|
10
+ process(options, args)
11
+ end
12
+ end
13
+ super
14
+ end
15
+
16
+ def process(options, args)
17
+ raise ArgumentError.new('You must specify a username') if args.empty?
18
+ username = args.shift
19
+
20
+ redditor = client.new(username)
21
+ created_at = redditor.created_at.strftime("%b %d, %Y %H:%M %p")
22
+ puts "Created: #{created_at} (#{redditor.age})"
23
+ printf "Link Karma: %d\n", redditor.link_karma
24
+ printf "Comment Karma: %d\n", redditor.comment_karma
25
+ rescue USaidWat::Client::NoSuchUserError
26
+ quit "No such user: #{username}", :no_such_user
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,64 @@
1
+ require 'usaidwat/client'
2
+ require 'usaidwat/command'
3
+ require 'usaidwat/filter'
4
+ require 'usaidwat/formatter'
5
+ require 'usaidwat/ext/array'
6
+
7
+ module USaidWat
8
+ module Application
9
+ class Log < Command
10
+ include FilterCommand
11
+
12
+ def initialize(prog)
13
+ prog.command(:log) do |c|
14
+ c.alias :l
15
+ c.option 'date', '--date FORMAT', 'Show dates in "absolute" or "relative" format'
16
+ c.option 'grep', '--grep STRING', 'Show only comments matching STRING'
17
+ c.option 'limit', '--limit LIMIT', '-n LIMIT', 'Only show n comments'
18
+ c.option 'oneline', '--oneline', 'Output log in a more compact form'
19
+ c.option 'raw', '--raw', 'Print raw comment bodies'
20
+
21
+ c.action do |args, options|
22
+ process(options, args)
23
+ end
24
+ end
25
+ super
26
+ end
27
+
28
+ def process(options, args)
29
+ raise ArgumentError.new('You must specify a username') if args.empty?
30
+ username = args.shift
31
+ subreddits = args.subreddits
32
+
33
+ redditor = client.new(username)
34
+ comments = redditor.comments
35
+
36
+ res = filter_entries('comments', redditor, comments, subreddits) >>
37
+ lambda { |r| grep_entries('comments', redditor, r.value, options['grep']) } >>
38
+ lambda { |r| limit_entries('comments', redditor, r.value, options['limit']) } >>
39
+ lambda { |r| ensure_entries('comments', redditor, r.value) }
40
+
41
+ quit res.value if res.left?
42
+
43
+ opts = {
44
+ :date_format => (options['date'] || :relative).to_sym,
45
+ :oneline => !options['oneline'].nil?,
46
+ :pattern => options['grep'],
47
+ :raw => !options['raw'].nil?,
48
+ }
49
+ list_comments(res.value, opts)
50
+ rescue USaidWat::Client::NoSuchUserError
51
+ quit "No such user: #{username}", :no_such_user
52
+ end
53
+
54
+ private
55
+
56
+ def list_comments(comments, options = {})
57
+ oneline = options[:oneline]
58
+ formatter = (oneline ? USaidWat::CLI::CompactCommentFormatter : USaidWat::CLI::CommentFormatter).new(options)
59
+ page
60
+ comments.each { |c| print formatter.format(c) }
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,77 @@
1
+ require 'usaidwat/client'
2
+ require 'usaidwat/command'
3
+ require 'usaidwat/count'
4
+ require 'usaidwat/filter'
5
+ require 'usaidwat/formatter'
6
+
7
+ module USaidWat
8
+ module Application
9
+ class Posts < Command
10
+ include CountCommand
11
+ include FilterCommand
12
+
13
+ def initialize(prog)
14
+ prog.command(:posts) do |c|
15
+ c.action do |args, options|
16
+ process(options, args)
17
+ end
18
+
19
+ c.command(:log) do |s|
20
+ s.description "Show a user's submitted posts"
21
+ s.option 'oneline', '--oneline', 'Output log in a more compact form'
22
+ s.action do |args, options|
23
+ process_log(options, args)
24
+ end
25
+ end
26
+
27
+ c.command(:tally) do |s|
28
+ s.description "Tally a user's posts by subreddit"
29
+ s.option 'count', '-c', '--count', 'Sort output by number of comments'
30
+ s.action do |args, options|
31
+ process_tally(options, args)
32
+ end
33
+ end
34
+ end
35
+ super
36
+ end
37
+
38
+ def process(options, args)
39
+ quit "Do you want to tally or log posts?", :usage
40
+ end
41
+
42
+ def process_log(options, args)
43
+ raise ArgumentError.new('You must specify a username') if args.empty?
44
+ oneline = !!options['oneline']
45
+ username = args.shift
46
+ subreddits = args.subreddits
47
+
48
+ redditor = client.new(username)
49
+ posts = redditor.posts
50
+
51
+ res = filter_entries('posts', redditor, posts, subreddits) >>
52
+ lambda { |r| ensure_entries('posts', redditor, r.value) }
53
+
54
+ quit res.value if res.left?
55
+ posts = res.value
56
+
57
+ formatter = (oneline ? USaidWat::CLI::CompactPostFormatter : USaidWat::CLI::PostFormatter).new
58
+ page
59
+ posts.each { |p| print formatter.format(p) }
60
+ end
61
+
62
+ def process_tally(options, args)
63
+ raise ArgumentError.new('You must specify a username') if args.empty?
64
+ raise ArgumentError.new('You cannot specify a subreddit when tallying comments') if args.count > 1
65
+ username = args.first
66
+
67
+ redditor = client.new(username)
68
+ quit "#{redditor.username} has no posts." if redditor.posts.empty?
69
+ partition_data = partition(redditor.posts, options['count'])
70
+ formatter = USaidWat::CLI::TallyFormatter.new
71
+ print formatter.format(partition_data)
72
+ rescue USaidWat::Client::NoSuchUserError
73
+ quit "No such user: #{username}", :no_such_user
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,37 @@
1
+ require 'usaidwat/client'
2
+ require 'usaidwat/command'
3
+ require 'usaidwat/count'
4
+
5
+ module USaidWat
6
+ module Application
7
+ class Tally < Command
8
+ include CountCommand
9
+
10
+ def initialize(prog)
11
+ prog.command(:tally) do |c|
12
+ c.alias :t
13
+ c.option 'count', '-c', '--count', 'Sort output by number of comments'
14
+
15
+ c.action do |args, options|
16
+ process(options, args)
17
+ end
18
+ end
19
+ super
20
+ end
21
+
22
+ def process(options, args)
23
+ raise ArgumentError.new('You must specify a username') if args.empty?
24
+ raise ArgumentError.new('You cannot specify a subreddit when tallying comments') if args.count > 1
25
+ username = args.first
26
+
27
+ redditor = client.new(username)
28
+ quit "#{redditor.username} has no comments." if redditor.comments.empty?
29
+ partition_data = partition(redditor.comments, options['count'])
30
+ formatter = USaidWat::CLI::TallyFormatter.new
31
+ print formatter.format(partition_data)
32
+ rescue USaidWat::Client::NoSuchUserError
33
+ quit "No such user: #{username}", :no_such_user
34
+ end
35
+ end
36
+ end
37
+ end
@@ -17,7 +17,7 @@ class String
17
17
  gsub(/#{pattern}/i) { |m| Rainbow(m).red }
18
18
  end
19
19
 
20
- def transform_html_entities
20
+ def unescape_html
21
21
  gsub(/&gt;/, '>').gsub(/&lt;/, '<').gsub(/&amp;/, '&')
22
22
  end
23
23
  end
@@ -1,3 +1,5 @@
1
+ require 'usaidwat/either'
2
+
1
3
  module USaidWat
2
4
  module Application
3
5
  module FilterCommand
@@ -48,7 +48,7 @@ module USaidWat
48
48
  out.write("\n\n\n") unless @count == 0
49
49
  out.write("#{post.subreddit}\n".color(:green))
50
50
  out.write("#{post_link(post)}\n".color(:yellow))
51
- out.write("#{post.title.strip.transform_html_entities.truncate(cols)}\n".color(:magenta))
51
+ out.write("#{post.title.strip.unescape_html.truncate(cols)}\n".color(:magenta))
52
52
  out.write("#{post_date(post)}".color(:blue))
53
53
  out.write("\n#{post.url}") unless post.url.end_with?(post.permalink)
54
54
  @count += 1
@@ -73,7 +73,7 @@ module USaidWat
73
73
  out = StringIO.new
74
74
  subreddit = post.subreddit
75
75
  cols -= subreddit.length + 1
76
- title = post.title.strip.transform_html_entities.truncate(cols)
76
+ title = post.title.strip.unescape_html.truncate(cols)
77
77
  out.write(subreddit.color(:green))
78
78
  out.write(" #{title}\n")
79
79
  out.rewind
@@ -88,7 +88,7 @@ module USaidWat
88
88
  out.write("\n\n") unless @count == 0
89
89
  out.write("#{comment.subreddit}\n".color(:green))
90
90
  out.write("#{comment_link(comment)}\n".color(:yellow))
91
- out.write("#{comment.link_title.strip.transform_html_entities.truncate(cols)}\n".color(:magenta))
91
+ out.write("#{comment.link_title.strip.unescape_html.truncate(cols)}\n".color(:magenta))
92
92
  out.write("#{comment_date(comment)}".color(:blue))
93
93
  out.write(" \u2022 ".color(:cyan))
94
94
  out.write(sprintf("%+d\n", comment_karma(comment)).color(:blue))
@@ -107,7 +107,7 @@ module USaidWat
107
107
  end
108
108
 
109
109
  def comment_body(comment)
110
- body = comment.body.strip
110
+ body = comment.body.strip.unescape_html
111
111
  body = markdown.render(body) unless raw?
112
112
  if pattern?
113
113
  body.highlight(pattern)
@@ -143,7 +143,7 @@ module USaidWat
143
143
  out = StringIO.new
144
144
  subreddit = comment.subreddit
145
145
  cols -= subreddit.length + 1
146
- title = comment.link_title.strip.transform_html_entities.truncate(cols)
146
+ title = comment.link_title.strip.unescape_html.truncate(cols)
147
147
  key = "#{subreddit} #{title}"
148
148
  if !seen?(key)
149
149
  out.write("#{subreddit}".color(:green))
@@ -1,3 +1,3 @@
1
1
  module USaidWat
2
- VERSION = "1.4.2"
2
+ VERSION = "1.4.3"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'timecop'
2
3
 
3
4
  module USaidWat
4
5
  module Client
@@ -125,6 +126,46 @@ module USaidWat
125
126
  end
126
127
  end
127
128
 
129
+ context "when Reddit requests timeout" do
130
+ before(:each) do
131
+ WebMock.disable_net_connect!
132
+ WebMock.reset!
133
+ stub_request(:get, "http://www.reddit.com/user/mipadi/comments.json?after=&limit=100").to_timeout
134
+ stub_request(:get, "http://www.reddit.com/user/mipadi/about.json").to_timeout
135
+ stub_request(:get, "http://www.reddit.com/user/mipadi/submitted.json?after=&limit=25").to_timeout
136
+ end
137
+
138
+ describe "#posts" do
139
+ it "raises 'Reddit unreachable' error" do
140
+ expect { redditor.comments }.to raise_error(ReachabilityError, /Reddit unreachable/)
141
+ end
142
+ end
143
+
144
+ describe "#link_karma" do
145
+ it "raises 'Reddit unreachable' error" do
146
+ expect { redditor.link_karma }.to raise_error(ReachabilityError, /Reddit unreachable/)
147
+ end
148
+ end
149
+
150
+ describe "#comment_karma" do
151
+ it "raises 'Reddit unreachable' error" do
152
+ expect { redditor.comment_karma }.to raise_error(ReachabilityError, /Reddit unreachable/)
153
+ end
154
+ end
155
+
156
+ describe "#created_at" do
157
+ it "raises 'Reddit unreachable' error" do
158
+ expect { redditor.created_at }.to raise_error(ReachabilityError, /Reddit unreachable/)
159
+ end
160
+ end
161
+
162
+ describe "#age" do
163
+ it "raises 'Reddit unreachable' error" do
164
+ expect { redditor.age }.to raise_error(ReachabilityError, /Reddit unreachable/)
165
+ end
166
+ end
167
+ end
168
+
128
169
  context "when Reddit is down" do
129
170
  before(:each) do
130
171
  WebMock.disable_net_connect!
@@ -19,20 +19,20 @@ module USaidWat
19
19
  end
20
20
  end
21
21
 
22
- describe "#transform_html_entities" do
22
+ describe "#unescape_html" do
23
23
  it "transforms &amp; to &" do
24
24
  s = "this &amp; that"
25
- expect(s.transform_html_entities).to eq("this & that")
25
+ expect(s.unescape_html).to eq("this & that")
26
26
  end
27
27
 
28
28
  it "transform &lt; to <" do
29
29
  s = "chocolate &lt; vanilla"
30
- expect(s.transform_html_entities).to eq("chocolate < vanilla")
30
+ expect(s.unescape_html).to eq("chocolate < vanilla")
31
31
  end
32
32
 
33
33
  it "transforms &gt; to >" do
34
34
  s = "cats &gt; dogs"
35
- expect(s.transform_html_entities).to eq("cats > dogs")
35
+ expect(s.unescape_html).to eq("cats > dogs")
36
36
  end
37
37
  end
38
38
 
data/usaidwat.gemspec CHANGED
@@ -28,8 +28,8 @@ Gem::Specification.new do |gem|
28
28
 
29
29
  gem.required_ruby_version = '>= 1.9.3'
30
30
 
31
- gem.add_runtime_dependency('downterm', '~> 0.1.4')
32
- gem.add_runtime_dependency('mercenary', '~> 0.3.5')
31
+ gem.add_runtime_dependency('downterm', '~> 0.1.5')
32
+ gem.add_runtime_dependency('mercenary', '0.3.5')
33
33
  gem.add_runtime_dependency('rainbow', '~> 2.0')
34
34
  gem.add_runtime_dependency('snooby', '~> 0.1.5')
35
35
  gem.add_runtime_dependency('sysexits', '~> 1.2')
@@ -39,5 +39,5 @@ Gem::Specification.new do |gem|
39
39
  gem.add_development_dependency('cucumber', '~> 2.0')
40
40
  gem.add_development_dependency('rspec', '~> 3.2')
41
41
  gem.add_development_dependency('timecop', '~> 0.8.0')
42
- gem.add_development_dependency('webmock', '~> 1.21')
42
+ gem.add_development_dependency('webmock', '~> 2.0')
43
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usaidwat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Dippery
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-07 00:00:00.000000000 Z
11
+ date: 2016-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: downterm
@@ -16,26 +16,26 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.4
19
+ version: 0.1.5
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.4
26
+ version: 0.1.5
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: mercenary
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - '='
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.3.5
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - '='
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.3.5
41
41
  - !ruby/object:Gem::Dependency
@@ -156,14 +156,14 @@ dependencies:
156
156
  requirements:
157
157
  - - "~>"
158
158
  - !ruby/object:Gem::Version
159
- version: '1.21'
159
+ version: '2.0'
160
160
  type: :development
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
- version: '1.21'
166
+ version: '2.0'
167
167
  description: View a user's last 100 Reddit comments, organized by subreddit.
168
168
  email:
169
169
  - michael@monkey-robot.com
@@ -197,8 +197,12 @@ files:
197
197
  - features/user.feature
198
198
  - lib/usaidwat.rb
199
199
  - lib/usaidwat/algo.rb
200
- - lib/usaidwat/application.rb
201
200
  - lib/usaidwat/client.rb
201
+ - lib/usaidwat/command.rb
202
+ - lib/usaidwat/commands/info.rb
203
+ - lib/usaidwat/commands/log.rb
204
+ - lib/usaidwat/commands/posts.rb
205
+ - lib/usaidwat/commands/tally.rb
202
206
  - lib/usaidwat/count.rb
203
207
  - lib/usaidwat/either.rb
204
208
  - lib/usaidwat/ext/array.rb
@@ -226,9 +230,9 @@ homepage: https://github.com/mdippery/usaidwat
226
230
  licenses:
227
231
  - MIT
228
232
  metadata:
229
- build_date: 2016-04-06 19:24:55 PDT
230
- commit: v1.4.2
231
- commit_hash: babb216c8e4a29a58fa00518a9cf164ab827fc73
233
+ build_date: 2016-05-04 19:02:53 PDT
234
+ commit: v1.4.3
235
+ commit_hash: fd36eb801d54c12c92948d00c082a700bd5228a5
232
236
  post_install_message:
233
237
  rdoc_options: []
234
238
  require_paths:
@@ -1,229 +0,0 @@
1
- require 'sysexits'
2
- require 'usaidwat/client'
3
- require 'usaidwat/count'
4
- require 'usaidwat/either'
5
- require 'usaidwat/filter'
6
- require 'usaidwat/pager'
7
- require 'usaidwat/ext/array'
8
-
9
- require 'timecop' if ENV['USAIDWAT_ENV'] == 'cucumber'
10
-
11
- module USaidWat
12
- module Application
13
- class Command
14
- include Pager
15
- include Sysexits
16
-
17
- attr_reader :client
18
-
19
- class << self
20
- def subclasses
21
- @subclasses ||= []
22
- end
23
-
24
- def inherited(base)
25
- subclasses << base
26
- super
27
- end
28
- end
29
-
30
- def initialize(prog)
31
- @client = cucumber? ? USaidWat::Client::TestRedditor : USaidWat::Client::Redditor
32
- Timecop.freeze(Time.parse(ENV['USAIDWAT_CURRENT_TIME'])) if cucumber_time?
33
- end
34
-
35
- protected
36
-
37
- def cucumber?
38
- ENV['USAIDWAT_ENV'] == 'cucumber'
39
- end
40
-
41
- def cucumber_time?
42
- cucumber? && !ENV['USAIDWAT_CURRENT_TIME'].nil?
43
- end
44
-
45
- def quit(message, code=:ok)
46
- stream = code == :ok ? $stdout : $stderr
47
- stream.puts message
48
- exit code
49
- end
50
- end
51
-
52
- class Info < Command
53
- def initialize(prog)
54
- prog.command(:info) do |c|
55
- c.action do |args, options|
56
- process(options, args)
57
- end
58
- end
59
- super
60
- end
61
-
62
- def process(options, args)
63
- raise ArgumentError.new('You must specify a username') if args.empty?
64
- username = args.shift
65
-
66
- redditor = client.new(username)
67
- created_at = redditor.created_at.strftime("%b %d, %Y %H:%M %p")
68
- puts "Created: #{created_at} (#{redditor.age})"
69
- printf "Link Karma: %d\n", redditor.link_karma
70
- printf "Comment Karma: %d\n", redditor.comment_karma
71
- rescue USaidWat::Client::NoSuchUserError
72
- quit "No such user: #{username}", :no_such_user
73
- end
74
- end
75
-
76
- class Log < Command
77
- include FilterCommand
78
-
79
- def initialize(prog)
80
- prog.command(:log) do |c|
81
- c.alias :l
82
- c.option 'date', '--date FORMAT', 'Show dates in "absolute" or "relative" format'
83
- c.option 'grep', '--grep STRING', 'Show only comments matching STRING'
84
- c.option 'limit', '--limit LIMIT', '-n LIMIT', 'Only show n comments'
85
- c.option 'oneline', '--oneline', 'Output log in a more compact form'
86
- c.option 'raw', '--raw', 'Print raw comment bodies'
87
-
88
- c.action do |args, options|
89
- process(options, args)
90
- end
91
- end
92
- super
93
- end
94
-
95
- def process(options, args)
96
- raise ArgumentError.new('You must specify a username') if args.empty?
97
- username = args.shift
98
- subreddits = args.subreddits
99
-
100
- redditor = client.new(username)
101
- comments = redditor.comments
102
-
103
- res = filter_entries('comments', redditor, comments, subreddits) >>
104
- lambda { |r| grep_entries('comments', redditor, r.value, options['grep']) } >>
105
- lambda { |r| limit_entries('comments', redditor, r.value, options['limit']) } >>
106
- lambda { |r| ensure_entries('comments', redditor, r.value) }
107
-
108
- quit res.value if res.left?
109
-
110
- opts = {
111
- :date_format => (options['date'] || :relative).to_sym,
112
- :oneline => !options['oneline'].nil?,
113
- :pattern => options['grep'],
114
- :raw => !options['raw'].nil?,
115
- }
116
- list_comments(res.value, opts)
117
- rescue USaidWat::Client::NoSuchUserError
118
- quit "No such user: #{username}", :no_such_user
119
- end
120
-
121
- private
122
-
123
- def list_comments(comments, options = {})
124
- oneline = options[:oneline]
125
- formatter = (oneline ? USaidWat::CLI::CompactCommentFormatter : USaidWat::CLI::CommentFormatter).new(options)
126
- page
127
- comments.each { |c| print formatter.format(c) }
128
- end
129
- end
130
-
131
- class Posts < Command
132
- include CountCommand
133
- include FilterCommand
134
-
135
- def initialize(prog)
136
- prog.command(:posts) do |c|
137
- c.action do |args, options|
138
- process(options, args)
139
- end
140
-
141
- c.command(:log) do |s|
142
- s.description "Show a user's submitted posts"
143
- s.option 'oneline', '--oneline', 'Output log in a more compact form'
144
- s.action do |args, options|
145
- process_log(options, args)
146
- end
147
- end
148
-
149
- c.command(:tally) do |s|
150
- s.description "Tally a user's posts by subreddit"
151
- s.option 'count', '-c', '--count', 'Sort output by number of comments'
152
- s.action do |args, options|
153
- process_tally(options, args)
154
- end
155
- end
156
- end
157
- super
158
- end
159
-
160
- def process(options, args)
161
- quit "Do you want to tally or log posts?", :usage
162
- end
163
-
164
- def process_log(options, args)
165
- raise ArgumentError.new('You must specify a username') if args.empty?
166
- oneline = !!options['oneline']
167
- username = args.shift
168
- subreddits = args.subreddits
169
-
170
- redditor = client.new(username)
171
- posts = redditor.posts
172
-
173
- res = filter_entries('posts', redditor, posts, subreddits) >>
174
- lambda { |r| ensure_entries('posts', redditor, r.value) }
175
-
176
- quit res.value if res.left?
177
- posts = res.value
178
-
179
- formatter = (oneline ? USaidWat::CLI::CompactPostFormatter : USaidWat::CLI::PostFormatter).new
180
- page
181
- posts.each { |p| print formatter.format(p) }
182
- end
183
-
184
- def process_tally(options, args)
185
- raise ArgumentError.new('You must specify a username') if args.empty?
186
- raise ArgumentError.new('You cannot specify a subreddit when tallying comments') if args.count > 1
187
- username = args.first
188
-
189
- redditor = client.new(username)
190
- quit "#{redditor.username} has no posts." if redditor.posts.empty?
191
- partition_data = partition(redditor.posts, options['count'])
192
- formatter = USaidWat::CLI::TallyFormatter.new
193
- print formatter.format(partition_data)
194
- rescue USaidWat::Client::NoSuchUserError
195
- quit "No such user: #{username}", :no_such_user
196
- end
197
- end
198
-
199
- class Tally < Command
200
- include CountCommand
201
-
202
- def initialize(prog)
203
- prog.command(:tally) do |c|
204
- c.alias :t
205
- c.option 'count', '-c', '--count', 'Sort output by number of comments'
206
-
207
- c.action do |args, options|
208
- process(options, args)
209
- end
210
- end
211
- super
212
- end
213
-
214
- def process(options, args)
215
- raise ArgumentError.new('You must specify a username') if args.empty?
216
- raise ArgumentError.new('You cannot specify a subreddit when tallying comments') if args.count > 1
217
- username = args.first
218
-
219
- redditor = client.new(username)
220
- quit "#{redditor.username} has no comments." if redditor.comments.empty?
221
- partition_data = partition(redditor.comments, options['count'])
222
- formatter = USaidWat::CLI::TallyFormatter.new
223
- print formatter.format(partition_data)
224
- rescue USaidWat::Client::NoSuchUserError
225
- quit "No such user: #{username}", :no_such_user
226
- end
227
- end
228
- end
229
- end