xmlstats 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +159 -0
  6. data/Rakefile +1 -0
  7. data/lib/xmlstats.rb +121 -0
  8. data/lib/xmlstats/cachers/memory.rb +21 -0
  9. data/lib/xmlstats/cachers/redis.rb +35 -0
  10. data/lib/xmlstats/endpoint.rb +68 -0
  11. data/lib/xmlstats/endpoints/events.rb +15 -0
  12. data/lib/xmlstats/endpoints/mlb_box_score.rb +13 -0
  13. data/lib/xmlstats/endpoints/mlb_standing.rb +15 -0
  14. data/lib/xmlstats/endpoints/mlb_team_results.rb +14 -0
  15. data/lib/xmlstats/endpoints/mlb_teams.rb +13 -0
  16. data/lib/xmlstats/endpoints/mlb_wild_card_standing.rb +15 -0
  17. data/lib/xmlstats/endpoints/nba_box_score.rb +13 -0
  18. data/lib/xmlstats/endpoints/nba_leaders.rb +34 -0
  19. data/lib/xmlstats/endpoints/nba_standing.rb +15 -0
  20. data/lib/xmlstats/endpoints/nba_team_results.rb +14 -0
  21. data/lib/xmlstats/endpoints/nba_teams.rb +13 -0
  22. data/lib/xmlstats/http_getters/net_http.rb +37 -0
  23. data/lib/xmlstats/object.rb +49 -0
  24. data/lib/xmlstats/objects/basketball_stat.rb +2 -0
  25. data/lib/xmlstats/objects/batter.rb +2 -0
  26. data/lib/xmlstats/objects/event.rb +7 -0
  27. data/lib/xmlstats/objects/event_information.rb +5 -0
  28. data/lib/xmlstats/objects/mlb_box_score.rb +14 -0
  29. data/lib/xmlstats/objects/nba_box_score.rb +12 -0
  30. data/lib/xmlstats/objects/nba_leader.rb +5 -0
  31. data/lib/xmlstats/objects/official.rb +2 -0
  32. data/lib/xmlstats/objects/pitcher.rb +2 -0
  33. data/lib/xmlstats/objects/site.rb +2 -0
  34. data/lib/xmlstats/objects/standing.rb +2 -0
  35. data/lib/xmlstats/objects/team.rb +2 -0
  36. data/lib/xmlstats/objects/team_result.rb +7 -0
  37. data/lib/xmlstats/version.rb +3 -0
  38. data/xmlstats.gemspec +23 -0
  39. metadata +109 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9967022fabf719efee4efde2fcd3e5686f06a205
4
+ data.tar.gz: fb93ca12dd6903b6f3738b2faeb13f604a454b3a
5
+ SHA512:
6
+ metadata.gz: f5538f92973552825b7721560fbc5d0410cbd4db77641100ba6f2ffb278895eaf3d9071c4581e5a67a8551a5da05ee8598d7ab94f76ea808c9149cf61b2a7714
7
+ data.tar.gz: 102ee492791150df296634f048c5532e0a200ab8296f14d988891d63f8dafa8895f17a9c9d5f2573d8085905bb05aec62b5db9dd179424069182cfaad3571fe1
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ /test.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in xmlstats.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alex McHale
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,159 @@
1
+ xmlstats-ruby
2
+ =============
3
+
4
+ A ruby client for xmlstats, an easy to use API for obtaining MLB and NBA statistics in XML or JSON.
5
+
6
+ See xmlstats here: https://erikberg.com/api
7
+
8
+ Installation
9
+ ------------
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'xmlstats'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install xmlstats
22
+
23
+ Configuration
24
+ -------------
25
+
26
+ Configure xmlstats-ruby as follows with your API key and contact info. The
27
+ contact info is used in the User-Agent string.
28
+
29
+ Xmlstats.api_key = "my api key"
30
+ Xmlstats.contact_info = "alex@anticlever.com"
31
+
32
+ This library supports a couple of different caching mechanisms to prevent
33
+ constantly hitting the API server.
34
+
35
+ Xmlstats.cacher = Xmlstats::Cachers::Memory.new
36
+ Xmlstats.cacher = Xmlstats::Cachers::Redis.new(:host => "10.0.81.7")
37
+
38
+ The cacher can be any object which responds to the get(key) and set(key, value)
39
+ methods. The Redis cacher will automatically expire keys after 1 hour. The
40
+ memory cacher never expires keys.
41
+
42
+ Usage
43
+ -----
44
+
45
+ ### General ###
46
+
47
+ * The objects returned by each endpoint method match the specification ( https://erikberg.com/api ).
48
+ * You can inspect what fields are available on a result with the "fields" method.
49
+
50
+ ### Events ###
51
+
52
+ https://erikberg.com/api#events
53
+
54
+ To query the MLB events for a given date:
55
+
56
+ Xmlstats.events(Date.parse("2013-08-01"), :mlb)
57
+
58
+ To query all events for a given date:
59
+
60
+ Xmlstats.events(Date.parse("2013-08-01"))
61
+
62
+ ### MLB Standings ###
63
+
64
+ https://erikberg.com/api#standings
65
+
66
+ To query the MLB standings for a given date:
67
+
68
+ Xmlstats.mlb_standing(Date.parse("2012-09-01"))
69
+
70
+ For the current date, you can drop the parameter:
71
+
72
+ Xmlstats.mlb_standing
73
+
74
+ ### NBA Standings ###
75
+
76
+ https://erikberg.com/api#standings
77
+
78
+ To query the NBA standings for a given date:
79
+
80
+ Xmlstats.nba_standing(Date.parse("2013-02-01"))
81
+
82
+ ### MLB Wild Card Standings ###
83
+
84
+ https://erikberg.com/api#wildcard
85
+
86
+ To query the MLB Wild Card Standings for a given date:
87
+
88
+ Xmlstats.mlb_wild_card_standing(Date.parse("2013-08-09"))
89
+
90
+ To query the MLB Wild Card Standings for the current date:
91
+
92
+ Xmlstats.mlb_wild_card_standing
93
+
94
+ ### MLB Box Score ###
95
+
96
+ https://erikberg.com/api#boxscore
97
+
98
+ When querying the box score, you must specify the event-id. You can find this
99
+ event-id as returned by the events or team results methods.
100
+
101
+ Xmlstats.mlb_box_score("20130818-kansas-city-royals-at-detroit-tigers")
102
+
103
+ ### NBA Box Score ###
104
+
105
+ https://erikberg.com/api#nbaboxscore
106
+
107
+ When querying the box score, you must specify the event-id. You can find this
108
+ event-id as returned by the events or team results methods.
109
+
110
+ Xmlstats.nba_box_score("20131215-portland-trail-blazers-at-detroit-pistons")
111
+
112
+ ### MLB Teams ###
113
+
114
+ https://erikberg.com/api#teams
115
+
116
+ To query the current list of MLB teams:
117
+
118
+ Xmlstats.mlb_teams
119
+
120
+ ### NBA Teams ###
121
+
122
+ https://erikberg.com/api#teams
123
+
124
+ To query the current list of NBA teams:
125
+
126
+ Xmlstats.nba_teams
127
+
128
+ ### MLB Team Results ###
129
+
130
+ https://erikberg.com/api#teamresults
131
+
132
+ To query a team's full schedule for the given season:
133
+
134
+ Xmlstats.mlb_team_results("detroit-tigers", 2012)
135
+
136
+ ### NBA Team Results ###
137
+
138
+ https://erikberg.com/api#teamresults
139
+
140
+ To query a team's full schedule for the given season:
141
+
142
+ Xmlstats.nba_team_results("detroit-pistons", 2012)
143
+
144
+ ### NBA Leaders ###
145
+
146
+ https://erikberg.com/api#nbaleaders
147
+
148
+ To query the current leaders for points per game:
149
+
150
+ Xmlstats.nba_leaders(:points_per_game)
151
+
152
+ Contributing
153
+ ------------
154
+
155
+ 1. Fork it
156
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
157
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
158
+ 4. Push to the branch (`git push origin my-new-feature`)
159
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/xmlstats.rb ADDED
@@ -0,0 +1,121 @@
1
+ require "time"
2
+ require "net/http"
3
+ require "net/https"
4
+ require "json"
5
+
6
+ require "xmlstats/endpoint"
7
+ require "xmlstats/version"
8
+
9
+ require "xmlstats/object"
10
+
11
+ require "xmlstats/objects/standing"
12
+ require "xmlstats/objects/team"
13
+ require "xmlstats/objects/site"
14
+ require "xmlstats/objects/event"
15
+ require "xmlstats/objects/basketball_stat"
16
+ require "xmlstats/objects/batter"
17
+ require "xmlstats/objects/event_information"
18
+ require "xmlstats/objects/official"
19
+ require "xmlstats/objects/pitcher"
20
+ require "xmlstats/objects/mlb_box_score"
21
+ require "xmlstats/objects/nba_box_score"
22
+ require "xmlstats/objects/team_result"
23
+ require "xmlstats/objects/nba_leader"
24
+
25
+ require "xmlstats/cachers/memory"
26
+ require "xmlstats/cachers/redis"
27
+
28
+ require "xmlstats/http_getters/net_http"
29
+
30
+ require "xmlstats/endpoints/events"
31
+ require "xmlstats/endpoints/mlb_standing"
32
+ require "xmlstats/endpoints/nba_standing"
33
+ require "xmlstats/endpoints/mlb_wild_card_standing"
34
+ require "xmlstats/endpoints/mlb_box_score"
35
+ require "xmlstats/endpoints/nba_box_score"
36
+ require "xmlstats/endpoints/mlb_teams"
37
+ require "xmlstats/endpoints/nba_teams"
38
+ require "xmlstats/endpoints/mlb_team_results"
39
+ require "xmlstats/endpoints/nba_team_results"
40
+ require "xmlstats/endpoints/nba_leaders"
41
+
42
+ module Xmlstats
43
+
44
+ def self.api_key
45
+ @api_key
46
+ end
47
+
48
+ def self.api_key= api_key
49
+ @api_key = api_key
50
+ end
51
+
52
+ def self.contact_info
53
+ error = "specify user-agent contact info with: Xmlstats.contact_info = 'you@example.com'"
54
+ @contact_info or raise(error)
55
+ end
56
+
57
+ def self.contact_info= contact_info
58
+ @contact_info = contact_info
59
+ end
60
+
61
+ def self.http_getter
62
+ @http_getter || Xmlstats::HttpGetters::NetHttp.new
63
+ end
64
+
65
+ def self.http_getter= http_getter
66
+ @http_getter = http_getter
67
+ end
68
+
69
+ def self.cacher
70
+ @cacher || Xmlstats::Cachers::Memory.new
71
+ end
72
+
73
+ def self.cacher= cacher
74
+ @cacher = cacher
75
+ end
76
+
77
+ def self.events *args
78
+ Xmlstats::Endpoints::Events.fetch *args
79
+ end
80
+
81
+ def self.mlb_box_score *args
82
+ Xmlstats::Endpoints::MlbBoxScore.fetch *args
83
+ end
84
+
85
+ def self.nba_box_score *args
86
+ Xmlstats::Endpoints::NbaBoxScore.fetch *args
87
+ end
88
+
89
+ def self.mlb_standing *args
90
+ Xmlstats::Endpoints::MlbStanding.fetch *args
91
+ end
92
+
93
+ def self.nba_standing *args
94
+ Xmlstats::Endpoints::NbaStanding.fetch *args
95
+ end
96
+
97
+ def self.mlb_wild_card_standing *args
98
+ Xmlstats::Endpoints::MlbWildCardStanding.fetch *args
99
+ end
100
+
101
+ def self.mlb_team_results *args
102
+ Xmlstats::Endpoints::MlbTeamResults.fetch *args
103
+ end
104
+
105
+ def self.nba_team_results *args
106
+ Xmlstats::Endpoints::NbaTeamResults.fetch *args
107
+ end
108
+
109
+ def self.mlb_teams *args
110
+ Xmlstats::Endpoints::MlbTeams.fetch *args
111
+ end
112
+
113
+ def self.nba_teams *args
114
+ Xmlstats::Endpoints::NbaTeams.fetch *args
115
+ end
116
+
117
+ def self.nba_leaders *args
118
+ Xmlstats::Endpoints::NbaLeaders.fetch *args
119
+ end
120
+
121
+ end
@@ -0,0 +1,21 @@
1
+ module Xmlstats
2
+
3
+ module Cachers
4
+
5
+ class Memory
6
+
7
+ def get(key)
8
+ @cache ||= {}
9
+ @cache[key]
10
+ end
11
+
12
+ def set(key, value)
13
+ @cache ||= {}
14
+ @cache[key] = value
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,35 @@
1
+ module Xmlstats
2
+
3
+ module Cachers
4
+
5
+ class Redis
6
+
7
+ def initialize(redis = nil)
8
+ @redis =
9
+ case redis
10
+ when Hash then ::Redis.new(redis)
11
+ when Redis then redis
12
+ when nil then ::Redis.new
13
+ else raise "unknown parameter type to redis cacher: #{redis.inspect}"
14
+ end
15
+ end
16
+
17
+ def get(key)
18
+ @redis.get(namespaced_key(key))
19
+ end
20
+
21
+ def set(key, value)
22
+ @redis.setex(namespaced_key(key), 3600, value)
23
+ end
24
+
25
+ private
26
+
27
+ def namespaced_key(key)
28
+ "xmlstats:cache:#{key}"
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,68 @@
1
+ module Xmlstats
2
+
3
+ module Endpoint
4
+
5
+ module InstanceMethods
6
+
7
+ def initialize(data = {})
8
+ data.each do |key, value|
9
+ instance_variable_set("@#{key}", value)
10
+ end
11
+ end
12
+
13
+ def http_get
14
+ self.class.http_get
15
+ end
16
+
17
+ end
18
+
19
+ module ClassMethods
20
+
21
+ def path= path
22
+ @path = path
23
+ end
24
+
25
+ def http_get(path = nil, params = {})
26
+ path ||= @path
27
+ raise "path not defined" unless path
28
+
29
+ encoded_params =
30
+ params.reject do |k, v|
31
+ v == nil
32
+ end.map do |k, v|
33
+ "#{URI.escape k.to_s}=#{URI.escape v.to_s}"
34
+ end.join("&")
35
+
36
+ uri = URI.parse("https://erikberg.com/#{path}.json?#{encoded_params}")
37
+
38
+ json = Xmlstats.cacher && Xmlstats.cacher.get(uri.to_s)
39
+ return json if json
40
+
41
+ json = Xmlstats.http_getter.get(uri)
42
+ if json
43
+ Xmlstats.cacher.set(uri.to_s, json)
44
+ json
45
+ end
46
+ end
47
+
48
+ def fetch_json(path = nil, params = {})
49
+ path ||= @path
50
+ raise "path not defined" unless path
51
+
52
+ json = http_get(path, params)
53
+ JSON.load(json)
54
+ end
55
+
56
+ end
57
+
58
+ def self.included(base)
59
+ base.send :include, InstanceMethods
60
+ base.send :extend, ClassMethods
61
+ end
62
+
63
+ end
64
+
65
+ module Endpoints
66
+ end
67
+
68
+ end
@@ -0,0 +1,15 @@
1
+ class Xmlstats::Endpoints::Events
2
+
3
+ include Xmlstats::Endpoint
4
+
5
+ def self.fetch(date = Date.today, sport = nil)
6
+ date_str = date.strftime("%Y%m%d") unless date.kind_of? String
7
+ params = { date: date_str, sport: sport }
8
+ response = fetch_json("events", params)
9
+
10
+ response["event"].map do |event|
11
+ Xmlstats::Objects::Event.new event.merge(date: date)
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,13 @@
1
+ class Xmlstats::Endpoints::MlbBoxScore
2
+
3
+ include Xmlstats::Endpoint
4
+
5
+ def self.fetch(event_id = nil)
6
+ raise "please specify event_id as detailed in https://erikberg.com/api#boxscore" unless event_id
7
+
8
+ response = fetch_json("mlb/boxscore/#{event_id}")
9
+
10
+ Xmlstats::Objects::MlbBoxScore.new response.merge(event_id: event_id)
11
+ end
12
+
13
+ end
@@ -0,0 +1,15 @@
1
+ class Xmlstats::Endpoints::MlbStanding
2
+
3
+ include Xmlstats::Endpoint
4
+
5
+ def self.fetch(date = Date.today)
6
+ date_str = date.strftime("%Y%m%d") unless date.kind_of? String
7
+ response = fetch_json("mlb/standings/#{date_str}")
8
+ standing = response["standing"]
9
+
10
+ standing.map do |team_standing|
11
+ Xmlstats::Objects::Standing.new team_standing.merge(date: date)
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,14 @@
1
+ class Xmlstats::Endpoints::MlbTeamResults
2
+
3
+ include Xmlstats::Endpoint
4
+
5
+ def self.fetch(team_id = nil, season = nil)
6
+ raise "please specify team_id as detailed in https://erikberg.com/api#teamresults" unless team_id
7
+ params = { season: season }
8
+ response = fetch_json("mlb/team/#{team_id}/results", params)
9
+ [ response ].flatten.map do |result|
10
+ Xmlstats::Objects::TeamResult.new result
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,13 @@
1
+ class Xmlstats::Endpoints::MlbTeams
2
+
3
+ include Xmlstats::Endpoint
4
+
5
+ def self.fetch
6
+ response = fetch_json("mlb/teams")
7
+
8
+ response.map do |team|
9
+ Xmlstats::Objects::Team.new team
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,15 @@
1
+ class Xmlstats::Endpoints::MlbWildCardStanding
2
+
3
+ include Xmlstats::Endpoint
4
+
5
+ def self.fetch(date = Date.today)
6
+ date_str = date.strftime("%Y%m%d") unless date.kind_of? String
7
+ response = fetch_json("mlb/standings/#{date_str}")
8
+ standing = response["standing"]
9
+
10
+ standing.map do |team_standing|
11
+ Xmlstats::Objects::Standing.new team_standing.merge(date: date)
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,13 @@
1
+ class Xmlstats::Endpoints::NbaBoxScore
2
+
3
+ include Xmlstats::Endpoint
4
+
5
+ def self.fetch(event_id = nil)
6
+ raise "please specify event_id as detailed in https://erikberg.com/api#boxscore" unless event_id
7
+
8
+ response = fetch_json("nba/boxscore/#{event_id}")
9
+
10
+ Xmlstats::Objects::NbaBoxScore.new response.merge(event_id: event_id)
11
+ end
12
+
13
+ end
@@ -0,0 +1,34 @@
1
+ class Xmlstats::Endpoints::NbaLeaders
2
+
3
+ CATEGORIES = %w(
4
+ points_per_game
5
+ assists_per_game
6
+ rebounds_per_game
7
+ off_rebounds_per_game
8
+ def_rebounds_per_game
9
+ field_goal_pct
10
+ free_throw_pct
11
+ three_point_pct
12
+ blocks_per_game
13
+ steals_per_game
14
+ assists_to_turnovers_per_game
15
+ steals_to_turnovers_per_game
16
+ minutes_per_game
17
+ games_played
18
+ )
19
+
20
+ include Xmlstats::Endpoint
21
+
22
+ def self.fetch(category_id = nil, options = {})
23
+ unless CATEGORIES.include? category_id.to_s
24
+ raise "unknown category id #{category_id.inspect}, must be one of: #{CATEGORIES.inspect}"
25
+ end
26
+
27
+ response = fetch_json("nba/leaders/#{category_id}", options)
28
+
29
+ response.map do |leader|
30
+ Xmlstats::Objects::NbaLeader.new leader.merge(category_id: category_id.to_s)
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,15 @@
1
+ class Xmlstats::Endpoints::NbaStanding
2
+
3
+ include Xmlstats::Endpoint
4
+
5
+ def self.fetch(date = Date.today)
6
+ date_str = date.strftime("%Y%m%d") unless date.kind_of? String
7
+ response = fetch_json("nba/standings/#{date_str}")
8
+ standing = response["standing"]
9
+
10
+ standing.map do |team_standing|
11
+ Xmlstats::Objects::Standing.new team_standing.merge(date: date)
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,14 @@
1
+ class Xmlstats::Endpoints::NbaTeamResults
2
+
3
+ include Xmlstats::Endpoint
4
+
5
+ def self.fetch(team_id = nil, season = nil)
6
+ raise "please specify team_id as detailed in https://erikberg.com/api#teamresults" unless team_id
7
+ params = { season: season }
8
+ response = fetch_json("nba/team/#{team_id}/results", params)
9
+ [ response ].flatten.map do |result|
10
+ Xmlstats::Objects::TeamResult.new result
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,13 @@
1
+ class Xmlstats::Endpoints::NbaTeams
2
+
3
+ include Xmlstats::Endpoint
4
+
5
+ def self.fetch
6
+ response = fetch_json("nba/teams")
7
+
8
+ response.map do |team|
9
+ Xmlstats::Objects::Team.new team
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,37 @@
1
+ module Xmlstats
2
+
3
+ module HttpGetters
4
+
5
+ class NetHttp
6
+
7
+ def get(uri)
8
+ http = Net::HTTP.new(uri.host, uri.port)
9
+ http.use_ssl = true
10
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
11
+ headers = {}
12
+ api_key = Xmlstats.api_key
13
+ headers["User-Agent"] = "xmlstats-ruby/#{Xmlstats::VERSION} (#{Xmlstats.contact_info})"
14
+ headers["Authorization"] = "Bearer #{api_key}" if api_key
15
+ request = Net::HTTP::Get.new(uri.request_uri, headers)
16
+ response = http.request(request)
17
+
18
+ if response.kind_of? Net::HTTPOK
19
+ response.body
20
+ else
21
+ message = "api request returned #{response.code}"
22
+ error = Error.new(message)
23
+ error.request = request
24
+ error.response = response
25
+ raise error
26
+ end
27
+ end
28
+
29
+ class Error < RuntimeError
30
+ attr_accessor :request, :response
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,49 @@
1
+ module Xmlstats
2
+
3
+ class Object
4
+
5
+ def initialize params = {}
6
+ params.each do |key, value|
7
+ instance_variable_set "@#{key}", value
8
+ end
9
+
10
+ self.class.object_references.each do |field_name, field_type|
11
+ ivar = "@#{field_name}"
12
+ val = instance_variable_get(ivar)
13
+
14
+ # Turn hashes into objects, turn arrays into arrays of objects.
15
+ obj =
16
+ case val
17
+ when Array then val.map { |v| field_type.new v }
18
+ else field_type.new val
19
+ end
20
+
21
+ instance_variable_set ivar, obj
22
+ end
23
+ end
24
+
25
+ def fields
26
+ instance_variables.map(&:to_s).map { |s| s.gsub(/^@/, "").to_sym }
27
+ end
28
+
29
+ def method_missing name, *args, &block
30
+ ivar = instance_variable_get("@#{name}")
31
+ return ivar if ivar
32
+ super
33
+ end
34
+
35
+ def self.reference field_name, field_type
36
+ @object_references ||= []
37
+ @object_references << [ field_name, field_type ]
38
+ end
39
+
40
+ def self.object_references
41
+ @object_references ||= []
42
+ end
43
+
44
+ end
45
+
46
+ module Objects
47
+ end
48
+
49
+ end
@@ -0,0 +1,2 @@
1
+ class Xmlstats::Objects::BasketballStat < Xmlstats::Object
2
+ end
@@ -0,0 +1,2 @@
1
+ class Xmlstats::Objects::Batter < Xmlstats::Object
2
+ end
@@ -0,0 +1,7 @@
1
+ class Xmlstats::Objects::Event < Xmlstats::Object
2
+
3
+ reference :away_team, Xmlstats::Objects::Team
4
+ reference :home_team, Xmlstats::Objects::Team
5
+ reference :site, Xmlstats::Objects::Site
6
+
7
+ end
@@ -0,0 +1,5 @@
1
+ class Xmlstats::Objects::EventInformation < Xmlstats::Object
2
+
3
+ reference :site, Xmlstats::Objects::Site
4
+
5
+ end
@@ -0,0 +1,14 @@
1
+ class Xmlstats::Objects::MlbBoxScore < Xmlstats::Object
2
+
3
+ reference :home_team, Xmlstats::Objects::Team
4
+ reference :away_team, Xmlstats::Objects::Team
5
+ reference :home_batters, Xmlstats::Objects::Batter
6
+ reference :away_batters, Xmlstats::Objects::Batter
7
+ reference :home_pitchers, Xmlstats::Objects::Pitcher
8
+ reference :away_pitchers, Xmlstats::Objects::Pitcher
9
+ reference :officials, Xmlstats::Objects::Official
10
+ reference :event_information, Xmlstats::Objects::EventInformation
11
+ reference :home_batter_totals, Xmlstats::Objects::Batter
12
+ reference :away_batter_totals, Xmlstats::Objects::Batter
13
+
14
+ end
@@ -0,0 +1,12 @@
1
+ class Xmlstats::Objects::NbaBoxScore < Xmlstats::Object
2
+
3
+ reference :home_team, Xmlstats::Objects::Team
4
+ reference :away_team, Xmlstats::Objects::Team
5
+ reference :home_stats, Xmlstats::Objects::BasketballStat
6
+ reference :away_stats, Xmlstats::Objects::BasketballStat
7
+ reference :officials, Xmlstats::Objects::Official
8
+ reference :event_information, Xmlstats::Objects::EventInformation
9
+ reference :home_totals, Xmlstats::Objects::BasketballStat
10
+ reference :away_totals, Xmlstats::Objects::BasketballStat
11
+
12
+ end
@@ -0,0 +1,5 @@
1
+ class Xmlstats::Objects::NbaLeader < Xmlstats::Object
2
+
3
+ reference :team, Xmlstats::Objects::Team
4
+
5
+ end
@@ -0,0 +1,2 @@
1
+ class Xmlstats::Objects::Official < Xmlstats::Object
2
+ end
@@ -0,0 +1,2 @@
1
+ class Xmlstats::Objects::Pitcher < Xmlstats::Object
2
+ end
@@ -0,0 +1,2 @@
1
+ class Xmlstats::Objects::Site < Xmlstats::Object
2
+ end
@@ -0,0 +1,2 @@
1
+ class Xmlstats::Objects::Standing < Xmlstats::Object
2
+ end
@@ -0,0 +1,2 @@
1
+ class Xmlstats::Objects::Team < Xmlstats::Object
2
+ end
@@ -0,0 +1,7 @@
1
+ class Xmlstats::Objects::TeamResult < Xmlstats::Object
2
+
3
+ reference :team, Xmlstats::Objects::Team
4
+ reference :opponent, Xmlstats::Objects::Team
5
+ reference :site, Xmlstats::Objects::Site
6
+
7
+ end
@@ -0,0 +1,3 @@
1
+ module Xmlstats
2
+ VERSION = "1.0.0"
3
+ end
data/xmlstats.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'xmlstats/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "xmlstats"
8
+ spec.version = Xmlstats::VERSION
9
+ spec.authors = ["Alex McHale"]
10
+ spec.email = ["alex@anticlever.com"]
11
+ spec.description = %q{A ruby client for xmlstats (https://erikberg.com/api).}
12
+ spec.summary = %q{A ruby client for xmlstats (https://erikberg.com/api).}
13
+ spec.homepage = "http://github.com/alexmchale/xmlstats-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xmlstats
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex McHale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A ruby client for xmlstats (https://erikberg.com/api).
42
+ email:
43
+ - alex@anticlever.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/xmlstats.rb
54
+ - lib/xmlstats/cachers/memory.rb
55
+ - lib/xmlstats/cachers/redis.rb
56
+ - lib/xmlstats/endpoint.rb
57
+ - lib/xmlstats/endpoints/events.rb
58
+ - lib/xmlstats/endpoints/mlb_box_score.rb
59
+ - lib/xmlstats/endpoints/mlb_standing.rb
60
+ - lib/xmlstats/endpoints/mlb_team_results.rb
61
+ - lib/xmlstats/endpoints/mlb_teams.rb
62
+ - lib/xmlstats/endpoints/mlb_wild_card_standing.rb
63
+ - lib/xmlstats/endpoints/nba_box_score.rb
64
+ - lib/xmlstats/endpoints/nba_leaders.rb
65
+ - lib/xmlstats/endpoints/nba_standing.rb
66
+ - lib/xmlstats/endpoints/nba_team_results.rb
67
+ - lib/xmlstats/endpoints/nba_teams.rb
68
+ - lib/xmlstats/http_getters/net_http.rb
69
+ - lib/xmlstats/object.rb
70
+ - lib/xmlstats/objects/basketball_stat.rb
71
+ - lib/xmlstats/objects/batter.rb
72
+ - lib/xmlstats/objects/event.rb
73
+ - lib/xmlstats/objects/event_information.rb
74
+ - lib/xmlstats/objects/mlb_box_score.rb
75
+ - lib/xmlstats/objects/nba_box_score.rb
76
+ - lib/xmlstats/objects/nba_leader.rb
77
+ - lib/xmlstats/objects/official.rb
78
+ - lib/xmlstats/objects/pitcher.rb
79
+ - lib/xmlstats/objects/site.rb
80
+ - lib/xmlstats/objects/standing.rb
81
+ - lib/xmlstats/objects/team.rb
82
+ - lib/xmlstats/objects/team_result.rb
83
+ - lib/xmlstats/version.rb
84
+ - xmlstats.gemspec
85
+ homepage: http://github.com/alexmchale/xmlstats-ruby
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.0.3
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: A ruby client for xmlstats (https://erikberg.com/api).
109
+ test_files: []