mlb 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mlb.rb CHANGED
@@ -6,9 +6,71 @@ class MLB
6
6
  include HTTParty
7
7
  base_uri 'http://www.freebase.com/api'
8
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"
9
24
  def self.teams
10
25
  format :json
11
- @query ||= {
26
+ @query ||= team_query
27
+ @response ||= exec_mql(@query)
28
+ @results ||= []
29
+ if @results.empty?
30
+ @response['result'].each do |result|
31
+ @results << OpenStruct.new({
32
+ :name => result['name'],
33
+ :league => result['league']['name'],
34
+ :division => result['division']['name'],
35
+ :manager => result['current_manager']['name'],
36
+ :wins => result['team_stats'].first['wins'].to_i,
37
+ :losses => result['team_stats'].first['losses'].to_i,
38
+ :founded => result['/sports/sports_team/founded'].first['value'].to_i,
39
+ :mascot => (result['/sports/sports_team/team_mascot'].first ? result['/sports/sports_team/team_mascot'].first['name'] : nil),
40
+ :ballpark => result['/sports/sports_team/arena_stadium'].first['name'],
41
+ :logo_url => 'http://img.freebase.com/api/trans/image_thumb' + result['/common/topic/image'].first['id'],
42
+ :players => result['current_roster'].map {|player|
43
+ OpenStruct.new({
44
+ :name => player['player'],
45
+ :number => player['number'],
46
+ :position => player['position'],
47
+ })
48
+ },
49
+ })
50
+ end
51
+ end
52
+ @results
53
+ end
54
+
55
+ private
56
+
57
+ # Converts MQL query to JSON and sends to Freebase API
58
+ def self.exec_mql(query)
59
+ begin
60
+ response ||= get('/service/mqlread?', :query => {:query => query.to_json})
61
+ unless response['code'] == '/api/status/ok'
62
+ errors = Array(response['messages']).map{|m| m.inspect}.join(', ')
63
+ raise "#{response['status']}: #{errors} (#{response['transaction_id']})"
64
+ end
65
+ rescue SocketError, Errno::ECONNREFUSED => e
66
+ raise Exception.new("Could not connect. Unclog tubes and try again.")
67
+ end
68
+ response
69
+ end
70
+
71
+ # Returns the MQL query for teams, as a Ruby hash
72
+ def self.team_query
73
+ {
12
74
  :query => [
13
75
  {
14
76
  'name' => nil,
@@ -53,31 +115,6 @@ class MLB
53
115
  }
54
116
  ]
55
117
  }
56
-
57
- @response ||= get('/service/mqlread?', :query => {:query => @query.to_json})
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
- :players => 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
118
  end
82
119
 
83
120
  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.0.3
4
+ version: 0.0.4
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-10-07 00:00:00 -07:00
12
+ date: 2009-10-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -39,11 +39,22 @@ executables: []
39
39
  extensions: []
40
40
 
41
41
  extra_rdoc_files:
42
- - README.markdown
42
+ - README.rdoc
43
43
  - LICENSE
44
44
  files:
45
45
  - LICENSE
46
- - README.markdown
46
+ - README.rdoc
47
+ - Rakefile
48
+ - doc/classes/MLB.html
49
+ - doc/created.rid
50
+ - doc/files/lib/mlb_rb.html
51
+ - doc/files/LICENSE.html
52
+ - doc/files/README_rdoc.html
53
+ - doc/fr_class_index.html
54
+ - doc/fr_file_index.html
55
+ - doc/fr_method_index.html
56
+ - doc/index.html
57
+ - doc/rdoc-style.css
47
58
  - lib/mlb.rb
48
59
  has_rdoc: true
49
60
  homepage: http://github.com/sferik/mlb
data/README.markdown DELETED
@@ -1,52 +0,0 @@
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.players[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).