mlb 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,7 +1,4 @@
1
- *.db
2
1
  *.gem
3
- *.sqlite
4
- *.sqlite3
5
2
  *~
6
3
  .#*
7
4
  .DS_Store
data/Rakefile CHANGED
@@ -19,6 +19,7 @@ begin
19
19
  gemspec.homepage = HOMEPAGE
20
20
  gemspec.add_dependency("httparty", ">= 0.4.5")
21
21
  gemspec.add_dependency("json", ">= 1.1.9")
22
+ gemspec.add_dependency("sqlite3-ruby", ">= 1.2.5")
22
23
  end
23
24
  Jeweler::GemcutterTasks.new
24
25
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
Binary file
@@ -1,7 +1,12 @@
1
1
  module MLB
2
2
  class Player
3
- attr_reader :name, :number, :position
4
- private_class_method :new
3
+ attr_accessor :name, :number, :position
4
+
5
+ def initialize(attributes={})
6
+ attributes.each_pair do |key, value|
7
+ self.send("#{key}=", value) if self.respond_to?("#{key}=")
8
+ end
9
+ end
5
10
 
6
11
  protected
7
12
 
@@ -16,14 +21,5 @@ module MLB
16
21
  end
17
22
  end
18
23
 
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
24
  end
29
25
  end
@@ -6,8 +6,7 @@ module MLB
6
6
  include HTTParty
7
7
  format :json
8
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
9
+ attr_accessor :name, :league, :division, :manager, :wins, :losses, :founded, :mascot, :ballpark, :logo_url, :players
11
10
 
12
11
  # Returns an array of Team objects
13
12
  #
@@ -25,73 +24,110 @@ module MLB
25
24
  # MLB::Team.all.first.players.first.number # => 28
26
25
  # MLB::Team.all.first.players.first.position # => "Right fielder"
27
26
  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
27
+ @results ||= run
61
28
  end
62
29
 
63
- private
64
-
65
30
  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]
31
+ attributes.each_pair do |key, value|
32
+ self.send("#{key}=", value) if self.respond_to?("#{key}=")
33
+ end
78
34
  end
79
35
 
80
- # Converts MQL query to JSON and fetches response from Freebase API
81
- def self.execute(query)
36
+ private
37
+
38
+ # Attempt to fetch the result from the Freebase API
39
+ # unless there is a connection error, in which case
40
+ # query a static SQLite3 database.
41
+ def self.run
82
42
  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
43
+ run_team_mql
87
44
  rescue SocketError, Errno::ECONNREFUSED
88
- raise Exception, "Could not connect. Unclog tubes and try again."
45
+ run_team_sql
89
46
  end
90
- response
47
+ end
48
+
49
+ def self.run_team_mql
50
+ query = team_mql_query
51
+ results = get('/service/mqlread?', :query => {:query => query.to_json})
52
+ raise(Exception, "#{results['status']} (Transaction: #{results['transaction_id']})") unless results['code'] == '/api/status/ok'
53
+
54
+ teams = []
55
+ results['result'].each do |result|
56
+ league = result['league']
57
+ division = result['division']
58
+ manager = result['current_manager']
59
+ stats = result['team_stats'].first
60
+ founded = result['/sports/sports_team/founded'].first
61
+ mascot = result['/sports/sports_team/team_mascot'].first
62
+ ballpark = result['/sports/sports_team/arena_stadium'].first
63
+ logo_prefix = 'http://img.freebase.com/api/trans/image_thumb'
64
+ logo_suffix = result['/common/topic/image'].first
65
+ players = result['current_roster']
66
+
67
+ teams << new(
68
+ :name => result['name'],
69
+ :league => (league ? league['name'] : nil),
70
+ :division => (division ? division['name'] : nil),
71
+ :manager => (manager ? manager['name'] : nil),
72
+ :wins => (stats ? stats['wins'].to_i : nil),
73
+ :losses => (stats ? stats['losses'].to_i : nil),
74
+ :founded => (founded ? founded['value'].to_i : nil),
75
+ :mascot => (mascot ? mascot['name'] : nil),
76
+ :ballpark => (ballpark ? ballpark['name'] : nil),
77
+ :logo_url => (logo_suffix ? logo_prefix + logo_suffix['id'] : nil),
78
+ :players => (players ? Player.all_from_roster(players) : [])
79
+ )
80
+ end
81
+ teams
82
+ end
83
+
84
+ def self.setup_db
85
+ require 'sqlite3'
86
+ @db ||= SQLite3::Database.new(File.join(File.dirname(__FILE__), "..", "..", "db", "mlb.sqlite3"), :type_translation => true, :results_as_hash => true)
87
+ end
88
+
89
+ def self.run_team_sql
90
+ db = setup_db
91
+ query = team_sql_query
92
+ results = db.execute(query)
93
+
94
+ teams = []
95
+ results.each do |result|
96
+ teams << new(
97
+ :name => result['name'],
98
+ :league => result['league'],
99
+ :division => result['division'],
100
+ :manager => result['manager'],
101
+ :wins => result['wins'],
102
+ :losses => result['losses'],
103
+ :founded => result['founded'],
104
+ :mascot => result['mascot'],
105
+ :ballpark => result['ballpark'],
106
+ :logo_url => result['logo_url'],
107
+ :players => run_player_sql(result['name'])
108
+ )
109
+ end
110
+ teams
111
+ end
112
+
113
+ def self.run_player_sql(team_name)
114
+ db = setup_db
115
+ query = player_sql_query(team_name)
116
+ results = db.execute(query)
117
+
118
+ players = []
119
+ results.each do |result|
120
+ players << Player.new(
121
+ :name => result['name'],
122
+ :position => result['position'],
123
+ :number => result['number']
124
+ )
125
+ end
126
+ players
91
127
  end
92
128
 
93
129
  # Returns the MQL query for teams, as a Ruby hash
94
- def self.query
130
+ def self.team_mql_query
95
131
  {
96
132
  :query => [
97
133
  {
@@ -139,5 +175,37 @@ module MLB
139
175
  }
140
176
  end
141
177
 
178
+ def self.team_sql_query
179
+ <<-eos
180
+ SELECT teams.name AS name
181
+ , leagues.name AS league
182
+ , divisions.name AS division
183
+ , teams.manager AS manager
184
+ , teams.wins AS wins
185
+ , teams.losses AS losses
186
+ , teams.founded AS founded
187
+ , teams.mascot AS mascot
188
+ , teams.ballpark AS ballpark
189
+ , teams.logo_url AS logo_url
190
+ FROM teams
191
+ , divisions
192
+ , leagues
193
+ WHERE teams.division_id = divisions.id
194
+ AND divisions.league_id = leagues.id
195
+ eos
196
+ end
197
+
198
+ def self.player_sql_query(team_name)
199
+ <<-eos
200
+ SELECT players.name AS name
201
+ , players.position AS position
202
+ , players.number AS number
203
+ FROM players
204
+ , teams
205
+ WHERE players.team_id = teams.id
206
+ AND teams.name = "#{team_name}"
207
+ eos
208
+ end
209
+
142
210
  end
143
211
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mlb}
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Erik Michaels-Ober"]
12
- s.date = %q{2009-11-20}
12
+ s.date = %q{2009-11-27}
13
13
  s.description = %q{MLB.rb is a Ruby library for retrieving current Major League Baseball players, managers, teams, divisions, and leagues.}
14
14
  s.email = %q{sferik@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  "README.rdoc",
23
23
  "Rakefile",
24
24
  "VERSION",
25
+ "db/mlb.sqlite3",
25
26
  "lib/mlb.rb",
26
27
  "lib/mlb/player.rb",
27
28
  "lib/mlb/team.rb",
@@ -40,13 +41,16 @@ Gem::Specification.new do |s|
40
41
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
41
42
  s.add_runtime_dependency(%q<httparty>, [">= 0.4.5"])
42
43
  s.add_runtime_dependency(%q<json>, [">= 1.1.9"])
44
+ s.add_runtime_dependency(%q<sqlite3-ruby>, [">= 1.2.5"])
43
45
  else
44
46
  s.add_dependency(%q<httparty>, [">= 0.4.5"])
45
47
  s.add_dependency(%q<json>, [">= 1.1.9"])
48
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.2.5"])
46
49
  end
47
50
  else
48
51
  s.add_dependency(%q<httparty>, [">= 0.4.5"])
49
52
  s.add_dependency(%q<json>, [">= 1.1.9"])
53
+ s.add_dependency(%q<sqlite3-ruby>, [">= 1.2.5"])
50
54
  end
51
55
  end
52
56
 
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.1.2
4
+ version: 0.2.0
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-11-20 00:00:00 -08:00
12
+ date: 2009-11-27 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,16 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.1.9
34
34
  version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: sqlite3-ruby
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.2.5
44
+ version:
35
45
  description: MLB.rb is a Ruby library for retrieving current Major League Baseball players, managers, teams, divisions, and leagues.
36
46
  email: sferik@gmail.com
37
47
  executables: []
@@ -47,6 +57,7 @@ files:
47
57
  - README.rdoc
48
58
  - Rakefile
49
59
  - VERSION
60
+ - db/mlb.sqlite3
50
61
  - lib/mlb.rb
51
62
  - lib/mlb/player.rb
52
63
  - lib/mlb/team.rb