npb-api 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d031b968900572899d35cc89576eae8b1a7b71db
4
+ data.tar.gz: d4155581192ba212a43f05d1ed58a663487fffa8
5
+ SHA512:
6
+ metadata.gz: 7ee02b4f02423f9d0ed6c290d4b6ff9ed11e3f5afb2b8ee1ca19012fd41ff1a3748489ce75e2b543497ecdbca7394e99e24a61ebf7193b9277312e2b4d0c6d6e
7
+ data.tar.gz: 472bb6285e2a8dad17d2fae7e1ffb3c76df46614daeafd6be38c254adfee6ab40291d3a950ae77fba8afcb74e304c14f7c5b393c73a5466011bcd1311ba57104
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in npb-api.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Npb::Api
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/npb/api`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'npb-api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install npb-api
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/npb-api.
36
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc 'Run tests'
9
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "npb/api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,6 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.2
4
+ dependencies:
5
+ pre:
6
+ - gem install bundler --pre
@@ -0,0 +1,106 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'pry'
4
+
5
+ class UnknownTeamError < StandardError; end
6
+
7
+ module Npb
8
+ module Api
9
+ class Player
10
+ TEAMS = %w[g t c d db s h bs f m l e]
11
+
12
+ def self.list(team: nil)
13
+ raise UnknownTeamError unless TEAMS.include?(team)
14
+ source(team).css('.rosterRegister a').each_with_object([]) do |link, arr|
15
+ link.attr('href') =~ /\/players\/(\d+)\.html/
16
+ arr << new(id: $1)
17
+ end
18
+ end
19
+
20
+ def self.all
21
+ TEAMS.each_with_object([]) do |team, arr|
22
+ arr << list(team: team)
23
+ end.flatten
24
+ end
25
+
26
+ def self.source(team)
27
+ Nokogiri::HTML(open(url(team)))
28
+ end
29
+
30
+ def self.url(team)
31
+ "http://bis.npb.or.jp/teams/rst_#{team}.html"
32
+ end
33
+
34
+ private_class_method :source, :url
35
+
36
+ def initialize(id: nil)
37
+ @id = id
38
+ end
39
+
40
+ def team
41
+ header.css('.registerTeam').text
42
+ end
43
+
44
+ def name
45
+ header.css('.registerPlayer').text
46
+ end
47
+
48
+ def number
49
+ header.css('.registerNo').text
50
+ end
51
+
52
+ def position
53
+ header.css('.registerPosition').text
54
+ end
55
+
56
+ def kana
57
+ detail[0].text
58
+ end
59
+
60
+ def birthday
61
+ detail[1].text.split('  ')[0] =~ /\A(\d{4})年(\d{1,2})月(\d{1,2})日生\z/
62
+ Date.new($1.to_i, $2.to_i, $3.to_i)
63
+ end
64
+
65
+ def height
66
+ detail[1].text.split('  ')[1] =~ /\A身長(\d{3})cm\z/
67
+ $1.to_i
68
+ end
69
+
70
+ def weight
71
+ detail[1].text.split('  ')[2] =~ /\A体重(\d{2,3})kg\z/
72
+ $1.to_i
73
+ end
74
+
75
+ def hand
76
+ detail[1].text.split('  ')[3]
77
+ end
78
+
79
+ def career
80
+ detail[2].text
81
+ end
82
+
83
+ def draft
84
+ detail[3].text
85
+ end
86
+
87
+ private
88
+
89
+ def url
90
+ "http://bis.npb.or.jp/players/#{@id}.html"
91
+ end
92
+
93
+ def source
94
+ Nokogiri::HTML(open(url))
95
+ end
96
+
97
+ def header
98
+ @header ||= source.css('#registerdivheader')
99
+ end
100
+
101
+ def detail
102
+ @detail ||= header.css('.registerDetail')
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,74 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'pry'
4
+
5
+ class UnknownLeagueError < StandardError; end
6
+ class UnknownTeamError < StandardError; end
7
+
8
+ module Npb
9
+ module Api
10
+ module Stats
11
+ class Base
12
+ TEAMS = %w[g t c d db s h bs f m l e]
13
+ LEAGUES = %w[central pacific eastern western]
14
+ TEAM_LEAGUES = [
15
+ { team: 'g', leagues: %w[central eastern] },
16
+ { team: 't', leagues: %w[central western] },
17
+ { team: 'c', leagues: %w[central western] },
18
+ { team: 'd', leagues: %w[central western] },
19
+ { team: 'db', leagues: %w[central eastern] },
20
+ { team: 's', leagues: %w[central eastern] },
21
+ { team: 'h', leagues: %w[pacific western] },
22
+ { team: 'bs', leagues: %w[pacific western] },
23
+ { team: 'f', leagues: %w[pacific eastern] },
24
+ { team: 'm', leagues: %w[pacific eastern] },
25
+ { team: 'l', leagues: %w[pacific eastern] },
26
+ { team: 'e', leagues: %w[pacific eastern] }
27
+ ]
28
+
29
+ def self.list(team, league, year)
30
+ raise UnknownTeamError unless TEAMS.include?(team)
31
+ raise UnknownLeagueError unless LEAGUES.include?(league)
32
+ source(team, league, year).css('tr.ststats').each_with_object([]) do |row, arr|
33
+ arr << new(row, team, year)
34
+ end
35
+ end
36
+
37
+ def self.all(year)
38
+ TEAM_LEAGUES.each_with_object([]) do |team_leagues, arr|
39
+ team_leagues[:leagues].each do |league|
40
+ arr << list(team_leagues[:team], league, year)
41
+ end
42
+ end.flatten
43
+ end
44
+
45
+ def self.source(team, league, year)
46
+ Nokogiri::HTML(open(url(team, league, year)))
47
+ end
48
+
49
+ def self.url(team, league, year)
50
+ case league
51
+ when 'central', 'pacific'
52
+ "http://bis.npb.or.jp/#{year}/stats/id#{url_key}1_#{team}.html"
53
+ when 'eastern', 'western'
54
+ "http://bis.npb.or.jp/#{year}/stats/id#{url_key}2_#{team}.html"
55
+ end
56
+ end
57
+
58
+ def self.url_key
59
+ raise NotImplementedError
60
+ end
61
+
62
+ private_class_method :source, :url, :url_key
63
+
64
+ attr_reader :team, :year
65
+
66
+ def initialize(row, team, year)
67
+ @row = row
68
+ @team = team
69
+ @year = year
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,73 @@
1
+ module Npb
2
+ module Api
3
+ module Stats
4
+ class Fielding < Base
5
+ def self.list(team, league, year)
6
+ raise UnknownTeamError unless TEAMS.include?(team)
7
+ raise UnknownLeagueError unless LEAGUES.include?(league)
8
+ position = ''
9
+ source(team, league, year).css('#stdivmaintbl tr').each_with_object([]) do |row, arr|
10
+ position = new_position(row) unless row.css('th.sthdfplayer').empty?
11
+ arr << new(row, team, year, position) if ststats?(row)
12
+ end
13
+ end
14
+
15
+ def self.ststats?(row)
16
+ !row.attributes['class'].nil? && row.attributes['class'].value == 'ststats'
17
+ end
18
+
19
+ def self.new_position(row)
20
+ row.css('th.sthdfplayer').text =~ /【(.+)】/; $1
21
+ end
22
+
23
+ def self.url_key
24
+ 'f'
25
+ end
26
+
27
+ private_class_method :ststats?, :new_position
28
+
29
+ attr_reader :position
30
+
31
+ def initialize(row, team, year, position)
32
+ @row = row
33
+ @team = team
34
+ @year = year
35
+ @position = position
36
+ end
37
+
38
+ def player
39
+ @row.children[3].children.text
40
+ end
41
+
42
+ def games
43
+ @row.children[5].children.text.to_i
44
+ end
45
+
46
+ def put_outs
47
+ @row.children[7].children.text.to_i
48
+ end
49
+
50
+ def assists
51
+ @row.children[9].children.text.to_i
52
+ end
53
+
54
+ def errors
55
+ @row.children[11].children.text.to_i
56
+ end
57
+
58
+ def double_plays
59
+ @row.children[13].children.text.to_i
60
+ end
61
+
62
+ def passed_ball
63
+ value = @row.children[15].children.text
64
+ value == '' ? nil : value.to_i
65
+ end
66
+
67
+ def fielding_average
68
+ @row.children[17].children.text.to_f
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,103 @@
1
+ module Npb
2
+ module Api
3
+ module Stats
4
+ class Hitting < Base
5
+ def self.url_key
6
+ 'b'
7
+ end
8
+
9
+ def player
10
+ @row.children[2].children.text
11
+ end
12
+
13
+ def games
14
+ @row.children[4].text.to_i
15
+ end
16
+
17
+ def total_plate_appearances
18
+ @row.children[5].text.to_i
19
+ end
20
+
21
+ def at_bats
22
+ @row.children[6].text.to_i
23
+ end
24
+
25
+ def runs
26
+ @row.children[7].text.to_i
27
+ end
28
+
29
+ def hits
30
+ @row.children[8].text.to_i
31
+ end
32
+
33
+ def double
34
+ @row.children[9].text.to_i
35
+ end
36
+
37
+ def triple
38
+ @row.children[10].text.to_i
39
+ end
40
+
41
+ def home_runs
42
+ @row.children[11].text.to_i
43
+ end
44
+
45
+ def total_bases
46
+ @row.children[13].text.to_i
47
+ end
48
+
49
+ def runs_batted_in
50
+ @row.children[14].text.to_i
51
+ end
52
+
53
+ def stolen_bases
54
+ @row.children[15].text.to_i
55
+ end
56
+
57
+ def caught_stealing
58
+ @row.children[16].text.to_i
59
+ end
60
+
61
+ def sacrifice_hits
62
+ @row.children[17].text.to_i
63
+ end
64
+
65
+ def sacrifice_flies
66
+ @row.children[18].text.to_i
67
+ end
68
+
69
+ def bases_on_balls
70
+ @row.children[19].text.to_i
71
+ end
72
+
73
+ def intentional_bases_on_balls
74
+ @row.children[21].text.to_i
75
+ end
76
+
77
+ def hit_by_pitch
78
+ @row.children[22].text.to_i
79
+ end
80
+
81
+ def strikeouts
82
+ @row.children[23].text.to_i
83
+ end
84
+
85
+ def grounded_into_double_play
86
+ @row.children[24].text.to_i
87
+ end
88
+
89
+ def batting_average
90
+ @row.children[25].text.to_f
91
+ end
92
+
93
+ def slugging_percentage
94
+ @row.children[26].text.to_f
95
+ end
96
+
97
+ def on_base_percentage
98
+ @row.children[27].text.to_f
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,107 @@
1
+ module Npb
2
+ module Api
3
+ module Stats
4
+ class Pitching < Base
5
+ def self.url_key
6
+ 'p'
7
+ end
8
+
9
+ def player
10
+ @row.children[2].text
11
+ end
12
+
13
+ def games
14
+ @row.children[4].text.to_i
15
+ end
16
+
17
+ def wins
18
+ @row.children[5].text.to_i
19
+ end
20
+
21
+ def loses
22
+ @row.children[6].text.to_i
23
+ end
24
+
25
+ def saves
26
+ @row.children[7].text.to_i
27
+ end
28
+
29
+ def holds
30
+ @row.children[8].text.to_i
31
+ end
32
+
33
+ def hold_points
34
+ @row.children[9].text.to_i
35
+ end
36
+
37
+ def complete_games
38
+ @row.children[10].text.to_i
39
+ end
40
+
41
+ def shutouts
42
+ @row.children[11].text.to_i
43
+ end
44
+
45
+ def without_walking
46
+ @row.children[12].text.to_i
47
+ end
48
+
49
+ def winning_percentage
50
+ @row.children[14].text.to_f
51
+ end
52
+
53
+ def total_batters_faced
54
+ @row.children[15].text.to_i
55
+ end
56
+
57
+ def innings_pitched
58
+ (@row.children[16].text + @row.children[17].text).to_f
59
+ end
60
+
61
+ def hits
62
+ @row.children[18].text.to_i
63
+ end
64
+
65
+ def home_runs
66
+ @row.children[20].text.to_i
67
+ end
68
+
69
+ def walks
70
+ @row.children[21].text.to_i
71
+ end
72
+
73
+ def intentional_walks
74
+ @row.children[22].text.to_i
75
+ end
76
+
77
+ def hit_by_pitch
78
+ @row.children[23].text.to_i
79
+ end
80
+
81
+ def strikeouts
82
+ @row.children[24].text.to_i
83
+ end
84
+
85
+ def wild_pitches
86
+ @row.children[25].text.to_i
87
+ end
88
+
89
+ def balks
90
+ @row.children[26].text.to_i
91
+ end
92
+
93
+ def runs
94
+ @row.children[27].text.to_i
95
+ end
96
+
97
+ def earned_runs
98
+ @row.children[28].text.to_i
99
+ end
100
+
101
+ def earned_run_average
102
+ @row.children[29].text.to_f
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,5 @@
1
+ module Npb
2
+ module Api
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
data/lib/npb/api.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "npb/api/version"
2
+
3
+ module Npb
4
+ module Api
5
+ # Your code goes here...
6
+ end
7
+ end
data/npb-api.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'npb/api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "npb-api"
8
+ spec.version = Npb::Api::VERSION
9
+ spec.authors = ["ohtsuka"]
10
+ spec.email = ["t.o.0526@gmail.com"]
11
+
12
+ spec.summary = %q{Write a short summary, because Rubygems requires one.}
13
+ spec.description = %q{Write a longer description or delete this line.}
14
+ spec.homepage = "http://example.com"
15
+ spec.licenses = ['me']
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency 'minitest', '>= 0'
25
+ spec.add_development_dependency 'pry', '>= 0'
26
+ spec.add_runtime_dependency 'nokogiri', '>= 0'
27
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: npb-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ohtsuka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Write a longer description or delete this line.
84
+ email:
85
+ - t.o.0526@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
97
+ - circle.yml
98
+ - lib/npb/api.rb
99
+ - lib/npb/api/player.rb
100
+ - lib/npb/api/stats/base.rb
101
+ - lib/npb/api/stats/fielding.rb
102
+ - lib/npb/api/stats/hitting.rb
103
+ - lib/npb/api/stats/pitching.rb
104
+ - lib/npb/api/version.rb
105
+ - npb-api.gemspec
106
+ homepage: http://example.com
107
+ licenses:
108
+ - me
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.4.8
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Write a short summary, because Rubygems requires one.
130
+ test_files: []
131
+ has_rdoc: