playwhe 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d783ee5066f3954ae5f3afbfedbaac7e963f14b
4
+ data.tar.gz: ef9392125d15a142ca70a5a3a29a6d2b678ea96a
5
+ SHA512:
6
+ metadata.gz: 26252985bd2b160a75c1127a1cb1d48d1bbc07832b973402bc1d115a08008ebd8e641cc371b287a4a50cf587f06c5df6917e110a0c53aae5d8be85604d8ac14d
7
+ data.tar.gz: ac4941653662988a5570b7fdc7c15bb4ea80671ae8c4fd0e78184d541d7552263c1776e6b31162901f6b7b4cc1e55889ddb7e71f63eeede34b1ad7b71f5f6076
data/.gitignore CHANGED
@@ -1,18 +1,3 @@
1
1
  *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .rvmrc
6
- .yardoc
2
+ .ruby-version
7
3
  Gemfile.lock
8
- InstalledFiles
9
- _yardoc
10
- coverage
11
- doc/
12
- lib/bundler/man
13
- pkg
14
- rdoc
15
- spec/reports
16
- test/tmp
17
- test/version_tmp
18
- tmp
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in playwhe.gemspec
4
3
  gemspec
data/LICENSE CHANGED
@@ -1,22 +1,21 @@
1
- Copyright (c) 2012 Dwayne R. Crooks
2
-
3
1
  MIT License
4
2
 
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
3
+ Copyright (c) 2016 Dwayne Crooks
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
12
11
 
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
15
14
 
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,106 +1,115 @@
1
- # Play Whe
1
+ ## About
2
2
 
3
- A ruby gem for retrieving and storing Play Whe results.
4
-
5
- The gem provides a ruby API and script for retrieving and storing Play Whe
6
- results from the National Lotteries Control Board (NLCB) website at
7
- http://www.nlcb.co.tt/.
8
-
9
- ## Quick Start
10
-
11
- $ gem install playwhe
12
- $ playwhe -b && playwhe -u
13
-
14
- $ echo Fetch results for October, 2010
15
- $ playwhe -f 2010-10
16
-
17
- $ echo Fetch results for 10 October, 2010
18
- $ playwhe -f 2010-10-10
3
+ playwhe is a Ruby library for retrieving Play Whe results from the [National Lotteries
4
+ Control Board](http://www.nlcb.co.tt/) (NLCB) website.
19
5
 
20
6
  ## Installation
21
7
 
22
8
  Add this line to your application's Gemfile:
23
9
 
24
- gem 'playwhe'
10
+ ```ruby
11
+ gem "playwhe"
12
+ ```
25
13
 
26
14
  And then execute:
27
15
 
28
- $ bundle
16
+ ```bash
17
+ $ bundle
18
+ ```
29
19
 
30
20
  Or install it yourself as:
31
21
 
32
- $ gem install playwhe
33
-
34
- ## Usage
35
-
36
- After installation you will have access to both the library and an executable.
37
-
38
- To use the library, simply require **playwhe** and you would be able to retrieve
39
- Play Whe results in your code. If you need a way to persist those results, then
40
- you can also require **playwhe/storage**.
41
-
42
- The executable is called **playwhe** and it allows you to
43
-
44
- 1. Setup a directory with a database of Play Whe results.
45
-
46
- $ playwhe --bootstrap # or playwhe -b
47
-
48
- 2. Create and initialize a database for storing Play Whe results.
49
-
50
- $ playwhe --create # or playwhe -c
51
-
52
- 3. Update a database with the latest Play Whe results.
53
-
54
- $ playwhe --update # or playwhe -u
55
-
56
- Note that you can create and update one after the other with the following
57
- command.
22
+ ```bash
23
+ $ gem install playwhe
24
+ ```
58
25
 
59
- $ playwhe -cu
26
+ Inside of your Ruby program do:
60
27
 
61
- 4. Fetch individual results by month
28
+ ```ruby
29
+ require "playwhe"
30
+ ```
62
31
 
63
- $ echo Fetch results for April, 2012
64
- $ playwhe --fetch 2012-04 # or playwhe -f 2012-04
32
+ ... to pull it in as a dependency.
65
33
 
66
- or by day.
67
-
68
- $ echo Fetch results for 2nd April, 2012
69
- $ playwhe --fetch 2012-04-02 # or playwhe -f 2012-04-02
70
-
71
- Note that the fetch is done using a local database of results. So, you must
72
- have created and updated your own local database before running this command.
73
-
74
- ## Dev Notes
75
-
76
- How to access the library?
77
-
78
- $ irb -Ilib --simple-prompt
79
- >> require 'playwhe'
80
- >> require 'playwhe/storage'
81
-
82
- How to update **data/playwhe.db**?
83
-
84
- >> PlayWhe::Storage.update './data'
85
-
86
- How to update **data/playwhe.db** using **bin/playwhe**?
87
-
88
- $ ruby -Ilib ./bin/playwhe -l debug -u ./data
89
-
90
- How to access the data in **data/playwhe.db**?
91
-
92
- >> PlayWhe::Storage.connect './data/playwhe.db'
93
- >> PlayWhe::Storage::Result.first # for e.g. retrieve the first result
94
-
95
- How to access the data in **data/playwhe.db** using **bin/playwhe**?
96
-
97
- $ ruby -Ilib ./bin/playwhe -f 1994-07-04 ./data
98
-
99
- ## Help
100
-
101
- For help using the executable, try
102
-
103
- $ playwhe --help # or playwhe -h
34
+ ## Usage
104
35
 
105
- You can also get help, report bugs, make suggestions or ask questions by
106
- contacting Dwayne R. Crooks via email at me@dwaynecrooks.com.
36
+ This gem comes with a CLI called `playwhe`. Below are some examples of its usage.
37
+
38
+ List the 4 most recent results in descending order:
39
+
40
+ ```bash
41
+ $ playwhe
42
+ # or
43
+ $ playwhe --most-recent 4
44
+ # or
45
+ $ playwhe --most-recent --order desc
46
+ 15253,2016-07-02,29,PM
47
+ 15252,2016-07-02,30,AN
48
+ 15251,2016-07-02,31,AM
49
+ 15250,2016-07-02,29,EM
50
+ ```
51
+
52
+ List the 4 most recent results in ascending order:
53
+
54
+ ```bash
55
+ $ playwhe --order asc
56
+ 15250,2016-07-02,29,EM
57
+ 15251,2016-07-02,31,AM
58
+ 15252,2016-07-02,30,AN
59
+ 15253,2016-07-02,29,PM
60
+ ```
61
+
62
+ List the 8 least recent results in ascending order:
63
+
64
+ ```bash
65
+ $ playwhe --least-recent 8
66
+ 1,1994-07-04,15,AM
67
+ 2,1994-07-04,11,PM
68
+ 3,1994-07-05,36,AM
69
+ 4,1994-07-05,31,PM
70
+ 5,1994-07-06,12,AM
71
+ 6,1994-07-06,36,PM
72
+ 7,1994-07-07,6,AM
73
+ 8,1994-07-07,23,PM
74
+ ```
75
+
76
+ List all the results for the year 2015 in ascending order:
77
+
78
+ ```bash
79
+ $ playwhe 2015
80
+ 13562,2015-01-01,29,EM
81
+ 13563,2015-01-01,29,AM
82
+ 13564,2015-01-01,1,PM
83
+ ...
84
+ 14630,2015-12-31,15,EM
85
+ 14631,2015-12-31,13,AM
86
+ 14632,2015-12-31,28,AN
87
+ 14633,2015-12-31,27,PM
88
+ ```
89
+
90
+ List all the results for April 2016 in descending order:
91
+
92
+ ```bash
93
+ $ playwhe 2016 4 --order desc
94
+ 15041,2016-04-30,3,PM
95
+ 15040,2016-04-30,6,AN
96
+ 15039,2016-04-30,31,AM
97
+ 15038,2016-04-30,27,EM
98
+ ...
99
+ 14941,2016-04-01,3,PM
100
+ 14940,2016-04-01,16,AN
101
+ 14939,2016-04-01,14,AM
102
+ 14938,2016-04-01,31,EM
103
+ ```
104
+
105
+ List all the results for October 10th, 2008 in ascending order:
106
+
107
+ ```bash
108
+ $ playwhe 2008 10 10
109
+ 8775,2008-10-10,31,AM
110
+ 8776,2008-10-10,12,PM
111
+ ```
112
+
113
+ ## Copyright
114
+
115
+ Copyright (c) 2016 Dwayne Crooks. See [LICENSE](/LICENSE) for further details.
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
- #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "test"
5
+ t.pattern = "test/**/*_test.rb"
6
+ t.warning = false
7
+ end
8
+
9
+ task default: :test
@@ -1,88 +1,113 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'playwhe'
4
- require 'playwhe/storage'
5
- require 'trollop'
6
-
7
- DATE_PATTERN = /\A(\d{4})-(\d{2})(?:-(\d{2}))?\z/
8
-
9
- opts = Trollop::options do
10
- version "playwhe #{PlayWhe::VERSION} (c) 2012 Dwayne R. Crooks"
11
- banner <<-EOS
12
- A ruby script for retrieving and storing Play Whe results.
13
-
14
- Usage:
15
- playwhe [options] [dir]
16
- where [options] are:
17
- EOS
18
-
19
- opt :bootstrap, "Setup a given directory with a database of Play Whe results. If no directory is given then $HOME/.playwhe is used"
20
- opt :create, "Create and initialize a database for Play Whe results in the given directory. If no directory is given then $HOME/.playwhe is used"
21
- opt :update, "Update the database in the given directory with the latest Play Whe results. If no directory is given then $HOME/.playwhe is used"
22
- opt :log_level, "Determines the amount of logging to perform. It must be one of 'fatal', 'error', 'warn', 'info' or 'debug'", :default => 'info'
23
- opt :fetch, "Get results for a given month (if format is 'yyyy-mm') or day (if format is 'yyyy-mm-dd'). If a directory is given then the results are retrieved from the database contained within, otherwise it attempts to retrieve the results from the database in $HOME/.playwhe", :type => String
24
- end
3
+ require "optparse"
4
+
5
+ require_relative "../lib/playwhe"
6
+
7
+ options = {}
8
+
9
+ option_parser = OptionParser.new do |opts|
10
+ executable_name = File.basename($PROGRAM_NAME)
11
+ opts.banner = <<-BANNER
12
+ Display Play Whe results
13
+
14
+ Usage: #{executable_name} [args] [options]
15
+
16
+ Arguments:
17
+ \t<year>
18
+ \t<year> <month>
19
+ \t<year> <month> <day>
20
+
21
+ Options:
22
+ BANNER
23
+
24
+ opts.on("--most-recent N", Integer, "List the N (>= 1) most recent results") do |n|
25
+ if n >= 1
26
+ options[:most_recent] = n
27
+ else
28
+ raise ArgumentError, "invalid argument: --most-recent #{n}"
29
+ end
30
+ end
31
+
32
+ opts.on("--least-recent N", Integer, "List the N (>= 1) least recent results") do |n|
33
+ if n >= 1
34
+ options[:least_recent] = n
35
+ else
36
+ raise ArgumentError, "invalid argument: --least-recent #{n}"
37
+ end
38
+ end
25
39
 
26
- Trollop::die :log_level, "must be one of 'fatal', 'error', 'warn', 'info' or 'debug'" unless %w(fatal error warn info debug).include? opts[:log_level]
40
+ opts.on("--order asc|desc", %i(asc desc), "List in ascending or descending order") do |order|
41
+ options[:order] = order
42
+ end
27
43
 
28
- if opts[:fetch]
29
- match = opts[:fetch].match DATE_PATTERN
30
- Trollop::die :fetch, "must be in the format 'yyyy-mm' or 'yyyy-mm-dd'" unless match
44
+ opts.on("-v", "--version", "Show version information, then exit") do
45
+ puts PlayWhe::VERSION
46
+ exit
47
+ end
48
+
49
+ opts.on("-h", "--help", "Show this help, then exit") do
50
+ puts opts
51
+ exit
52
+ end
31
53
  end
32
54
 
33
55
  begin
34
- path = ARGV[0] || File.join(Dir.home, '.playwhe')
56
+ option_parser.parse!
57
+ rescue => e
58
+ STDERR.puts "Please check your options/arguments"
59
+ STDERR.puts "Error: #{e.message}"
60
+ exit 1
61
+ end
35
62
 
36
- if opts[:bootstrap]
37
- spec = Gem::Specification.find_by_name('playwhe')
38
- data = File.join(spec.gem_dir, 'data/playwhe.db')
63
+ def as_csv(results)
64
+ results.map(&:to_s).join("\n")
65
+ end
39
66
 
40
- if File.file? path
41
- puts "Sorry, file '#{path}' exists. We could not complete the operation."
67
+ def get_results(args, options)
68
+ if options.key?(:most_recent)
69
+ PlayWhe.get.most_recent(limit: options[:most_recent], order: options.fetch(:order, :desc))
70
+ elsif options.key?(:least_recent)
71
+ PlayWhe.get.least_recent(limit: options[:least_recent], order: options.fetch(:order, :asc))
72
+ elsif args.any?
73
+ nargs = args.length
74
+ order = options.fetch(:order, :asc)
75
+
76
+ if nargs == 1
77
+ year = args[0].to_i
78
+ PlayWhe.get.results_for_year(Date.new(year), order: order)
79
+ elsif nargs == 2
80
+ year = args[0].to_i
81
+ month = args[1].to_i
82
+ PlayWhe.get.results_for_month(Date.new(year, month), order: order)
83
+ elsif nargs == 3
84
+ year = args[0].to_i
85
+ month = args[1].to_i
86
+ day = args[2].to_i
87
+ PlayWhe.get.results_for_day(Date.new(year, month, day), order: order)
42
88
  else
43
- Dir.mkdir(path) unless File.directory?(path)
44
- FileUtils.cp(data, path, :verbose => true)
45
-
46
- puts "You're all setup now. Have fun!"
89
+ raise ArgumentError, "Too many arguments"
47
90
  end
48
91
  else
49
- PlayWhe::Storage.create(path, opts[:log_level]) if opts[:create]
50
- PlayWhe::Storage.update(path, opts[:log_level]) if opts[:update]
51
-
52
- if opts[:fetch]
53
- PlayWhe::Storage.connect(File.join(path, 'playwhe.db'), opts[:log_level])
54
-
55
- year = match[1].to_i
56
- month = match[2].to_i
57
-
58
- if match[3].nil?
59
- # get results for a given month
60
- begin
61
- results = PlayWhe::Storage::Result.all_by_month(year, month).reverse
62
- rescue
63
- puts "Sorry, we encountered an error. Please check the date you entered."
64
- exit 1
65
- end
66
- else
67
- # get results for a given day
68
- day = match[3].to_i
69
-
70
- begin
71
- results = PlayWhe::Storage::Result.all_by_day(year, month, day).reverse
72
- rescue
73
- puts "Sorry, we encountered an error. Please check the date you entered."
74
- exit 1
75
- end
76
- end
77
-
78
- output = results.map do |result|
79
- "Draw : #{result.draw}\nDate : #{result.date}\nPeriod: #{result.period}\nMark : #{result.mark} (#{PlayWhe::SPIRITS[result.mark]})"
80
- end.join "\n\n"
81
- puts output
82
- end
92
+ PlayWhe.get.most_recent(limit: 4, order: options.fetch(:order, :desc))
83
93
  end
84
- rescue Interrupt
85
- puts "Patience is a virtue. Sorry to see you didn't have any :). Bye!"
86
- rescue
87
- puts "Sorry, we encountered an error during processing."
88
94
  end
95
+
96
+ def main(args, options)
97
+ good_results, bad_results = get_results(args, options)
98
+ puts as_csv(good_results) if good_results.any?
99
+ rescue PlayWhe::Error => e
100
+ STDERR.puts "Sorry, we were unable to retrieve the results"
101
+ STDERR.puts "Error: #{e.message}"
102
+ exit 1
103
+ rescue ArgumentError => e
104
+ STDERR.puts "Please check your options/arguments"
105
+ STDERR.puts "Error: #{e.message}"
106
+ exit 1
107
+ rescue => e
108
+ STDERR.puts "Sorry, we encountered an unknown error"
109
+ STDERR.puts "Error: #{e.message}"
110
+ exit 1
111
+ end
112
+
113
+ main(ARGV, options)