mlb 0.0.5 → 0.1.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.
- data/README.rdoc +14 -14
- data/Rakefile +20 -36
- data/lib/mlb.rb +3 -122
- data/lib/mlb/player.rb +29 -0
- data/lib/mlb/team.rb +143 -0
- metadata +7 -5
data/README.rdoc
CHANGED
@@ -5,20 +5,20 @@
|
|
5
5
|
== Usage
|
6
6
|
$ irb
|
7
7
|
>> require 'mlb'
|
8
|
-
>> MLB.
|
9
|
-
>> MLB.
|
10
|
-
>> MLB.
|
11
|
-
>> MLB.
|
12
|
-
>> MLB.
|
13
|
-
>> MLB.
|
14
|
-
>> MLB.
|
15
|
-
>> MLB.
|
16
|
-
>> MLB.
|
17
|
-
>> MLB.
|
18
|
-
>> MLB.
|
19
|
-
>> MLB.
|
20
|
-
>> MLB.
|
21
|
-
==
|
8
|
+
>> MLB::Team.all.first.name # => "Arizona Diamondbacks"
|
9
|
+
>> MLB::Team.all.first.league # => "National League"
|
10
|
+
>> MLB::Team.all.first.division # => "National League West"
|
11
|
+
>> MLB::Team.all.first.manager # => "Bob Melvin"
|
12
|
+
>> MLB::Team.all.first.wins # => 82
|
13
|
+
>> MLB::Team.all.first.losses # => 80
|
14
|
+
>> MLB::Team.all.first.founded # => 1998
|
15
|
+
>> MLB::Team.all.first.mascot # => nil
|
16
|
+
>> MLB::Team.all.first.ballpark # => "Chase Field"
|
17
|
+
>> MLB::Team.all.first.logo_url # => "http://img.freebase.com/api/trans/image_thumb/wikipedia/images/en_id/13104064"
|
18
|
+
>> MLB::Team.all.first.players.first.name # => "Alex Romero"
|
19
|
+
>> MLB::Team.all.first.players.first.number # => 28
|
20
|
+
>> MLB::Team.all.first.players.first.position # => "Right fielder"
|
21
|
+
== Credits
|
22
22
|
Many thanks to:
|
23
23
|
* Freebase[http://www.freebase.com]
|
24
24
|
* httparty[http://github.com/jnunemaker/httparty]
|
data/Rakefile
CHANGED
@@ -8,44 +8,28 @@ AUTHOR = "Erik Michaels-Ober"
|
|
8
8
|
EMAIL = "sferik@gmail.com"
|
9
9
|
HOMEPAGE = "http://github.com/sferik/mlb"
|
10
10
|
SUMMARY = "MLB.rb is a Ruby library for retrieving current Major League Baseball players, managers, teams, divisions, and leagues."
|
11
|
-
GEM_VERSION = "0.
|
11
|
+
GEM_VERSION = "0.1.1"
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
31
|
-
pkg.gem_spec = spec
|
32
|
-
end
|
33
|
-
|
34
|
-
desc "Install the gem"
|
35
|
-
task :install do
|
36
|
-
Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
|
37
|
-
end
|
38
|
-
|
39
|
-
desc "Uninstall the gem"
|
40
|
-
task :uninstall do
|
41
|
-
Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
|
42
|
-
end
|
43
|
-
|
44
|
-
desc "Create a gemspec file"
|
45
|
-
task :gemspec do
|
46
|
-
File.open("#{GEM_NAME}.gemspec", "w") do |file|
|
47
|
-
file.puts spec.to_ruby
|
13
|
+
begin
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gemspec|
|
16
|
+
gemspec.name = GEM_NAME
|
17
|
+
gemspec.version = GEM_VERSION
|
18
|
+
gemspec.platform = Gem::Platform::RUBY
|
19
|
+
gemspec.has_rdoc = true
|
20
|
+
gemspec.extra_rdoc_files = ["README.rdoc", "LICENSE"]
|
21
|
+
gemspec.summary = SUMMARY
|
22
|
+
gemspec.description = gemspec.summary
|
23
|
+
gemspec.author = AUTHOR
|
24
|
+
gemspec.email = EMAIL
|
25
|
+
gemspec.homepage = HOMEPAGE
|
26
|
+
gemspec.add_dependency("httparty", ">= 0.4.5")
|
27
|
+
gemspec.add_dependency("json", ">= 1.1.9")
|
28
|
+
gemspec.require_path = "lib"
|
29
|
+
gemspec.files = %w(LICENSE README.rdoc Rakefile) + Dir.glob("{lib}/**/*")
|
48
30
|
end
|
31
|
+
rescue LoadError
|
32
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
49
33
|
end
|
50
34
|
|
51
35
|
Rake::RDocTask.new do |rd|
|
data/lib/mlb.rb
CHANGED
@@ -1,123 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
class MLB
|
6
|
-
include HTTParty
|
7
|
-
base_uri 'http://www.freebase.com/api'
|
8
|
-
|
9
|
-
# Returns an array of objects, one for each Major League Baseball team
|
10
|
-
#
|
11
|
-
# MLB.teams.first.name # => "Arizona Diamondbacks"
|
12
|
-
# MLB.teams.first.league # => "National League"
|
13
|
-
# MLB.teams.first.division # => "National League West"
|
14
|
-
# MLB.teams.first.manager # => "Bob Melvin"
|
15
|
-
# MLB.teams.first.wins # => 82
|
16
|
-
# MLB.teams.first.losses # => 80
|
17
|
-
# MLB.teams.first.founded # => 1998
|
18
|
-
# MLB.teams.first.mascot # => nil
|
19
|
-
# MLB.teams.first.ballpark # => "Chase Field"
|
20
|
-
# MLB.teams.first.logo_url # => "http://img.freebase.com/api/trans/image_thumb/wikipedia/images/en_id/13104064"
|
21
|
-
# MLB.teams.first.players.first.name # => "Alex Romero"
|
22
|
-
# MLB.teams.first.players.first.number # => 28
|
23
|
-
# MLB.teams.first.players.first.position # => "Right fielder"
|
24
|
-
def self.teams
|
25
|
-
@query ||= team_query
|
26
|
-
@response ||= exec_query(@query)
|
27
|
-
@results ||= []
|
28
|
-
if @response && @results.empty?
|
29
|
-
@response['result'].each do |result|
|
30
|
-
@results << OpenStruct.new({
|
31
|
-
:name => result['name'],
|
32
|
-
:league => result['league']['name'],
|
33
|
-
:division => result['division']['name'],
|
34
|
-
:manager => result['current_manager']['name'],
|
35
|
-
:wins => result['team_stats'].first['wins'].to_i,
|
36
|
-
:losses => result['team_stats'].first['losses'].to_i,
|
37
|
-
:founded => result['/sports/sports_team/founded'].first['value'].to_i,
|
38
|
-
:mascot => (result['/sports/sports_team/team_mascot'].first ? result['/sports/sports_team/team_mascot'].first['name'] : nil),
|
39
|
-
:ballpark => result['/sports/sports_team/arena_stadium'].first['name'],
|
40
|
-
:logo_url => 'http://img.freebase.com/api/trans/image_thumb' + result['/common/topic/image'].first['id'],
|
41
|
-
:players => result['current_roster'].map {|player|
|
42
|
-
OpenStruct.new({
|
43
|
-
:name => player['player'],
|
44
|
-
:number => player['number'],
|
45
|
-
:position => player['position'],
|
46
|
-
})
|
47
|
-
},
|
48
|
-
})
|
49
|
-
end
|
50
|
-
end
|
51
|
-
@results
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
|
56
|
-
# Converts MQL query to JSON and fetches response from Freebase API
|
57
|
-
def self.exec_query(query)
|
58
|
-
format :json
|
59
|
-
begin
|
60
|
-
response = get('/service/mqlread?', :query => {:query => query.to_json})
|
61
|
-
if response['code'] != '/api/status/ok'
|
62
|
-
error("#{response['status']} (Transaction: #{response['transaction_id']})")
|
63
|
-
end
|
64
|
-
rescue SocketError, Errno::ECONNREFUSED => e
|
65
|
-
error("Could not connect. Unclog tubes and try again.")
|
66
|
-
end
|
67
|
-
response
|
68
|
-
end
|
69
|
-
|
70
|
-
# Returns the MQL query for teams, as a Ruby hash
|
71
|
-
def self.team_query
|
72
|
-
{
|
73
|
-
:query => [
|
74
|
-
{
|
75
|
-
'name' => nil,
|
76
|
-
'league' => {
|
77
|
-
'name' => nil,
|
78
|
-
},
|
79
|
-
'division' => {
|
80
|
-
'name' => nil,
|
81
|
-
},
|
82
|
-
'current_manager' => {
|
83
|
-
'name' => nil,
|
84
|
-
},
|
85
|
-
'team_stats' => [{
|
86
|
-
'wins' => nil,
|
87
|
-
'losses' => nil,
|
88
|
-
'season' => nil,
|
89
|
-
'limit' => 1,
|
90
|
-
'sort' => '-season',
|
91
|
-
}],
|
92
|
-
'current_roster' => [{
|
93
|
-
'player' => nil,
|
94
|
-
'position' => nil,
|
95
|
-
'number' => nil,
|
96
|
-
'sort' => 'player',
|
97
|
-
}],
|
98
|
-
'/sports/sports_team/founded' => [{
|
99
|
-
'value' => nil,
|
100
|
-
}],
|
101
|
-
'/sports/sports_team/team_mascot' => [{
|
102
|
-
}],
|
103
|
-
'/sports/sports_team/arena_stadium' => [{
|
104
|
-
'name' => nil,
|
105
|
-
}],
|
106
|
-
'/common/topic/image' => [{
|
107
|
-
'id' => nil,
|
108
|
-
'timestamp' => nil,
|
109
|
-
'sort' => '-timestamp',
|
110
|
-
'limit' => 1,
|
111
|
-
}],
|
112
|
-
'sort' => 'name',
|
113
|
-
'type' => '/baseball/baseball_team',
|
114
|
-
}
|
115
|
-
]
|
116
|
-
}
|
117
|
-
end
|
118
|
-
|
119
|
-
def self.error(*messages)
|
120
|
-
puts messages.map{|msg| "\033[1;31mError: #{msg}\033[0m"}
|
121
|
-
end
|
122
|
-
|
1
|
+
module MLB
|
2
|
+
autoload :Team, 'mlb/team'
|
3
|
+
autoload :Player, 'mlb/player'
|
123
4
|
end
|
data/lib/mlb/player.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module MLB
|
2
|
+
class Player
|
3
|
+
attr_reader :name, :number, :position
|
4
|
+
private_class_method :new
|
5
|
+
|
6
|
+
protected
|
7
|
+
|
8
|
+
# Returns an array of Player objects given a team roster
|
9
|
+
def self.all_from_roster(players)
|
10
|
+
players.map do |player|
|
11
|
+
new(
|
12
|
+
:name => player['player'],
|
13
|
+
:number => player['number'],
|
14
|
+
:position => player['position']
|
15
|
+
)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def initialize(attributes={})
|
22
|
+
super unless attributes.is_a?(Hash)
|
23
|
+
@name = attributes[:name]
|
24
|
+
@number = attributes[:number]
|
25
|
+
@position = attributes[:position]
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/lib/mlb/team.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module MLB
|
5
|
+
class Team
|
6
|
+
include HTTParty
|
7
|
+
format :json
|
8
|
+
base_uri 'http://www.freebase.com/api'
|
9
|
+
attr_reader :name, :league, :division, :manager, :wins, :losses, :founded, :mascot, :ballpark, :logo_url, :players
|
10
|
+
private_class_method :new
|
11
|
+
|
12
|
+
# Returns an array of Team objects
|
13
|
+
#
|
14
|
+
# MLB::Team.all.first.name # => "Arizona Diamondbacks"
|
15
|
+
# MLB::Team.all.first.league # => "National League"
|
16
|
+
# MLB::Team.all.first.division # => "National League West"
|
17
|
+
# MLB::Team.all.first.manager # => "Bob Melvin"
|
18
|
+
# MLB::Team.all.first.wins # => 82
|
19
|
+
# MLB::Team.all.first.losses # => 80
|
20
|
+
# MLB::Team.all.first.founded # => 1998
|
21
|
+
# MLB::Team.all.first.mascot # => nil
|
22
|
+
# MLB::Team.all.first.ballpark # => "Chase Field"
|
23
|
+
# MLB::Team.all.first.logo_url # => "http://img.freebase.com/api/trans/image_thumb/wikipedia/images/en_id/13104064"
|
24
|
+
# MLB::Team.all.first.players.first.name # => "Alex Romero"
|
25
|
+
# MLB::Team.all.first.players.first.number # => 28
|
26
|
+
# MLB::Team.all.first.players.first.position # => "Right fielder"
|
27
|
+
def self.all
|
28
|
+
@query ||= query
|
29
|
+
@response ||= execute(@query)
|
30
|
+
@results ||= []
|
31
|
+
if @response && @results.empty?
|
32
|
+
@response['result'].each do |result|
|
33
|
+
league = result['league']
|
34
|
+
division = result['division']
|
35
|
+
manager = result['current_manager']
|
36
|
+
stats = result['team_stats'].first
|
37
|
+
founded = result['/sports/sports_team/founded'].first
|
38
|
+
mascot = result['/sports/sports_team/team_mascot'].first
|
39
|
+
ballpark = result['/sports/sports_team/arena_stadium'].first
|
40
|
+
logo_prefix = 'http://img.freebase.com/api/trans/image_thumb'
|
41
|
+
logo_suffix = result['/common/topic/image'].first
|
42
|
+
players = result['current_roster']
|
43
|
+
|
44
|
+
@results << new(
|
45
|
+
:name => result['name'],
|
46
|
+
:league => (league ? league['name'] : nil),
|
47
|
+
:division => (division ? division['name'] : nil),
|
48
|
+
:manager => (manager ? manager['name'] : nil),
|
49
|
+
:wins => (stats ? stats['wins'].to_i : nil),
|
50
|
+
:losses => (stats ? stats['losses'].to_i : nil),
|
51
|
+
:founded => (founded ? founded['value'].to_i : nil),
|
52
|
+
:mascot => (mascot ? mascot['name'] : nil),
|
53
|
+
:ballpark => (ballpark ? ballpark['name'] : nil),
|
54
|
+
:logo_url => (logo_suffix ? logo_prefix + logo_suffix['id'] : nil),
|
55
|
+
:players => (players ? Player.all_from_roster(players) : [])
|
56
|
+
)
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
@results
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def initialize(attributes={})
|
66
|
+
super unless attributes.is_a?(Hash)
|
67
|
+
@name = attributes[:name]
|
68
|
+
@league = attributes[:league]
|
69
|
+
@division = attributes[:division]
|
70
|
+
@manager = attributes[:manager]
|
71
|
+
@wins = attributes[:wins]
|
72
|
+
@losses = attributes[:losses]
|
73
|
+
@founded = attributes[:founded]
|
74
|
+
@mascot = attributes[:mascot]
|
75
|
+
@ballpark = attributes[:ballpark]
|
76
|
+
@logo_url = attributes[:logo_url]
|
77
|
+
@players = attributes[:players]
|
78
|
+
end
|
79
|
+
|
80
|
+
# Converts MQL query to JSON and fetches response from Freebase API
|
81
|
+
def self.execute(query)
|
82
|
+
begin
|
83
|
+
response = get('/service/mqlread?', :query => {:query => query.to_json})
|
84
|
+
if response['code'] != '/api/status/ok'
|
85
|
+
raise Exception, "#{response['status']} (Transaction: #{response['transaction_id']})"
|
86
|
+
end
|
87
|
+
rescue SocketError, Errno::ECONNREFUSED
|
88
|
+
raise Exception, "Could not connect. Unclog tubes and try again."
|
89
|
+
end
|
90
|
+
response
|
91
|
+
end
|
92
|
+
|
93
|
+
# Returns the MQL query for teams, as a Ruby hash
|
94
|
+
def self.query
|
95
|
+
{
|
96
|
+
:query => [
|
97
|
+
{
|
98
|
+
'name' => nil,
|
99
|
+
'league' => {
|
100
|
+
'name' => nil,
|
101
|
+
},
|
102
|
+
'division' => {
|
103
|
+
'name' => nil,
|
104
|
+
},
|
105
|
+
'current_manager' => {
|
106
|
+
'name' => nil,
|
107
|
+
},
|
108
|
+
'team_stats' => [{
|
109
|
+
'wins' => nil,
|
110
|
+
'losses' => nil,
|
111
|
+
'season' => nil,
|
112
|
+
'limit' => 1,
|
113
|
+
'sort' => '-season',
|
114
|
+
}],
|
115
|
+
'current_roster' => [{
|
116
|
+
'player' => nil,
|
117
|
+
'position' => nil,
|
118
|
+
'number' => nil,
|
119
|
+
'sort' => 'player',
|
120
|
+
}],
|
121
|
+
'/sports/sports_team/founded' => [{
|
122
|
+
'value' => nil,
|
123
|
+
}],
|
124
|
+
'/sports/sports_team/team_mascot' => [{
|
125
|
+
}],
|
126
|
+
'/sports/sports_team/arena_stadium' => [{
|
127
|
+
'name' => nil,
|
128
|
+
}],
|
129
|
+
'/common/topic/image' => [{
|
130
|
+
'id' => nil,
|
131
|
+
'timestamp' => nil,
|
132
|
+
'sort' => '-timestamp',
|
133
|
+
'limit' => 1,
|
134
|
+
}],
|
135
|
+
'sort' => 'name',
|
136
|
+
'type' => '/baseball/baseball_team',
|
137
|
+
}
|
138
|
+
]
|
139
|
+
}
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mlb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Michaels-Ober
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-11-13 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -39,20 +39,22 @@ executables: []
|
|
39
39
|
extensions: []
|
40
40
|
|
41
41
|
extra_rdoc_files:
|
42
|
-
- README.rdoc
|
43
42
|
- LICENSE
|
43
|
+
- README.rdoc
|
44
44
|
files:
|
45
45
|
- LICENSE
|
46
46
|
- README.rdoc
|
47
47
|
- Rakefile
|
48
48
|
- lib/mlb.rb
|
49
|
+
- lib/mlb/player.rb
|
50
|
+
- lib/mlb/team.rb
|
49
51
|
has_rdoc: true
|
50
52
|
homepage: http://github.com/sferik/mlb
|
51
53
|
licenses: []
|
52
54
|
|
53
55
|
post_install_message:
|
54
|
-
rdoc_options:
|
55
|
-
|
56
|
+
rdoc_options:
|
57
|
+
- --charset=UTF-8
|
56
58
|
require_paths:
|
57
59
|
- lib
|
58
60
|
required_ruby_version: !ruby/object:Gem::Requirement
|