sports_direct 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sports_direct.rb +6 -0
- data/lib/sports_direct/api.rb +46 -0
- data/lib/sports_direct/basketball.rb +5 -0
- data/lib/sports_direct/basketball/ncaa.rb +104 -0
- data/lib/sports_direct/normalization.rb +110 -0
- metadata +157 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
module SportsDirect
|
5
|
+
class API
|
6
|
+
Error = Class.new(StandardError)
|
7
|
+
Timeout = Class.new(Error)
|
8
|
+
Unauthorized = Class.new(Error)
|
9
|
+
|
10
|
+
include HTTParty
|
11
|
+
|
12
|
+
base_uri 'http://xml.sportsdirectinc.com/sport/v2'
|
13
|
+
parser lambda { |body, format| Nokogiri.XML(body) }
|
14
|
+
|
15
|
+
if ENV['SPORTS_DIRECT_PROXY']
|
16
|
+
http_proxy *ENV['SPORTS_DIRECT_PROXY'].split(':')
|
17
|
+
end
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def ncaa_basketball_schedule
|
21
|
+
get('/basketball/NCAAB/schedule/schedule_NCAAB.xml')
|
22
|
+
end
|
23
|
+
|
24
|
+
def ncaa_basketball_teams(season)
|
25
|
+
get("/basketball/NCAAB/teams/#{season}/teams_NCAAB.xml")
|
26
|
+
end
|
27
|
+
|
28
|
+
def get(*args)
|
29
|
+
response = super
|
30
|
+
|
31
|
+
case response.code
|
32
|
+
when 401
|
33
|
+
raise Unauthorized.new(response.at('h2').text.split(': ').last)
|
34
|
+
when 403
|
35
|
+
raise Unauthorized.new(response.at('head/title').text)
|
36
|
+
else
|
37
|
+
response
|
38
|
+
end
|
39
|
+
rescue Errno::ECONNRESET
|
40
|
+
raise Error.new($!.message)
|
41
|
+
rescue Errno::ETIMEDOUT
|
42
|
+
raise Timeout.new($!.message)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'active_support/core_ext/object/try'
|
2
|
+
require 'active_support/core_ext/string/inflections'
|
3
|
+
|
4
|
+
module SportsDirect
|
5
|
+
module Basketball
|
6
|
+
class NCAA
|
7
|
+
include Normalization
|
8
|
+
|
9
|
+
def schedule
|
10
|
+
(API.ncaa_basketball_schedule / 'competition').collect do |event|
|
11
|
+
normalize(event)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def teams
|
16
|
+
API.ncaa_basketball_teams('2010-2011') / 'team'
|
17
|
+
end
|
18
|
+
private :teams
|
19
|
+
|
20
|
+
def team_name(id)
|
21
|
+
id = id.split(':').last.to_i if id.is_a?(String)
|
22
|
+
team_names[id]
|
23
|
+
end
|
24
|
+
private :team_name
|
25
|
+
|
26
|
+
def team_names
|
27
|
+
@team_names ||= Hash[teams.collect do |team|
|
28
|
+
name = normalize_performer_name(team.at('name').text)
|
29
|
+
nick = team.at('name[@type="nick"]/text()').to_s
|
30
|
+
|
31
|
+
[team.at('id').text.split(':').last.to_i, "#{name} #{nick}"]
|
32
|
+
end]
|
33
|
+
end
|
34
|
+
private :team_names
|
35
|
+
|
36
|
+
def normalize(event)
|
37
|
+
details = event.at('details')
|
38
|
+
venue = details.at('venue')
|
39
|
+
locality = venue.at('location/city').text
|
40
|
+
region = venue.at('location/state').try(:text)
|
41
|
+
home_name = team_name(event.at('home-team-content/team/id').text)
|
42
|
+
away_name = team_name(event.at('away-team-content/team/id').text)
|
43
|
+
sport = "Mens #{event.at('id').text.split('/')[2].titleize}"
|
44
|
+
|
45
|
+
name = if details.at('competition-type').text == 'Regular Season'
|
46
|
+
"#{home_name} #{sport} vs. #{away_name} #{sport}"
|
47
|
+
else
|
48
|
+
details.at('competition-type').text
|
49
|
+
end
|
50
|
+
|
51
|
+
{
|
52
|
+
:id => event.at('id').text.split(':').last,
|
53
|
+
:sport => sport,
|
54
|
+
:event_name => name,
|
55
|
+
:occurs_at => normalize_date(
|
56
|
+
event.at('start-date').text,
|
57
|
+
event.at('timezone').text,
|
58
|
+
locality,
|
59
|
+
region
|
60
|
+
),
|
61
|
+
:occurs_at_utc => normalize_date(
|
62
|
+
event.at('start-date').text,
|
63
|
+
event.at('timezone').text,
|
64
|
+
locality,
|
65
|
+
region,
|
66
|
+
true
|
67
|
+
),
|
68
|
+
:tbd => details.at('date-tbd').text == 'true',
|
69
|
+
:venue_name => venue.at('name').text,
|
70
|
+
:locality => locality,
|
71
|
+
:region => normalize_region(region.to_s),
|
72
|
+
:country => venue.at('location/country').text,
|
73
|
+
:home_name => home_name,
|
74
|
+
:away_name => away_name
|
75
|
+
}
|
76
|
+
end
|
77
|
+
private :normalize
|
78
|
+
|
79
|
+
def normalize_performer_name(name)
|
80
|
+
name.gsub!(/&/, '&')
|
81
|
+
name.gsub!(/-/, ' ')
|
82
|
+
name.gsub!(/\((.*)\)/, '\1')
|
83
|
+
name.gsub!(/\s+/, ' ')
|
84
|
+
name.sub!(/Col\./, 'College')
|
85
|
+
name.sub!(/Conn\./, 'Connecticut')
|
86
|
+
name.sub!(/Miami\s+Florida/, 'Miami Hurricanes')
|
87
|
+
name.sub!(/N\.?C\.? (Asheville|Greensboro|Wilmington)/, 'UNC \1')
|
88
|
+
name.sub!(/N\.C\./, 'North Carolina')
|
89
|
+
name.sub!(/N\.Y\./, 'NY')
|
90
|
+
name.sub!(/No\.Carolina/, 'North Carolina')
|
91
|
+
name.sub!(/St\.$/, 'State')
|
92
|
+
name.sub!(/Wis\./, 'Wisconsin')
|
93
|
+
name.sub!(/\bCC$/, 'Corpus Christi')
|
94
|
+
name.sub!(/\bCty\b/, 'County')
|
95
|
+
name.sub!(/\bSE\b/, 'Southeast')
|
96
|
+
name.sub!(/\bU(\.|\b|$)/, 'University')
|
97
|
+
name.gsub!(/[\.']/, '')
|
98
|
+
name.strip!
|
99
|
+
name.split.select { |word| word.length > 1 }.join(' ')
|
100
|
+
end
|
101
|
+
private :normalize_performer_name
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'active_support/core_ext/time/calculations'
|
2
|
+
require 'active_support/core_ext/time/conversions'
|
3
|
+
require 'active_support/core_ext/time/zones'
|
4
|
+
|
5
|
+
Time.zone = 'UTC'
|
6
|
+
|
7
|
+
module SportsDirect
|
8
|
+
module Normalization
|
9
|
+
def normalize_venue_name(name)
|
10
|
+
name.gsub!(/&/, '&')
|
11
|
+
name.gsub!(/-/, ' ')
|
12
|
+
name.gsub!(/\((.*)\)/, '\1')
|
13
|
+
name.gsub!(/[\.']/, '')
|
14
|
+
name.gsub!(/\bThe\b/, '')
|
15
|
+
name.gsub!(/\s+/, ' ')
|
16
|
+
name.sub!(/\bMountian\b/, 'Mountain')
|
17
|
+
name.strip!
|
18
|
+
name.split.select { |word| word.length > 1 }.join(' ')
|
19
|
+
end
|
20
|
+
private :normalize_venue_name
|
21
|
+
|
22
|
+
def normalize_date(date, zone, locality, region, utc=false)
|
23
|
+
time_zone = {
|
24
|
+
'America/St_Thomas' => 'Atlantic Time (Canada)',
|
25
|
+
'EST' => 'Eastern Time (US & Canada)',
|
26
|
+
'Etc/GMT+12' => nil,
|
27
|
+
'US/Alaska' => 'Alaska',
|
28
|
+
'US/Arizona' => 'Arizona',
|
29
|
+
'US/Central' => 'Central Time (US & Canada)',
|
30
|
+
'US/Eastern' => 'Eastern Time (US & Canada)',
|
31
|
+
'US/Hawaii' => 'Hawaii',
|
32
|
+
'US/Mountain' => 'Mountain Time (US & Canada)',
|
33
|
+
'US/Pacific' => 'Pacific Time (US & Canada)'
|
34
|
+
}[zone]
|
35
|
+
|
36
|
+
unless time_zone
|
37
|
+
time_zone = {
|
38
|
+
['Abilene', 'Texas'] => 'Central Time (US & Canada)',
|
39
|
+
['Monticello', 'Arkansas'] => 'Central Time (US & Canada)',
|
40
|
+
['Seaside', 'California'] => 'Pacific Time (US & Canada)',
|
41
|
+
['Vermillion', 'South Dakota'] => 'Central Time (US & Canada)'
|
42
|
+
}[[locality, region]]
|
43
|
+
end
|
44
|
+
|
45
|
+
time = Time.zone.parse(date).in_time_zone(time_zone)
|
46
|
+
|
47
|
+
utc ? time : Time.zone.parse(time.to_s.split.first(2).join(' '))
|
48
|
+
end
|
49
|
+
private :normalize_date
|
50
|
+
|
51
|
+
def normalize_region(region)
|
52
|
+
map = {
|
53
|
+
'Alabama' => 'AL',
|
54
|
+
'Alaska' => 'AK',
|
55
|
+
'Arizona' => 'AZ',
|
56
|
+
'Arkansas' => 'AR',
|
57
|
+
'California' => 'CA',
|
58
|
+
'Colorado' => 'CO',
|
59
|
+
'Connecticut' => 'CT',
|
60
|
+
'Delaware' => 'DE',
|
61
|
+
'District of Columbia' => 'DC',
|
62
|
+
'Florida' => 'FL',
|
63
|
+
'Georgia' => 'GA',
|
64
|
+
'Hawaii' => 'HI',
|
65
|
+
'Idaho' => 'ID',
|
66
|
+
'Illinois' => 'IL',
|
67
|
+
'Indiana' => 'IN',
|
68
|
+
'Iowa' => 'IA',
|
69
|
+
'Kansas' => 'KS',
|
70
|
+
'Kentucky' => 'KY',
|
71
|
+
'Louisiana' => 'LA',
|
72
|
+
'Maine' => 'ME',
|
73
|
+
'Maryland' => 'MD',
|
74
|
+
'Massachusetts' => 'MA',
|
75
|
+
'Michigan' => 'MI',
|
76
|
+
'Minnesota' => 'MN',
|
77
|
+
'Mississippi' => 'MS',
|
78
|
+
'Missouri' => 'MO',
|
79
|
+
'Montana' => 'MT',
|
80
|
+
'Nebraska' => 'NE',
|
81
|
+
'Nevada' => 'NV',
|
82
|
+
'New Hampshire' => 'NH',
|
83
|
+
'New Jersey' => 'NJ',
|
84
|
+
'New Mexico' => 'NM',
|
85
|
+
'New York' => 'NY',
|
86
|
+
'North Carolina' => 'NC',
|
87
|
+
'North Dakota' => 'ND',
|
88
|
+
'Ohio' => 'OH',
|
89
|
+
'Oklahoma' => 'OK',
|
90
|
+
'Oregon' => 'OR',
|
91
|
+
'Pennsylvania' => 'PA',
|
92
|
+
'Rhode Island' => 'RI',
|
93
|
+
'South Carolina' => 'SC',
|
94
|
+
'South Dakota' => 'SD',
|
95
|
+
'Tennessee' => 'TN',
|
96
|
+
'Texas' => 'TX',
|
97
|
+
'Utah' => 'UT',
|
98
|
+
'Vermont' => 'VT',
|
99
|
+
'Virginia' => 'VA',
|
100
|
+
'Washington' => 'WA',
|
101
|
+
'West Virginia' => 'WV',
|
102
|
+
'Wisconsin' => 'WI',
|
103
|
+
'Wyoming' => 'WY'
|
104
|
+
}
|
105
|
+
|
106
|
+
map[region]
|
107
|
+
end
|
108
|
+
private :normalize_region
|
109
|
+
end
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sports_direct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Tyler Hunt
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-15 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activesupport
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - "="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
- 3
|
32
|
+
version: 3.0.3
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: httparty
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - "="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
- 6
|
46
|
+
- 1
|
47
|
+
version: 0.6.1
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: i18n
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - "="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
- 5
|
61
|
+
- 0
|
62
|
+
version: 0.5.0
|
63
|
+
type: :runtime
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: nokogiri
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - "="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 1
|
75
|
+
- 4
|
76
|
+
- 4
|
77
|
+
version: 1.4.4
|
78
|
+
type: :runtime
|
79
|
+
version_requirements: *id004
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: tzinfo
|
82
|
+
prerelease: false
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - "="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
- 3
|
91
|
+
- 23
|
92
|
+
version: 0.3.23
|
93
|
+
type: :runtime
|
94
|
+
version_requirements: *id005
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: rspec
|
97
|
+
prerelease: false
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - "="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
segments:
|
104
|
+
- 2
|
105
|
+
- 3
|
106
|
+
- 0
|
107
|
+
version: 2.3.0
|
108
|
+
type: :development
|
109
|
+
version_requirements: *id006
|
110
|
+
description:
|
111
|
+
email:
|
112
|
+
executables: []
|
113
|
+
|
114
|
+
extensions: []
|
115
|
+
|
116
|
+
extra_rdoc_files: []
|
117
|
+
|
118
|
+
files:
|
119
|
+
- lib/sports_direct/api.rb
|
120
|
+
- lib/sports_direct/basketball/ncaa.rb
|
121
|
+
- lib/sports_direct/basketball.rb
|
122
|
+
- lib/sports_direct/normalization.rb
|
123
|
+
- lib/sports_direct.rb
|
124
|
+
has_rdoc: true
|
125
|
+
homepage: http://github.com/tylerhunt/sports_direct
|
126
|
+
licenses: []
|
127
|
+
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
none: false
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
segments:
|
139
|
+
- 0
|
140
|
+
version: "0"
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
segments:
|
147
|
+
- 0
|
148
|
+
version: "0"
|
149
|
+
requirements: []
|
150
|
+
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 1.3.7
|
153
|
+
signing_key:
|
154
|
+
specification_version: 3
|
155
|
+
summary: An interface library for the Sports Direct web service.
|
156
|
+
test_files: []
|
157
|
+
|