lib/sportradar/api/soccer/player.rb in sportradar-api-0.1.13 vs lib/sportradar/api/soccer/player.rb in sportradar-api-0.1.14

- old
+ new

@@ -1,36 +1,40 @@ module Sportradar module Api class Soccer::Player < Data - attr_accessor :id, :first_name, :last_name, :country_code, :country, :reference_id, :full_first_name, :full_last_name, :position, :started, :jersey_number, :tactical_position, :tactical_order, :statistics, :preferred_foot, :birthdate, :height_in, :weight_lb, :height_cm, :weight_kg, :teams, :response, :rank, :total, :statistics, :last_modified, :age + attr_accessor :id, :first_name, :last_name, :country_code, :country, :reference_id, :full_first_name, :full_last_name, :position, :started, :jersey_number, :tactical_position, :tactical_order, :statistics, :preferred_foot, :birthdate, :height_in, :weight_lb, :height_cm, :weight_kg, :teams, :response, :rank, :total, :statistics, :last_modified, :age, :height_ft, :position_name, :tactical_position_name, :name, :full_name def initialize(data) @response = data @id = data["id"] @first_name = data["first_name"] @last_name = data["last_name"] + @name = name @country_code = data["country_code"] @country = data["country"] @reference_id = data["reference_id"] @full_first_name = data["full_first_name"] @full_last_name = data["full_last_name"] + @full_name = full_name @position = data["position"] + @position_name = get_position_name @started = data["started"] @jersey_number = data["jersey_number"] @tactical_position = data["tactical_position"] @tactical_order = data["tactical_order"] @last_modified = data["last_modified"] # profile @preferred_foot = data["preferred_foot"] @birthdate = data["birthdate"] @height_in = data["height_in"] + @height_ft = get_height_ft @weight_lb = data["weight_lb"] @height_cm = data["height_cm"] @weight_kg = data["weight_kg"] - @age = age + @age = get_age set_teams @rank = data["rank"] @total = OpenStruct.new data["total"] if data["total"] @@ -44,23 +48,36 @@ def full_name full = [full_first_name, full_last_name].join(' ') full == " " ? name : full end - def position_name + def get_position_name positions = {"G" => "Goalie", "D" => "Defender", "M" => "Midfielder", "F" => "Forward"} - positions[position] if position + if position.present? + positions[position] + elsif teams.present? + teams.map do |team| + if team.position.present? + positions[team.position] + end + end.uniq.join(", ") + end end def tactical_position_name tactical_positions = { "0" => "Unknown", "1" => "Goalkeeper", "2" => "Right back", "3" => "Central defender", "4" => "Left back", "5" => "Right winger", "6" => "Central midfielder", "7" => "Left winger", "8" => "Forward" } tactical_positions[tactical_position] if tactical_position end - def age + def get_age now = Time.now.utc.to_date - dob = @birthdate.to_date + dob = birthdate.to_date now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1) + end + + def get_height_ft + feet, inches = height_in.to_i.divmod(12) + "#{feet}' #{inches}\"" end private def set_teams