mlb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.markdown +52 -0
  3. data/lib/mlb.rb +83 -0
  4. metadata +77 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Erik Michaels-Ober
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ # MLB.rb
2
+
3
+ **MLB.rb is a Ruby library for retrieving current Major League Baseball players, managers, teams, divisions, and leagues.
4
+
5
+ ## Get it
6
+
7
+ At the command prompt, type:
8
+
9
+ sudo gem install mlb -s http://gemcutter.org
10
+
11
+ ## Use it
12
+
13
+ $ irb
14
+ >> require 'mlb'
15
+ => true
16
+ >> team = MLB.teams[17]; nil
17
+ => nil
18
+ >> team.name
19
+ => "New York Mets"
20
+ >> team.league
21
+ => "National League"
22
+ >> team.division
23
+ => "National League East"
24
+ >> team.manager
25
+ => "Jerry Manuel"
26
+ >> team.wins
27
+ => 89
28
+ >> team.losses
29
+ => 73
30
+ >> team.founded
31
+ => 1962
32
+ >> team.mascot
33
+ => "Mr. Met"
34
+ >> team.ballpark
35
+ => "Shea Stadium"
36
+ >> team.logo_url
37
+ => "http://img.freebase.com/api/trans/image_thumb/wikipedia/images/commons_id/3269938"
38
+ >> player = team.roster[14]; nil
39
+ => nil
40
+ >> player.name
41
+ => "David Wright"
42
+ >> player.number
43
+ => 5
44
+ >> player.position
45
+ => "Third baseman"
46
+
47
+ Many thanks to:
48
+
49
+ * [Freebase](http://www.freebase.com)
50
+ * [httparty](http://github.com/jnunemaker/httparty)
51
+
52
+ Also, thanks to [beer](http://www.21st-amendment.com).
@@ -0,0 +1,83 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'ostruct'
4
+
5
+ class MLB
6
+ include HTTParty
7
+ base_uri 'http://www.freebase.com/api'
8
+
9
+ def self.teams
10
+ format :json
11
+ @query ||= {
12
+ :query => [
13
+ {
14
+ 'name' => nil,
15
+ 'league' => {
16
+ 'name' => nil,
17
+ },
18
+ 'division' => {
19
+ 'name' => nil,
20
+ },
21
+ 'current_manager' => {
22
+ 'name' => nil,
23
+ },
24
+ 'team_stats' => [{
25
+ 'wins' => nil,
26
+ 'losses' => nil,
27
+ 'season' => nil,
28
+ 'limit' => 1,
29
+ 'sort' => '-season',
30
+ }],
31
+ 'current_roster' => [{
32
+ 'player' => nil,
33
+ 'position' => nil,
34
+ 'number' => nil,
35
+ 'sort' => 'player',
36
+ }],
37
+ '/sports/sports_team/founded' => [{
38
+ 'value' => nil,
39
+ }],
40
+ '/sports/sports_team/team_mascot' => [{
41
+ }],
42
+ '/sports/sports_team/arena_stadium' => [{
43
+ 'name' => nil,
44
+ }],
45
+ '/common/topic/image' => [{
46
+ 'id' => nil,
47
+ 'timestamp' => nil,
48
+ 'sort' => '-timestamp',
49
+ 'limit' => 1,
50
+ }],
51
+ 'sort' => 'name',
52
+ 'type' => '/baseball/baseball_team',
53
+ }
54
+ ]
55
+ }
56
+
57
+ @response ||= get('/service/mqlread?', :query => {:query => JSON.unparse(@query)})
58
+ unless @response['code'] == '/api/status/ok'
59
+ errors = Array(@response['messages']).map{|m| m.inspect}.join(', ')
60
+ raise "#{@response['status']}: #{errors} (#{@response['transaction_id']})"
61
+ end
62
+ @results ||= []
63
+ if @results.empty?
64
+ @response['result'].each do |result|
65
+ @results << OpenStruct.new({
66
+ :name => result['name'],
67
+ :league => result['league']['name'],
68
+ :division => result['division']['name'],
69
+ :manager => result['current_manager']['name'],
70
+ :roster => result['current_roster'].map{|player| OpenStruct.new({:name => player['player'], :number => player['number'], :position => player['position'],})},
71
+ :wins => result['team_stats'].first['wins'].to_i,
72
+ :losses => result['team_stats'].first['losses'].to_i,
73
+ :founded => result['/sports/sports_team/founded'].first['value'].to_i,
74
+ :mascot => (result['/sports/sports_team/team_mascot'].first ? result['/sports/sports_team/team_mascot'].first['name'] : nil),
75
+ :ballpark => result['/sports/sports_team/arena_stadium'].first['name'],
76
+ :logo_url => 'http://img.freebase.com/api/trans/image_thumb' + result['/common/topic/image'].first['id'],
77
+ })
78
+ end
79
+ end
80
+ @results
81
+ end
82
+
83
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mlb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Erik Michaels-Ober
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-06 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.4.5
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.9
34
+ version:
35
+ description: MLB.rb is a Ruby library for retrieving current Major League Baseball players, managers, teams, divisions, and leagues.
36
+ email: sferik@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.markdown
43
+ - LICENSE
44
+ files:
45
+ - LICENSE
46
+ - README.markdown
47
+ - lib/mlb.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/sferik/mlb
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: MLB.rb is a Ruby library for retrieving current Major League Baseball players, managers, teams, divisions, and leagues.
76
+ test_files: []
77
+