Skip to content

Commit

Permalink
can now fetch games, hurrah!
Browse files Browse the repository at this point in the history
  $ ruby -rrubygems -rmlb_gameday_api -rpp -e "pp MLBAPI::Base.games"
  • Loading branch information
wuputah committed Sep 23, 2009
1 parent 429f3df commit 268df01
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/mlb_gameday_api.rb
Expand Up @@ -8,6 +8,6 @@ module MLBAPI; end
require 'mlb_gameday_api/base'
require 'mlb_gameday_api/client'
require 'mlb_gameday_api/cache'
require 'mlb_gameday_api/model'
require 'mlb_gameday_api/game'
require 'mlb_gameday_api/player'
require 'mlb_gameday_api/day'
13 changes: 11 additions & 2 deletions lib/mlb_gameday_api/base.rb
Expand Up @@ -2,8 +2,17 @@ class MLBAPI::Error < StandardError; end

class MLBAPI::Base

def initialize
@client = MLBAPI::Client.new
def self.client
Thread.current[:MLBAPI_Client] ||= MLBAPI::Client.new
end

def self.games(date = Date.today)
games = []
game_xml = client.load('miniscoreboard.xml', date)
game_xml.xpath('/games/game').each do |game|
games << MLBAPI::Game.new(game.attributes)
end
games
end

end
3 changes: 3 additions & 0 deletions lib/mlb_gameday_api/game.rb
@@ -0,0 +1,3 @@
class MLBAPI::Game < MLBAPI::Model

end
40 changes: 40 additions & 0 deletions lib/mlb_gameday_api/model.rb
@@ -0,0 +1,40 @@
class MLBAPI::Model < MLBAPI::Base

def initialize(attributes = {})
@attributes = attributes
end

private

# roughly, this sets up an openstruct-like model
def method_missing(sym, *args, &block)
if args.empty? && block.nil? && sym !~ /[\!\?\=]$/ && @attributes.has_key?(sym.to_s)
create_attr_reader(sym)
send(sym)
elsif args.size == 1 && block.nil? && sym =~ /\=$/
create_attr_writer(sym)
send(sym, *args)
else
super
end
end

def create_attr_reader(sym)
self.class.class_eval do
define_method(sym) do |*args|
raise ArgumentError, "wrong number of arguments (#{args.size} for 0)" unless args.size == 0
@attributes[sym.to_s]
end
end
end

def create_attr_writer(sym)
self.class.class_eval do
define_method(sym) do |*args|
raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" unless args.size == 1
@attributes[sym.to_s] = args.first
end
end
end

end

0 comments on commit 268df01

Please sign in to comment.