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,46 @@
1
+ require "test_helper"
2
+
3
+ describe PlayWhe::HTTP::Adapter do
4
+ let(:url) { "http://example.com" }
5
+ let(:data) { { year: "16" } }
6
+
7
+ describe "#post" do
8
+ it "makes a POST request" do
9
+ http_client = Minitest::Mock.new
10
+ http_client.expect(:post, :response, [url, { form: data }])
11
+
12
+ http_adapter = PlayWhe::HTTP::Adapter.new(http_client)
13
+ http_adapter.post(url, data)
14
+
15
+ expect(http_client.verify).must_equal true
16
+ end
17
+
18
+ describe "when successful" do
19
+ it "returns a response" do
20
+ http_client = Object.new
21
+ def http_client.post(*attrs)
22
+ :response
23
+ end
24
+
25
+ http_adapter = PlayWhe::HTTP::Adapter.new(http_client)
26
+
27
+ expect(http_adapter.post(url, data)).must_be_instance_of \
28
+ PlayWhe::HTTP::Response
29
+ end
30
+ end
31
+
32
+ describe "when there is an HTTP::Error" do
33
+ it "raises PlayWhe::NetworkError" do
34
+ http_client = Object.new
35
+ def http_client.post(*attrs)
36
+ raise Class.new(HTTP::Error)
37
+ end
38
+
39
+ http_adapter = PlayWhe::HTTP::Adapter.new(http_client)
40
+
41
+ expect { http_adapter.post(url, data) }.must_raise \
42
+ PlayWhe::NetworkError
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,39 @@
1
+ require "test_helper"
2
+
3
+ describe PlayWhe::HTTP::Response do
4
+ subject { PlayWhe::HTTP::Response.new(response) }
5
+
6
+ let(:response) do
7
+ response = OpenStruct.new(status: 200)
8
+ def response.to_s
9
+ "A body"
10
+ end
11
+ response
12
+ end
13
+
14
+ it "returns the response's status" do
15
+ expect(subject.status).must_equal response.status
16
+ end
17
+
18
+ it "returns the response's body" do
19
+ expect(subject.body).must_equal response.to_s
20
+ end
21
+
22
+ describe "#ok?" do
23
+ describe "when the status is 200" do
24
+ it "returns true" do
25
+ expect(subject.ok?).must_equal true
26
+ end
27
+ end
28
+
29
+ describe "when the status is not 200" do
30
+ before do
31
+ response.status = 400
32
+ end
33
+
34
+ it "returns false" do
35
+ expect(subject.ok?).must_equal false
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,34 @@
1
+ require "test_helper"
2
+
3
+ describe PlayWhe::Parser do
4
+ describe "::parse" do
5
+ it "returns a list of the results it finds in the order it finds it" do
6
+ html_results = "<h2><strong> Draw #: </strong>1<br><strong> Date: </strong>04-Jul-94<br><strong> Mark Drawn: </strong>15<br><strong> Drawn at: </strong>AM<br></h2><br><h2><strong> Draw #: </strong>2<br><strong> Date: </strong>04-Jul-94<br><strong> Mark Drawn: </strong>11<br><strong> Drawn at: </strong>PM<br></h2><br><h2><strong> Draw #: </strong>3<br><strong> Date: </strong>05-Jul-94<br><strong> Mark Drawn: </strong>36<br><strong> Drawn at: </strong>AM<br></h2><br><h2><strong> Draw #: </strong>4<br><strong> Date: </strong>05-Jul-94<br><strong> Mark Drawn: </strong>31<br><strong> Drawn at: </strong>PM<br></h2><br><h2><strong> Draw #: </strong>5<br><strong> Date: </strong>06-Jul-94<br><strong> Mark Drawn: </strong>12<br><strong> Drawn at: </strong>AM<br></h2><br><h2><strong> Draw #: </strong>6<br><strong> Date: </strong>06-Jul-94<br><strong> Mark Drawn: </strong>36<br><strong> Drawn at: </strong>PM<br></h2><br><h2><strong> Draw #: </strong>7<br><strong> Date: </strong>07-Jul-94<br><strong> Mark Drawn: </strong>6<br><strong> Drawn at: </strong>AM<br></h2>"
7
+
8
+ results = PlayWhe::Parser.parse(html_results)
9
+
10
+ expect(results.length).must_equal 7
11
+ expect(results[0]).must_equal \
12
+ PlayWhe::Result.new \
13
+ draw: 1, date: "1994-07-04", mark: 15, period: "AM"
14
+ expect(results[1]).must_equal \
15
+ PlayWhe::Result.new \
16
+ draw: 2, date: "1994-07-04", mark: 11, period: "PM"
17
+ expect(results[2]).must_equal \
18
+ PlayWhe::Result.new \
19
+ draw: 3, date: "1994-07-05", mark: 36, period: "AM"
20
+ expect(results[3]).must_equal \
21
+ PlayWhe::Result.new \
22
+ draw: 4, date: "1994-07-05", mark: 31, period: "PM"
23
+ expect(results[4]).must_equal \
24
+ PlayWhe::Result.new \
25
+ draw: 5, date: "1994-07-06", mark: 12, period: "AM"
26
+ expect(results[5]).must_equal \
27
+ PlayWhe::Result.new \
28
+ draw: 6, date: "1994-07-06", mark: 36, period: "PM"
29
+ expect(results[6]).must_equal \
30
+ PlayWhe::Result.new \
31
+ draw: 7, date: "1994-07-07", mark: 6, period: "AM"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,158 @@
1
+ require "test_helper"
2
+
3
+ describe PlayWhe::Result do
4
+ describe "#to_s" do
5
+ it "returns a comma separated string of it's draw, date, mark and period" do
6
+ result = PlayWhe::Result.new \
7
+ draw: 1, date: "01-01-2016", mark: 5, period: "EM"
8
+
9
+ expect(result.to_s).must_equal("1,2016-01-01,5,EM")
10
+
11
+ result = PlayWhe::Result.new \
12
+ draw: 1, date: nil, mark: 5, period: "AM"
13
+
14
+ expect(result.to_s).must_equal("1,-,5,AM")
15
+ end
16
+ end
17
+
18
+ describe "#is_valid?" do
19
+ describe "when it is valid" do
20
+ it "returns true" do
21
+ result = PlayWhe::Result.new \
22
+ draw: 1, date: "01-01-2016", mark: 5, period: "EM"
23
+ context = OpenStruct.new(year: 2016, month: 1)
24
+
25
+ expect(result.is_valid?(context)).must_equal true
26
+ end
27
+ end
28
+
29
+ describe "when it is not valid" do
30
+ describe "when draw is not valid" do
31
+ it "returns false and sets errors[:draw]" do
32
+ result = PlayWhe::Result.new \
33
+ draw: "x", date: "01-01-2016", mark: 5, period: "EM"
34
+
35
+ expect(result.is_valid?).must_equal false
36
+ expect(result.errors.length).must_equal 1
37
+ expect(result.errors[:draw].length).must_equal 1
38
+ expect(result.errors[:draw].first).must_equal \
39
+ "must be a positive integer"
40
+ end
41
+ end
42
+
43
+ describe "when date is not valid" do
44
+ describe "when it is nil" do
45
+ it "returns false and sets errors[:date]" do
46
+ result = PlayWhe::Result.new \
47
+ draw: 1, date: nil, mark: 5, period: "EM"
48
+
49
+ expect(result.is_valid?).must_equal false
50
+ expect(result.errors.length).must_equal 1
51
+ expect(result.errors[:date].length).must_equal 1
52
+ expect(result.errors[:date].first).must_equal "must be a date"
53
+ end
54
+ end
55
+
56
+ describe "when it is less than Play Whe's birthday" do
57
+ it "returns false and sets errors[:date]" do
58
+ result = PlayWhe::Result.new \
59
+ draw: 1, date: PlayWhe::BIRTHDAY - 1, mark: 5, period: "EM"
60
+
61
+ expect(result.is_valid?).must_equal false
62
+ expect(result.errors.length).must_equal 1
63
+ expect(result.errors[:date].length).must_equal 1
64
+ expect(result.errors[:date].first).must_equal \
65
+ "must be greater than Play Whe's birthday"
66
+ end
67
+ end
68
+
69
+ describe "when it is not for the correct year" do
70
+ it "returns false and sets errors[:date]" do
71
+ result = PlayWhe::Result.new \
72
+ draw: 1, date: "01-01-2016", mark: 5, period: "EM"
73
+ context = OpenStruct.new(year: 2015, month: 1)
74
+
75
+ expect(result.is_valid?(context)).must_equal false
76
+ expect(result.errors.length).must_equal 1
77
+ expect(result.errors[:date].length).must_equal 1
78
+ expect(result.errors[:date].first).must_equal \
79
+ "must be for the correct year (2015)"
80
+ end
81
+ end
82
+
83
+ describe "when it is not for the correct month" do
84
+ it "returns false and sets errors[:date]" do
85
+ result = PlayWhe::Result.new \
86
+ draw: 1, date: "01-01-2016", mark: 5, period: "EM"
87
+ context = OpenStruct.new(year: 2016, month: 2)
88
+
89
+ expect(result.is_valid?(context)).must_equal false
90
+ expect(result.errors.length).must_equal 1
91
+ expect(result.errors[:date].length).must_equal 1
92
+ expect(result.errors[:date].first).must_equal \
93
+ "must be for the correct month (2)"
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "when mark is not valid" do
99
+ describe "when mark is less than PlayWhe::LOWEST_MARK" do
100
+ it "returns false and sets errors[:mark]" do
101
+ result = PlayWhe::Result.new \
102
+ draw: 1, date: "01-01-2016", mark: PlayWhe::LOWEST_MARK - 1,
103
+ period: "EM"
104
+
105
+ expect(result.is_valid?).must_equal false
106
+ expect(result.errors.length).must_equal 1
107
+ expect(result.errors[:mark].length).must_equal 1
108
+ expect(result.errors[:mark].first).must_equal \
109
+ "must be between #{PlayWhe::LOWEST_MARK} and " \
110
+ "#{PlayWhe::HIGHEST_MARK} inclusive"
111
+ end
112
+ end
113
+
114
+ describe "when mark is greater than PlayWhe::HIGHEST_MARK" do
115
+ it "returns false and sets errors[:mark]" do
116
+ result = PlayWhe::Result.new \
117
+ draw: 1, date: "01-01-2016", mark: PlayWhe::HIGHEST_MARK + 1,
118
+ period: "EM"
119
+
120
+ expect(result.is_valid?).must_equal false
121
+ expect(result.errors.length).must_equal 1
122
+ expect(result.errors[:mark].length).must_equal 1
123
+ expect(result.errors[:mark].first).must_equal \
124
+ "must be between #{PlayWhe::LOWEST_MARK} and " \
125
+ "#{PlayWhe::HIGHEST_MARK} inclusive"
126
+ end
127
+ end
128
+ end
129
+
130
+ describe "when period is not valid" do
131
+ it "retuns false and sets errors[:period]" do
132
+ result = PlayWhe::Result.new \
133
+ draw: 1, date: "01-01-2016", mark: 5, period: "X"
134
+
135
+ expect(result.is_valid?).must_equal false
136
+ expect(result.errors.length).must_equal 1
137
+ expect(result.errors[:period].length).must_equal 1
138
+ expect(result.errors[:period].first).must_equal \
139
+ "must be one of EM, AM, AN, PM"
140
+ end
141
+ end
142
+
143
+ describe "when multiple attributes aren't valid" do
144
+ it "returns false with errors set for all of the invalid ones" do
145
+ result = PlayWhe::Result.new \
146
+ draw: 1, date: PlayWhe::BIRTHDAY - 1, mark: -1, period: "X"
147
+ context = OpenStruct.new(year: 2016, month: 1)
148
+
149
+ expect(result.is_valid?(context)).must_equal false
150
+ expect(result.errors.length).must_equal 3
151
+ expect(result.errors[:date].length).must_equal 3
152
+ expect(result.errors[:mark].length).must_equal 1
153
+ expect(result.errors[:mark].length).must_equal 1
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,13 @@
1
+ require "test_helper"
2
+
3
+ describe PlayWhe::Settings do
4
+ subject { PlayWhe::Settings.config }
5
+
6
+ it "has the correct defaults" do
7
+ expect(subject.http.read_timeout).must_equal 5
8
+ expect(subject.http.write_timeout).must_equal 5
9
+ expect(subject.http.connect_timeout).must_equal 5
10
+ expect(subject.url).must_equal \
11
+ "http://nlcb.co.tt/app/index.php/pwresults/playwhemonthsum"
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ require "test_helper"
2
+
3
+ describe PlayWhe::Util do
4
+ subject { PlayWhe::Util }
5
+
6
+ describe "::normalize_year" do
7
+ it "returns the last 2 digits of the year" do
8
+ inputs = [1994, 94, 2016, 16]
9
+ outputs = %w(94 94 16 16)
10
+
11
+ inputs.zip(outputs).each do |input, output|
12
+ expect(subject.normalize_year(input)).must_equal output
13
+ expect(subject.normalize_year(input.to_s)).must_equal output
14
+ end
15
+ end
16
+ end
17
+
18
+ describe "::normalize_month" do
19
+ describe "when given a number in the range 1 to 12" do
20
+ it "returns the abbreviated month name" do
21
+ inputs = 1..12
22
+ outputs = %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
23
+
24
+ inputs.zip(outputs).each do |input, output|
25
+ expect(subject.normalize_month(input)).must_equal output
26
+ expect(subject.normalize_month(input.to_s)).must_equal output
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "when given nil" do
32
+ it "returns nil" do
33
+ expect(subject.normalize_month(nil)).must_be_nil
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,6 @@
1
+ require "ostruct"
2
+
3
+ require "pry-byebug"
4
+ require "playwhe"
5
+
6
+ require "minitest/autorun"
metadata CHANGED
@@ -1,108 +1,160 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: playwhe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
- - Dwayne R. Crooks
7
+ - Dwayne Crooks
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-06-09 00:00:00.000000000 Z
11
+ date: 2016-07-04 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: data_mapper
14
+ name: http
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: 1.2.0
19
+ version: '2.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.2
22
23
  type: :runtime
23
24
  prerelease: false
24
25
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
26
  requirements:
27
- - - ~>
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - ">="
28
31
  - !ruby/object:Gem::Version
29
- version: 1.2.0
32
+ version: 2.0.2
30
33
  - !ruby/object:Gem::Dependency
31
- name: dm-sqlite-adapter
34
+ name: dry-configurable
32
35
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
36
  requirements:
35
- - - ~>
37
+ - - "~>"
36
38
  - !ruby/object:Gem::Version
37
- version: 1.2.0
39
+ version: '0'
38
40
  type: :runtime
39
41
  prerelease: false
40
42
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
43
  requirements:
43
- - - ~>
44
+ - - "~>"
44
45
  - !ruby/object:Gem::Version
45
- version: 1.2.0
46
+ version: '0'
46
47
  - !ruby/object:Gem::Dependency
47
- name: trollop
48
+ name: bundler
48
49
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
50
  requirements:
51
- - - ~>
51
+ - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: 1.16.2
54
- type: :runtime
53
+ version: '1.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: minitest
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '5.9'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '5.9'
75
+ - !ruby/object:Gem::Dependency
76
+ name: pry-byebug
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rake
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
55
97
  prerelease: false
56
98
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
99
  requirements:
59
- - - ~>
100
+ - - "~>"
60
101
  - !ruby/object:Gem::Version
61
- version: 1.16.2
62
- description: ! "A ruby gem for retrieving and storing Play Whe results.\n\n The gem
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/."
65
- email:
66
- - me@dwaynecrooks.com
102
+ version: '0'
103
+ description: A Ruby library for retrieving Play Whe results from the National Lotteries
104
+ Control Board (NLCB) website.
105
+ email: dwayne@playwhesmarter.com
67
106
  executables:
68
107
  - playwhe
69
108
  extensions: []
70
109
  extra_rdoc_files: []
71
110
  files:
72
- - .gitignore
111
+ - ".gitignore"
73
112
  - Gemfile
74
113
  - LICENSE
75
114
  - README.md
76
115
  - Rakefile
77
116
  - bin/playwhe
78
- - data/playwhe.db
79
117
  - lib/playwhe.rb
80
- - lib/playwhe/storage.rb
81
- - lib/playwhe/storage/models.rb
118
+ - lib/playwhe/fetcher.rb
119
+ - lib/playwhe/get.rb
120
+ - lib/playwhe/http.rb
121
+ - lib/playwhe/parser.rb
122
+ - lib/playwhe/result.rb
123
+ - lib/playwhe/settings.rb
124
+ - lib/playwhe/util.rb
82
125
  - lib/playwhe/version.rb
83
126
  - playwhe.gemspec
84
- homepage: http://rubygems.org/gems/playwhe
85
- licenses: []
127
+ - test/playwhe/fetcher_test.rb
128
+ - test/playwhe/get_test.rb
129
+ - test/playwhe/http/adapter_test.rb
130
+ - test/playwhe/http/response_test.rb
131
+ - test/playwhe/parser_test.rb
132
+ - test/playwhe/result_test.rb
133
+ - test/playwhe/settings_test.rb
134
+ - test/playwhe/util_test.rb
135
+ - test/test_helper.rb
136
+ homepage: https://rubygems.org/gems/playwhe
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
86
140
  post_install_message:
87
141
  rdoc_options: []
88
142
  require_paths:
89
143
  - lib
90
144
  required_ruby_version: !ruby/object:Gem::Requirement
91
- none: false
92
145
  requirements:
93
- - - ! '>='
146
+ - - ">="
94
147
  - !ruby/object:Gem::Version
95
- version: '0'
148
+ version: '2.0'
96
149
  required_rubygems_version: !ruby/object:Gem::Requirement
97
- none: false
98
150
  requirements:
99
- - - ! '>='
151
+ - - ">="
100
152
  - !ruby/object:Gem::Version
101
153
  version: '0'
102
154
  requirements: []
103
155
  rubyforge_project:
104
- rubygems_version: 1.8.24
156
+ rubygems_version: 2.6.4
105
157
  signing_key:
106
- specification_version: 3
107
- summary: A ruby gem for retrieving and storing Play Whe results.
158
+ specification_version: 4
159
+ summary: Play Whe results should be easy to get
108
160
  test_files: []