hnews 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/bin/hnews +176 -0
  2. data/hnews.rdoc +64 -0
  3. data/lib/hnews_version.rb +3 -0
  4. metadata +135 -0
data/bin/hnews ADDED
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env ruby
2
+ # 1.9 adds realpath to resolve symlinks; 1.8 doesn't
3
+ # have this method, so we add it so we get resolved symlinks
4
+ # and compatibility
5
+
6
+ require 'hn'
7
+ require 'paint'
8
+ require 'json'
9
+ require 'launchy'
10
+
11
+
12
+
13
+ unless File.respond_to? :realpath
14
+ class File #:nodoc:
15
+ def self.realpath path
16
+ return realpath(File.readlink(path)) if symlink?(path)
17
+ path
18
+ end
19
+ end
20
+ end
21
+ $: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
22
+ require 'rubygems'
23
+ require 'gli'
24
+ require 'hnews_version'
25
+
26
+ include GLI
27
+
28
+ program_desc 'Hacker News CLI'
29
+
30
+ version HNews::VERSION
31
+
32
+ desc 'Get Best Items'
33
+ arg_name 'none'
34
+ command :best do |c|
35
+ c.action do |global_options,options,args|
36
+
37
+ hnData = HackerNews::Engine.best
38
+ cachePage hnData
39
+ genericRender hnData
40
+
41
+ end
42
+ end
43
+
44
+ desc 'Get Home Items'
45
+ arg_name 'none'
46
+ command :home do |c|
47
+ c.action do |global_options,options,args|
48
+
49
+ hnData = HackerNews::Engine.homepage
50
+ cachePage hnData
51
+ genericRender hnData
52
+
53
+ end
54
+ end
55
+
56
+ desc 'Get New Items'
57
+ arg_name 'pageNumber'
58
+ command :new do |c|
59
+ c.action do |global_options,options,args|
60
+ hnData = HackerNews::Engine.newest
61
+ cachePage hnData
62
+ genericRender hnData
63
+ end
64
+ end
65
+
66
+ desc 'Upvote Item from last Loaded Page'
67
+ arg_name 'lastPageItemID'
68
+ command :upvote do |c|
69
+ c.action do |global_options,options,args|
70
+ end
71
+ end
72
+
73
+ desc 'Open Item from last Loaded Page'
74
+ arg_name 'lastPageItemID'
75
+ command :open do |c|
76
+ c.action do |global_options,options,args|
77
+ cachedPage = getCachedPage()
78
+
79
+ puts "Opening: " + cachedPage[Integer(args[0])]['link']
80
+ Launchy.open(cachedPage[Integer(args[0])]['link'])
81
+ end
82
+ end
83
+
84
+ desc 'Open Comments Page from last loaded page'
85
+ arg_name 'lastPageItemID'
86
+ command :comments do |c|
87
+ c.action do |global_options,options,args|
88
+ end
89
+ end
90
+
91
+ pre do |global,command,options,args|
92
+ # Pre logic here
93
+ # Return true to proceed; false to abort and not call the
94
+ # chosen command
95
+ # Use skips_pre before a command to skip this block
96
+ # on that command only
97
+ true
98
+ end
99
+
100
+ post do |global,command,options,args|
101
+ # Post logic here
102
+ # Use skips_post before a command to skip this
103
+ # block on that command only
104
+ end
105
+
106
+ on_error do |exception|
107
+ # Error logic here
108
+ # return false to skip default error handling
109
+ true
110
+ end
111
+
112
+ # Our Stuff
113
+ def cachePage (itemArray)
114
+ itemCount = 1
115
+ toSave = []
116
+ cacheVars = ['title', 'link', 'siteline', 'site', 'points', 'username', 'num_comments', 'time_string']
117
+ itemArray.each do |item|
118
+
119
+ toSave[itemCount] = {}
120
+
121
+ cacheVars.each do |cacheVar|
122
+ if defined? item[cacheVar]
123
+ toSave[itemCount][cacheVar] = item[cacheVar]
124
+ end
125
+ end
126
+
127
+ itemCount = itemCount + 1
128
+ end
129
+
130
+ File.open(File.expand_path("~/.hnews.cache"),"w") do |f|
131
+ f.write(toSave.to_json)
132
+ end
133
+
134
+ end
135
+
136
+ def getCachedPage ()
137
+ File.open(File.expand_path("~/.hnews.cache"),"r") do |f|
138
+ return JSON.parse(f.read)
139
+ end
140
+ end
141
+
142
+ def genericRender (hnData)
143
+ count = 1
144
+ hnDataHighestNum = hnData.length
145
+ highestNumLen = hnDataHighestNum.to_s().length
146
+ line = []
147
+ cachePage(hnData)
148
+ hnData.each do |item|
149
+
150
+ line[1] = Paint['[' + count.to_s() + ']', :yellow]
151
+ for i in -1...highestNumLen - count.to_s().length
152
+ line[1] = line[1] + ' '
153
+ end
154
+
155
+ line[1] = line[1] + Paint[item.title, :white]
156
+
157
+ if defined? item.siteline
158
+ line[1] = line[1] + ' ' + Paint['(' + item.site + ')', "666"]
159
+ end
160
+
161
+ puts line[1]
162
+
163
+ line[2] = ''
164
+ for i in -3...highestNumLen
165
+ line[2] = line[2] + ' '
166
+ end
167
+
168
+ line[2] = line[2] + Paint[item.points.to_s() + ' points by ' + item.username.to_s() + ' | ' + item.num_comments.to_s() + ' comments ' + item.time_string.to_s(), "666"]
169
+ puts line[2]
170
+ count = count + 1
171
+ end
172
+ end
173
+
174
+ exit GLI.run(ARGV)
175
+
176
+
data/hnews.rdoc ADDED
@@ -0,0 +1,64 @@
1
+ = <tt>hnews</tt>
2
+
3
+ Describe your application here
4
+
5
+ hnews [global options] command_name [command-specific options] [--] arguments...
6
+
7
+ * Use the command +help+ to get a summary of commands
8
+ * Use the command <tt>help command_name</tt> to get a help for +command_name+
9
+ * Use <tt>--</tt> to stop command line argument processing; useful if your arguments have dashes in them
10
+
11
+ == Global Options
12
+ These options are available for any command and are specified before the name of the command
13
+
14
+ [<tt>-f, --flagname=The name of the argument</tt>] Describe some flag here <i>( default: <tt>the default</tt>)</i>
15
+ [<tt>--help</tt>] Show this message
16
+ [<tt>-s, --switch</tt>] Describe some switch here
17
+ == Commands
18
+ [<tt>best</tt>] Describe best here
19
+ [<tt>comments</tt>] Open Comments Page from last loaded page
20
+ [<tt>help</tt>] Shows list of commands or help for one command
21
+ [<tt>home</tt>] Get Home Items
22
+ [<tt>new</tt>] Get New Items
23
+ [<tt>open</tt>] Open Item from last Loaded Page
24
+ [<tt>upvote</tt>] Upvote Item from last Loaded Page
25
+
26
+ === <tt>best Describe arguments to best here</tt>
27
+
28
+ Describe best here
29
+
30
+ ==== Options
31
+ These options are specified *after* the command.
32
+
33
+ [<tt>-f arg</tt>] Describe a flag to best <i>( default: <tt>default</tt>)</i>
34
+ [<tt>-s</tt>] Describe a switch to best
35
+ === <tt>comments lastPageItemID</tt>
36
+
37
+ Open Comments Page from last loaded page
38
+
39
+ === <tt>help [command]</tt>
40
+
41
+ Shows list of commands or help for one command
42
+
43
+ Gets help for the application or its commands. Can also list the commands in a way helpful to creating a bash-style completion function
44
+
45
+ ==== Options
46
+ These options are specified *after* the command.
47
+
48
+ [<tt>-c, --completion</tt>] List all commands one line at a time, for use with shell completion ([command] argument is partial command to match)
49
+ === <tt>home none</tt>
50
+
51
+ Get Home Items
52
+
53
+ === <tt>new pageNumber</tt>
54
+
55
+ Get New Items
56
+
57
+ === <tt>open lastPageItemID</tt>
58
+
59
+ Open Item from last Loaded Page
60
+
61
+ === <tt>upvote lastPageItemID</tt>
62
+
63
+ Upvote Item from last Loaded Page
64
+
@@ -0,0 +1,3 @@
1
+ module HNews
2
+ VERSION = '0.0.2'
3
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hnews
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben Evans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: gli
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: hn
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: launchy
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description:
95
+ email: ben@bensbit.co.uk
96
+ executables:
97
+ - hnews
98
+ extensions: []
99
+ extra_rdoc_files:
100
+ - hnews.rdoc
101
+ files:
102
+ - bin/hnews
103
+ - lib/hnews_version.rb
104
+ - hnews.rdoc
105
+ homepage: http://bensbit.co.uk
106
+ licenses: []
107
+ post_install_message:
108
+ rdoc_options:
109
+ - --title
110
+ - hnews
111
+ - --main
112
+ - README.rdoc
113
+ - -ri
114
+ require_paths:
115
+ - lib
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.24
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: CLI Hacker News (news.ycombinator.com)
135
+ test_files: []