mlb 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mlb (0.3.0)
4
+ mlb (0.4.0)
5
5
  faraday (~> 0.5.2)
6
- sqlite3-ruby (~> 1.3.2)
7
6
  yajl-ruby (~> 0.7.8)
8
7
 
9
8
  GEM
@@ -21,17 +20,14 @@ GEM
21
20
  rack (1.2.1)
22
21
  rake (0.8.7)
23
22
  rcov (0.9.9)
24
- rspec (2.0.1)
25
- rspec-core (~> 2.0.1)
26
- rspec-expectations (~> 2.0.1)
27
- rspec-mocks (~> 2.0.1)
28
- rspec-core (2.0.1)
29
- rspec-expectations (2.0.1)
30
- diff-lcs (>= 1.1.2)
31
- rspec-mocks (2.0.1)
32
- rspec-core (~> 2.0.1)
33
- rspec-expectations (~> 2.0.1)
34
- sqlite3-ruby (1.3.2)
23
+ rspec (2.1.0)
24
+ rspec-core (~> 2.1.0)
25
+ rspec-expectations (~> 2.1.0)
26
+ rspec-mocks (~> 2.1.0)
27
+ rspec-core (2.1.0)
28
+ rspec-expectations (2.1.0)
29
+ diff-lcs (~> 1.1.2)
30
+ rspec-mocks (2.1.0)
35
31
  webmock (1.5.0)
36
32
  addressable (>= 2.2.2)
37
33
  crack (>= 0.1.7)
@@ -48,8 +44,7 @@ DEPENDENCIES
48
44
  mlb!
49
45
  rake (~> 0.8)
50
46
  rcov (~> 0.9)
51
- rspec (~> 2.0)
52
- sqlite3-ruby (~> 1.3.2)
47
+ rspec (~> 2.1)
53
48
  webmock (~> 1.5)
54
49
  yajl-ruby (~> 0.7.8)
55
50
  yard (~> 0.6)
data/Rakefile CHANGED
@@ -28,3 +28,14 @@ namespace :doc do
28
28
  ]
29
29
  end
30
30
  end
31
+
32
+ namespace :cache do
33
+ require File.expand_path('../lib/mlb', __FILE__)
34
+ desc "Update the teams file cache"
35
+ task :update do
36
+ doc = MLB::Team.results_from_freebase(true)
37
+ File.open('cache/teams.json', 'w') do |file|
38
+ file.write(doc.body)
39
+ end
40
+ end
41
+ end
@@ -7158,5 +7158,5 @@
7158
7158
  }
7159
7159
  ],
7160
7160
  "status": "200 OK",
7161
- "transaction_id": "cache;cache03.p01.sjc1:8101;2010-11-06T22:13:32Z;0003"
7161
+ "transaction_id": "cache;cache02.p01.sjc1:8101;2010-11-07T17:25:04Z;0023"
7162
7162
  }
data/lib/mlb.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module MLB
2
- autoload :Connection, 'mlb/connection'
3
- autoload :Request, 'mlb/request'
4
- autoload :Team, 'mlb/team'
5
- autoload :Player, 'mlb/player'
2
+ require File.expand_path('../mlb/request', __FILE__)
3
+ require File.expand_path('../mlb/team', __FILE__)
4
+ require File.expand_path('../mlb/player', __FILE__)
6
5
  end
@@ -1,15 +1,16 @@
1
1
  module MLB
2
2
  class Player
3
- attr_accessor :name, :number, :position
3
+ private_class_method :new
4
+ attr_reader :name, :number, :position
5
+
6
+ private
4
7
 
5
8
  def initialize(attributes={})
6
9
  attributes.each_pair do |key, value|
7
- self.send("#{key}=", value) if self.respond_to?("#{key}=")
10
+ instance_eval("@#{key}=value") if self.respond_to?(key)
8
11
  end
9
12
  end
10
13
 
11
- protected
12
-
13
14
  # Returns an array of Player objects given a team roster
14
15
  def self.all_from_roster(players)
15
16
  players.map do |player|
@@ -4,18 +4,18 @@ module MLB
4
4
  # @private
5
5
  class Request
6
6
  # Perform an HTTP GET request
7
- def self.get(path, options={})
8
- response = connection.get do |request|
7
+ def self.get(path, options={}, raw=false)
8
+ response = connection(raw).get do |request|
9
9
  request.url(path, options)
10
10
  end
11
- response.body
11
+ raw ? response : response.body
12
12
  end
13
13
 
14
14
  private
15
15
 
16
- def self.connection
16
+ def self.connection(raw=false)
17
17
  Faraday::Connection.new(:url => 'http://api.freebase.com') do |connection|
18
- connection.use Faraday::Response::Yajl
18
+ connection.use Faraday::Response::Yajl unless raw
19
19
  connection.adapter Faraday.default_adapter
20
20
  end
21
21
  end
@@ -1,6 +1,7 @@
1
1
  module MLB
2
2
  class Team
3
- attr_accessor :name, :league, :division, :manager, :wins, :losses, :founded, :mascot, :ballpark, :logo_url, :players
3
+ private_class_method :new
4
+ attr_reader :name, :league, :division, :manager, :wins, :losses, :founded, :mascot, :ballpark, :logo_url, :players
4
5
 
5
6
  # Returns an array of Team objects
6
7
  #
@@ -19,29 +20,41 @@ module MLB
19
20
  # MLB::Team.all.first.players.first.number # => 28
20
21
  # MLB::Team.all.first.players.first.position # => "Right fielder"
21
22
  def self.all
22
- @results ||= run
23
+ # Attempt to fetch the result from the Freebase API unless there is a
24
+ # connection error, in which case read from a fixture file
25
+ @all ||= begin
26
+ results_to_team(results_from_freebase)
27
+ rescue SocketError, Errno::ECONNREFUSED, Timeout::Error
28
+ results_to_team(results_from_cache)
29
+ end
30
+ end
31
+
32
+ def self.reset
33
+ @all = nil
23
34
  end
24
35
 
36
+ private
37
+
25
38
  def initialize(attributes={})
26
39
  attributes.each_pair do |key, value|
27
- self.send("#{key}=", value) if self.respond_to?("#{key}=")
40
+ instance_eval("@#{key}=value") if self.respond_to?(key)
28
41
  end
29
42
  end
30
43
 
31
- private
44
+ def self.results_from_freebase(raw=false)
45
+ options = {:query => mql_query}
46
+ Request.get('/api/service/mqlread', options, raw)
47
+ end
32
48
 
33
- # Attempt to fetch the result from the Freebase API unless there is a connection error, in which case query a static SQLite3 database
34
- def self.run
35
- begin
36
- run_team_mql
37
- rescue SocketError, Errno::ECONNREFUSED
38
- run_team_sql
39
- end
49
+ def self.results_from_cache
50
+ Yajl::Parser.parse(file_from_cache("teams.json"))
40
51
  end
41
52
 
42
- def self.run_team_mql
43
- results = Request.get('/api/service/mqlread', :query => team_mql_query)
53
+ def self.file_from_cache(file_name)
54
+ File.new(File.expand_path("../../../cache", __FILE__) + "/" + file_name)
55
+ end
44
56
 
57
+ def self.results_to_team(results)
45
58
  teams = []
46
59
  results['result'].each do |result|
47
60
  league = result['league']
@@ -72,53 +85,8 @@ module MLB
72
85
  teams
73
86
  end
74
87
 
75
- def self.setup_db
76
- require 'sqlite3'
77
- @db ||= SQLite3::Database.new(File.join(File.dirname(__FILE__), "..", "..", "db", "mlb.sqlite3"), :type_translation => true, :results_as_hash => true)
78
- end
79
-
80
- def self.run_team_sql
81
- db = setup_db
82
- query = team_sql_query
83
- results = db.execute(query)
84
-
85
- teams = []
86
- results.each do |result|
87
- teams << new(
88
- :name => result['name'],
89
- :league => result['league'],
90
- :division => result['division'],
91
- :manager => result['manager'],
92
- :wins => result['wins'],
93
- :losses => result['losses'],
94
- :founded => result['founded'],
95
- :mascot => result['mascot'],
96
- :ballpark => result['ballpark'],
97
- :logo_url => result['logo_url'],
98
- :players => run_player_sql(result['name'])
99
- )
100
- end
101
- teams
102
- end
103
-
104
- def self.run_player_sql(team_name)
105
- db = setup_db
106
- query = player_sql_query(team_name)
107
- results = db.execute(query)
108
-
109
- players = []
110
- results.each do |result|
111
- players << Player.new(
112
- :name => result['name'],
113
- :position => result['position'],
114
- :number => result['number']
115
- )
116
- end
117
- players
118
- end
119
-
120
88
  # Returns the MQL query for teams, as a Ruby hash
121
- def self.team_mql_query
89
+ def self.mql_query
122
90
  query = <<-eos
123
91
  [{
124
92
  "name": null,
@@ -164,39 +132,5 @@ module MLB
164
132
  '{"query":' + query.gsub!("\n", '').gsub!(' ', '') + '}'
165
133
  end
166
134
 
167
- def self.team_sql_query
168
- <<-eos
169
- SELECT teams.name AS name
170
- , leagues.name AS league
171
- , divisions.name AS division
172
- , teams.manager AS manager
173
- , teams.wins AS wins
174
- , teams.losses AS losses
175
- , teams.founded AS founded
176
- , teams.mascot AS mascot
177
- , teams.ballpark AS ballpark
178
- , teams.logo_url AS logo_url
179
- FROM teams
180
- , divisions
181
- , leagues
182
- WHERE teams.division_id = divisions.id
183
- AND divisions.league_id = leagues.id
184
- ORDER BY teams.name
185
- eos
186
- end
187
-
188
- def self.player_sql_query(team_name)
189
- <<-eos
190
- SELECT players.name AS name
191
- , players.position AS position
192
- , players.number AS number
193
- FROM players
194
- , teams
195
- WHERE players.team_id = teams.id
196
- AND teams.name = "#{team_name}"
197
- ORDER BY players.name
198
- eos
199
- end
200
-
201
135
  end
202
136
  end
@@ -1,3 +1,3 @@
1
1
  module MLB
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -5,13 +5,12 @@ Gem::Specification.new do |s|
5
5
  s.add_development_dependency("bundler", "~> 1.0")
6
6
  s.add_development_dependency("rake", "~> 0.8")
7
7
  s.add_development_dependency("rcov", "~> 0.9")
8
- s.add_development_dependency("rspec", "~> 2.0")
8
+ s.add_development_dependency("rspec", "~> 2.1")
9
9
  s.add_development_dependency("webmock", "~> 1.5")
10
10
  s.add_development_dependency("yard", "~> 0.6")
11
11
  s.add_development_dependency("ZenTest", "~> 4.4")
12
12
  s.add_runtime_dependency("faraday", "~> 0.5.2")
13
13
  s.add_runtime_dependency("yajl-ruby", "~> 0.7.8")
14
- s.add_runtime_dependency("sqlite3-ruby", "~> 1.3.2")
15
14
  s.authors = ["Erik Michaels-Ober"]
16
15
  s.description = %q{MLB.rb is a Ruby library for retrieving current Major League Baseball players, managers, teams, divisions, and leagues.}
17
16
  s.email = ["sferik@gmail.com"]
@@ -4,14 +4,18 @@ describe MLB::Team, ".all" do
4
4
  context "with connection" do
5
5
  before do
6
6
  stub_request(:get, 'http://api.freebase.com/api/service/mqlread').
7
- with(:query => {:query => MLB::Team.team_mql_query}).
7
+ with(:query => {:query => MLB::Team.mql_query}).
8
8
  to_return(:body => fixture("teams.json"))
9
9
  end
10
10
 
11
+ after do
12
+ MLB::Team.reset
13
+ end
14
+
11
15
  it "should request the correct resource" do
12
16
  MLB::Team.all
13
17
  a_request(:get, 'http://api.freebase.com/api/service/mqlread').
14
- with(:query => {:query => MLB::Team.team_mql_query}).
18
+ with(:query => {:query => MLB::Team.mql_query}).
15
19
  should have_been_made
16
20
  end
17
21
 
@@ -24,10 +28,14 @@ describe MLB::Team, ".all" do
24
28
  context "with timeout" do
25
29
  before do
26
30
  stub_request(:get, 'http://api.freebase.com/api/service/mqlread').
27
- with(:query => {:query => MLB::Team.team_mql_query}).
31
+ with(:query => {:query => MLB::Team.mql_query}).
28
32
  to_timeout
29
33
  end
30
34
 
35
+ after do
36
+ MLB::Team.reset
37
+ end
38
+
31
39
  it "should return the correct results" do
32
40
  teams = MLB::Team.all
33
41
  teams.first.name.should == "Arizona Diamondbacks"
@@ -37,10 +45,14 @@ describe MLB::Team, ".all" do
37
45
  context "without connection" do
38
46
  before do
39
47
  stub_request(:get, 'http://api.freebase.com/api/service/mqlread').
40
- with(:query => {:query => MLB::Team.team_mql_query}).
48
+ with(:query => {:query => MLB::Team.mql_query}).
41
49
  to_raise(SocketError)
42
50
  end
43
51
 
52
+ after do
53
+ MLB::Team.reset
54
+ end
55
+
44
56
  it "should return the correct results" do
45
57
  teams = MLB::Team.all
46
58
  teams.first.name.should == "Arizona Diamondbacks"
@@ -6,7 +6,7 @@ RSpec.configure do |config|
6
6
  end
7
7
 
8
8
  def fixture_path
9
- File.expand_path("../fixtures", __FILE__)
9
+ File.expand_path("../../cache", __FILE__)
10
10
  end
11
11
 
12
12
  def fixture(file)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mlb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Erik Michaels-Ober
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-06 00:00:00 -07:00
18
+ date: 2010-11-07 01:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -71,11 +71,11 @@ dependencies:
71
71
  requirements:
72
72
  - - ~>
73
73
  - !ruby/object:Gem::Version
74
- hash: 3
74
+ hash: 1
75
75
  segments:
76
76
  - 2
77
- - 0
78
- version: "2.0"
77
+ - 1
78
+ version: "2.1"
79
79
  type: :development
80
80
  version_requirements: *id004
81
81
  - !ruby/object:Gem::Dependency
@@ -155,22 +155,6 @@ dependencies:
155
155
  version: 0.7.8
156
156
  type: :runtime
157
157
  version_requirements: *id009
158
- - !ruby/object:Gem::Dependency
159
- name: sqlite3-ruby
160
- prerelease: false
161
- requirement: &id010 !ruby/object:Gem::Requirement
162
- none: false
163
- requirements:
164
- - - ~>
165
- - !ruby/object:Gem::Version
166
- hash: 31
167
- segments:
168
- - 1
169
- - 3
170
- - 2
171
- version: 1.3.2
172
- type: :runtime
173
- version_requirements: *id010
174
158
  description: MLB.rb is a Ruby library for retrieving current Major League Baseball players, managers, teams, divisions, and leagues.
175
159
  email:
176
160
  - sferik@gmail.com
@@ -190,15 +174,13 @@ files:
190
174
  - README.mkd
191
175
  - Rakefile
192
176
  - autotest/discover.rb
193
- - db/cleanup.sql
194
- - db/mlb.sqlite3
177
+ - cache/teams.json
195
178
  - lib/mlb.rb
196
179
  - lib/mlb/player.rb
197
180
  - lib/mlb/request.rb
198
181
  - lib/mlb/team.rb
199
182
  - lib/mlb/version.rb
200
183
  - mlb.gemspec
201
- - spec/fixtures/teams.json
202
184
  - spec/mlb_spec.rb
203
185
  - spec/spec_helper.rb
204
186
  has_rdoc: true
@@ -238,6 +220,5 @@ signing_key:
238
220
  specification_version: 3
239
221
  summary: MLB.rb is a Ruby library for retrieving current Major League Baseball players, managers, teams, divisions, and leagues.
240
222
  test_files:
241
- - spec/fixtures/teams.json
242
223
  - spec/mlb_spec.rb
243
224
  - spec/spec_helper.rb
@@ -1,36 +0,0 @@
1
- DROP TABLE "drafts";
2
-
3
- BEGIN TRANSACTION;
4
- CREATE TABLE "leagues_new" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50) NOT NULL);
5
- INSERT INTO "leagues_new" SELECT "id", "name" FROM "leagues";
6
- DROP TABLE "leagues";
7
- ALTER TABLE "leagues_new" RENAME TO "leagues";
8
- COMMIT;
9
-
10
- BEGIN TRANSACTION;
11
- CREATE TABLE "divisions_new" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "league_id" INTEGER NOT NULL, "name" VARCHAR(50) NOT NULL);
12
- INSERT INTO "divisions_new" SELECT "id", "league_id", "name" FROM "divisions";
13
- DROP TABLE "divisions";
14
- ALTER TABLE "divisions_new" RENAME TO "divisions";
15
- CREATE INDEX "index_divisions_league_id" ON "divisions" ("league_id");
16
- COMMIT;
17
-
18
- BEGIN TRANSACTION;
19
- CREATE TABLE "players_new" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "team_id" INTEGER, "name" VARCHAR(100) NOT NULL, "position" VARCHAR(50), "number" INTEGER NOT NULL);
20
- INSERT INTO "players_new" SELECT "id", "team_id", "name", "position", "number" FROM "players";
21
- DROP TABLE "players";
22
- ALTER TABLE "players_new" RENAME TO "players";
23
- CREATE INDEX "index_players_team_id" ON "players" ("team_id");
24
- COMMIT;
25
-
26
- BEGIN TRANSACTION;
27
- CREATE TABLE "teams_new" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "league_id" INTEGER NOT NULL, "division_id" INTEGER NOT NULL, "name" VARCHAR(50), "logo_url" VARCHAR(255), "manager" VARCHAR(100) NOT NULL, "ballpark" VARCHAR(100), "mascot" VARCHAR(100), "founded" INTEGER NOT NULL, "wins" INTEGER NOT NULL, "losses" INTEGER NOT NULL);
28
- INSERT INTO "teams_new" SELECT "id", "league_id", "division_id", "name", "logo_url", "manager", "ballpark", "mascot", "founded", "wins", "losses" FROM "teams";
29
- DROP TABLE "teams";
30
- ALTER TABLE "teams_new" RENAME TO "teams";
31
- CREATE INDEX "index_teams_division_id" ON "teams" ("division_id");
32
- CREATE INDEX "index_teams_league_id" ON "teams" ("league_id");
33
- CREATE INDEX "index_teams_name" ON "teams" ("name");
34
- COMMIT;
35
-
36
- VACUUM;
Binary file