npb_api 0.3.0 → 0.3.1

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. checksums.yaml +4 -4
  2. data/lib/npb_api/game.rb +46 -37
  3. data/lib/npb_api.rb +1 -0
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d1baf5a1597fb8cedf2d72dc2a530f2f05f468c
4
- data.tar.gz: 4a5fd49ffc2a2a608060f0e635c5e26228f5ab27
3
+ metadata.gz: ae80da680bd018222f39c1e9bf838304cb576c06
4
+ data.tar.gz: 68e691c98c6bec6be81eb027db5809a111118e79
5
5
  SHA512:
6
- metadata.gz: 78e35d9ffce8239f2a7f3ec39040d276541238cc8ef9bc10bc3fae387535ea75f65e863f7e69c71ba98c12514e671147dbdf92cf43ad549ea7b09a5d5ce9385f
7
- data.tar.gz: 49d9bb924687f7589436d1a8224f5f1a92571e38d08a5f34d9d51444ff77038adcfea6a11e8f6309b01d78080624c3556025657da36f8af0eba6c081dfd9fc40
6
+ metadata.gz: 653f98a9a30ee58ce690ece84176104bf891da4f40a115d55e3f403631e031391449d7a67aae745c16cd99bad5a0c26ca746469a9ca05f9f9364f1f4692ce192
7
+ data.tar.gz: cfed9a76fdb4c9a57bfb2284bbcb1938458f3ae6eafde9be7e1b6f226771350a9cc8d5d95667390b3022a879528fd6ace049a2c42bcefaf14e1a871f8fded62e
data/lib/npb_api/game.rb CHANGED
@@ -1,72 +1,71 @@
1
1
  class NpbApi::Game
2
2
  def initialize(date: '', id: '')
3
3
  @date = date
4
- @id = date.year.to_s + format('%02d', date.month) + format('%02d', date.day) + id
4
+ @id = (date.year.to_s + format('%02d', date.month) + format('%02d', date.day) + id).to_i
5
5
  @doc = Nokogiri::HTML(open("http://baseball.yahoo.co.jp/npb/game/#{@id}/top"))
6
6
  end
7
7
 
8
8
  def away
9
- {
10
- id: @id.to_i,
11
- status: status,
12
- team: @doc.css('.column-right.clearfix i').text,
13
- score: away_score,
14
- result: away_result,
15
- datetime: datetime,
16
- ballpark: ballpark
17
- }
9
+ info(:away)
18
10
  end
19
11
 
20
12
  def home
13
+ info(:home)
14
+ end
15
+
16
+ private
17
+
18
+ def info(home_or_away)
21
19
  {
22
- id: @id.to_i,
20
+ id: @id,
23
21
  status: status,
24
- team: @doc.css('.column-left.clearfix i').text,
25
- score: home_score,
26
- result: home_result,
22
+ team_id: team_id(home_or_away),
23
+ score: score(home_or_away),
24
+ result: result(home_or_away),
27
25
  datetime: datetime,
28
26
  ballpark: ballpark
29
27
  }
30
28
  end
31
29
 
32
- private
33
-
34
30
  def status
35
31
  @doc.css('.column-center em').text
36
32
  end
37
33
 
38
- def away_score
34
+ def score(home_or_away)
39
35
  return nil if status == '試合前中止'
40
- @doc.css('tr#tb1 td.sum').text.to_i
36
+
37
+ case home_or_away
38
+ when :away then @doc.css('tr#tb1 td.sum').text.to_i
39
+ when :home then @doc.css('tr#tb2 td.sum').text.to_i
40
+ end
41
41
  end
42
42
 
43
- def away_result
43
+ def result(home_or_away)
44
44
  return 'postponed' if status.include?('中止')
45
45
 
46
- if away_score > home_score
47
- 'win'
48
- elsif away_score < home_score
49
- 'lose'
50
- else
51
- 'draw'
46
+ case home_or_away
47
+ when :away
48
+ if score(:away) > score(:home)
49
+ 'win'
50
+ elsif score(:away) < score(:home)
51
+ 'lose'
52
+ else
53
+ 'draw'
54
+ end
55
+ when :home
56
+ if score(:home) > score(:away)
57
+ 'win'
58
+ elsif score(:home) < score(:away)
59
+ 'lose'
60
+ else
61
+ 'draw'
62
+ end
52
63
  end
53
64
  end
54
65
 
55
- def home_score
56
- return nil if status == '試合前中止'
57
- @doc.css('tr#tb2 td.sum').text.to_i
58
- end
59
-
60
66
  def home_result
61
67
  return 'postponed' if status.include?('中止')
62
68
 
63
- if home_score > away_score
64
- 'win'
65
- elsif home_score < away_score
66
- 'lose'
67
- else
68
- 'draw'
69
- end
70
69
  end
71
70
 
72
71
  def datetime
@@ -77,4 +76,14 @@ class NpbApi::Game
77
76
  def ballpark
78
77
  @doc.css('p.stadium').children[0].text.strip
79
78
  end
79
+
80
+ def team_id(home_or_away)
81
+ anchor =
82
+ case home_or_away
83
+ when :home then @doc.css('.column-left.clearfix a')
84
+ when :away then @doc.css('.column-right.clearfix a')
85
+ end
86
+ anchor.attribute('href').value =~ /\A\/npb\/teams\/(\d+)\/\z/
87
+ $1.to_i
88
+ end
80
89
  end
data/lib/npb_api.rb CHANGED
@@ -7,3 +7,4 @@ require 'npb_api/game'
7
7
  require 'npb_api/hitting_stats'
8
8
  require 'npb_api/pitching_stats'
9
9
  require 'npb_api/player'
10
+ require 'npb_api/team'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: npb_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ohtsuka Takahiro