playwhe 0.0.1 → 0.1.0

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.
data/README.md CHANGED
@@ -2,8 +2,20 @@
2
2
 
3
3
  A ruby gem for retrieving and storing Play Whe results.
4
4
 
5
- The gem provides a ruby API for retrieving and storing Play Whe results from the
6
- National Lotteries Control Board (NLCB) website at http://www.nlcb.co.tt/.
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
7
19
 
8
20
  ## Installation
9
21
 
@@ -19,7 +31,76 @@ Or install it yourself as:
19
31
 
20
32
  $ gem install playwhe
21
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.
58
+
59
+ $ playwhe -cu
60
+
61
+ 4. Fetch individual results by month
62
+
63
+ $ echo Fetch results for April, 2012
64
+ $ playwhe --fetch 2012-04 # or playwhe -f 2012-04
65
+
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
+
22
99
  ## Help
23
100
 
24
- You can get help, report bugs, make suggestions or ask questions by contacting
25
- Dwayne R. Crooks via email at me@dwaynecrooks.com.
101
+ For help using the executable, try
102
+
103
+ $ playwhe --help # or playwhe -h
104
+
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.
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env ruby
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
25
+
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]
27
+
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
31
+ end
32
+
33
+ begin
34
+ path = ARGV[0] || File.join(Dir.home, '.playwhe')
35
+
36
+ if opts[:bootstrap]
37
+ spec = Gem::Specification.find_by_name('playwhe')
38
+ data = File.join(spec.gem_dir, 'data/playwhe.db')
39
+
40
+ if File.file? path
41
+ puts "Sorry, file '#{path}' exists. We could not complete the operation."
42
+ 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!"
47
+ end
48
+ 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
83
+ 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
+ end
Binary file
@@ -20,6 +20,24 @@ module PlayWhe
20
20
  validates_within :period, set: 1..3
21
21
  validates_within :mark, set: PlayWhe::LOWEST_MARK..PlayWhe::HIGHEST_MARK
22
22
 
23
+ def self.all_by_day(year, month, day)
24
+ self.all(:date => Date.new(year, month, day))
25
+ end
26
+
27
+ def self.all_by_month(year, month)
28
+ a = Date.new year, month, 1 # start of the month
29
+ b = (a >> 1) - 1 # end of the month
30
+
31
+ self.all(:date => a..b)
32
+ end
33
+
34
+ def self.all_by_year(year)
35
+ a = Date.new year, 1, 1 # start of the year
36
+ b = (a >> 12) - 1 # end of the year
37
+
38
+ self.all(:date => a..b)
39
+ end
40
+
23
41
  private
24
42
 
25
43
  def check_draw
@@ -1,3 +1,3 @@
1
1
  module PlayWhe
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -6,13 +6,15 @@ Gem::Specification.new do |gem|
6
6
  gem.email = ["me@dwaynecrooks.com"]
7
7
  gem.description = %q{A ruby gem for retrieving and storing Play Whe results.
8
8
 
9
- The gem provides a ruby API for retrieving and storing Play Whe results from
10
- the National Lotteries Control Board (NLCB) website at http://www.nlcb.co.tt/.}
9
+ The gem provides a ruby API and script for retrieving and storing Play Whe
10
+ results from the National Lotteries Control Board (NLCB) website at
11
+ http://www.nlcb.co.tt/.}
11
12
  gem.summary = %q{A ruby gem for retrieving and storing Play Whe results.}
12
13
  gem.homepage = "http://rubygems.org/gems/playwhe"
13
14
 
14
15
  gem.add_dependency('data_mapper', '~> 1.2.0')
15
16
  gem.add_dependency('dm-sqlite-adapter', '~> 1.2.0')
17
+ gem.add_dependency('trollop', '~> 1.16.2')
16
18
 
17
19
  gem.files = `git ls-files`.split($\)
18
20
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: playwhe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-28 00:00:00.000000000 Z
12
+ date: 2012-06-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: data_mapper
@@ -43,12 +43,29 @@ dependencies:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
45
  version: 1.2.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: trollop
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.16.2
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: 1.16.2
46
62
  description: ! "A ruby gem for retrieving and storing Play Whe results.\n\n The gem
47
- provides a ruby API for retrieving and storing Play Whe results from\n the National
48
- Lotteries Control Board (NLCB) website at http://www.nlcb.co.tt/."
63
+ provides a ruby API and script for retrieving and storing Play Whe\n results from
64
+ the National Lotteries Control Board (NLCB) website at\n http://www.nlcb.co.tt/."
49
65
  email:
50
66
  - me@dwaynecrooks.com
51
- executables: []
67
+ executables:
68
+ - playwhe
52
69
  extensions: []
53
70
  extra_rdoc_files: []
54
71
  files:
@@ -57,6 +74,7 @@ files:
57
74
  - LICENSE
58
75
  - README.md
59
76
  - Rakefile
77
+ - bin/playwhe
60
78
  - data/playwhe.db
61
79
  - lib/playwhe.rb
62
80
  - lib/playwhe/storage.rb