Skip to content

Instantly share code, notes, and snippets.

@omphe
Created December 22, 2011 14:27
Show Gist options
  • Save omphe/1510472 to your computer and use it in GitHub Desktop.
Save omphe/1510472 to your computer and use it in GitHub Desktop.
CDW - Transformation document classes
require 'json'
require 'nori'
require 'open-uri'
class CDWDocument
attr_accessor :doc
def initialize(uri)
@uri = uri
@xml = ''
begin
open(uri) do |f|
@xml << f.read
end
@doc = Nori.parse(@xml)
@processed = convert(@doc)
rescue OpenURI::HTTPError
@errors = true
end
end
def errors?
return @errors
end
def to_json
@processed.to_json
end
def convert(source_hash)
converted = source_hash
return converted
end
end
class CDWInventoryDocument < CDWDocument
def convert(source_hash)
converted = {}
converted['title'] = source_hash['inventory']['@title']
converted['first_item'] = source_hash['inventory']['section'][0]
return converted
end
end
class CDWStandingsDocument < CDWDocument
def convert(source_hash)
converted = {}
teams = []
source_hash['standings']['round']['group'].each do |group|
group['team'].each do |team|
feed_team = {
'teamId' => team['@idteam'],
'rank' => team['@rank'],
'played' => team['played'],
'won' => team['won'],
'lost' => team['lost'],
'drawn' => team['drawn'],
'goalsFor' => team['scored'],
'goalsAgainst' => team['conceded'],
'points' => team['points']
}
teams << feed_team
end
end
converted['standings'] = {'teams' => teams}
return converted
end
end
class CDWMatchesDocument < CDWDocument
def convert(source_hash)
puts source_hash
converted = {}
matches = []
competition_id = source_hash['allmatches']['@idcompetition']
source_hash['allmatches']['round'].each do |round|
round_id = round['@code']
round['match'].each do |match|
if match.kind_of?(Hash)
feed_match = {
'competitionId' => competition_id,
'roundId' => round_id,
'groupId' => (match['group'] != nil) ? round_id.to_s + '-' + match['group']: round_id,
'matchId' => match['@code'] ,
'matchDate' => match['@date'],
'matchDay' => match['@matchday'],
'matchTime' => match['@matchtime'],
'score' => match['score'],
'session' => match['@session'],
'status' => match['@status'],
}
match['team'].each do |team|
if team['@team'] == 'home'
feed_match['homeTeam'] = { 'teamId' => team['@code'], 'teamName' => team['officialname'] }
elsif team['@team'] == 'away'
feed_match['awayTeam'] = { 'teamId' => team['@code'], 'teamName' => team['officialname'] }
end
end
matches << feed_match
end
end
end
converted['matches'] = matches
return converted
end
end
class CDWAggregateDocument
def initialize()
@errors = false
matches_doc = CDWMatchesDocument.new('http://cd.uefa.com/competitions/euro/Matches/allMatches.html')
if matches_doc.errors?
@errors = true
end
@matches = matches_doc.doc
teams_doc = CDWDocument.new('http://cd.uefa.com/competitions/euro/teams/allTeams.html')
if teams_doc.errors?
@errors = True
end
@teams = teams_doc.doc
@processed = convert
end
def errors?
return @errors
end
def convert
converted = {}
competition = {}
competition['season'] = @matches['allmatches']['@season']
competition['competitionId'] = @matches['allmatches']['@idcompetition']
competition['competitionName'] = @matches['allmatches']['@competition']
competition['rounds'] = []
@matches['allmatches']['round'].each do |round|
feed_round = {}
feed_round['roundName'] = round['@name']
feed_round['roundId'] = round['@code']
groups = {}
round['match'].each do |match|
if match.kind_of?(Hash) and match['group'] and !match['group'].nil? and !match['group'].empty?
if !groups.has_key?(match['group'])
groups[match['group']] = []
end
feed_group = { 'groupName' => match['group'], 'groupId' => match['@code']}
end
end
feed_round['groups'] = groups
competition['rounds'] << feed_round
end
converted['competition'] = competition
return converted
end
def to_json
@processed.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment