lib/foot_stats/narration.rb in foot_stats-0.0.1 vs lib/foot_stats/narration.rb in foot_stats-0.1.0
- old
+ new
@@ -1,49 +1,51 @@
module FootStats
class Narration < Resource
attr_accessor :championship_id, :name, :season, :match_id, :score, :has_penalty, :details
def self.all(options={})
- request = Request.new(self, :Partida => options.fetch(:match))
- response = request.parse
+ [ self.find(options) ]
+ end
+ def self.find(options={})
+ match_id = options.fetch(:match)
+ request = Request.new self, :Partida => match_id
+ response = request.parse stream_key: "match_narration-#{match_id}"
+
return response.error if response.error?
- response.resource.collect do |match, value|
- match_hash = response['Partida']
- narrations = response['Narracoes'] || []
+ updated_response response, options
+ end
- narration = Narration.new(
- :championship_id => response['@Id'].to_i,
- :name => response['@Nome'],
- :season => response['@Temporada'],
- :match_id => match_hash['@Id'].to_i,
- :score => match_hash['@Placar'],
- :has_penalty => match_hash['@TemDisputaPenaltis']
- )
+ def self.parse_response(response)
+ match_hash = response['Partida']
+ # Yeah... FootStats bizarre collection made me do it!
+ narrations = [(response['Narracoes'] || {})['Narracao']].flatten.compact
- if narrations.empty?
- narration.details = narrations
- else
- narration.details = []
+ narration = Narration.new(
+ :championship_id => response['@Id'].to_i,
+ :name => response['@Nome'],
+ :season => response['@Temporada'],
+ :match_id => match_hash['@Id'].to_i,
+ :score => match_hash['@Placar'],
+ :has_penalty => match_hash['@TemDisputaPenaltis']
+ )
- narrations.each do |foot_stats_narration|
- narration.details.push(NarrationDetail.new(
- :source_id => foot_stats_narration["@Id"].to_i,
- :team_source_id => foot_stats_narration["IdEquipe"].to_i,
- :team_name => foot_stats_narration["NomeEquipe"],
- :player_source_id => foot_stats_narration["IdJogador"].to_i,
- :player_name => foot_stats_narration["NomeJogador"],
- :period => foot_stats_narration["Periodo"],
- :moment => foot_stats_narration["Momento"],
- :description => foot_stats_narration["Descricao"],
- :action => foot_stats_narration["Acao"]
- ))
- end
- end
-
- narration
+ narration.details = []
+ narrations.each do |foot_stats_narration|
+ narration.details.push(NarrationDetail.new(
+ :source_id => foot_stats_narration["@Id"].to_i,
+ :team_source_id => foot_stats_narration["IdEquipe"].to_i,
+ :team_name => foot_stats_narration["NomeEquipe"],
+ :player_source_id => foot_stats_narration["IdJogador"].to_i,
+ :player_name => foot_stats_narration["NomeJogador"],
+ :period => foot_stats_narration["Periodo"],
+ :moment => foot_stats_narration["Momento"],
+ :description => foot_stats_narration["Descricao"],
+ :action => foot_stats_narration["Acao"]
+ ))
end
+ narration
end
# Return the resource name to request to FootStats.
#
# @return [String]
\ No newline at end of file