class AtpScraper::Activities::Tournament

Activity Tournamnet Class

Public Class Methods

new(doc) click to toggle source
# File lib/atp_scraper/activities/tournament.rb, line 5
def initialize(doc)
  @tournament = doc
end

Public Instance Methods

get() click to toggle source

Return tournament data

# File lib/atp_scraper/activities/tournament.rb, line 10
def get
  tournament_date = pickup_text(".tourney-dates")
  surface = pickup_surface
  caption = pickup_text(".activity-tournament-caption")
  {
    name: pickup_text(".tourney-title"),
    category: pickup_category,
    location: pickup_text(".tourney-location"),
    date: divide_tournament_date(tournament_date),
    year: tournament_date[0, 4],
    surface: surface[:surface],
    surface_inout: surface[:inout],
    ranking: pickup_player_rank(caption)
  }
end
records() click to toggle source

Return records in this tournament

# File lib/atp_scraper/activities/tournament.rb, line 27
def records
  @tournament.css(".mega-table tbody tr")
end

Private Instance Methods

divide_surface(surface) click to toggle source

“OutdoorHard” => { surface: “Hard”, inout: “Outdoor” }

# File lib/atp_scraper/activities/tournament.rb, line 59
def divide_surface(surface)
  inout = surface.match(/^(Outdoor|Indoor)/)
  return { surface: surface, inout: nil } if inout.nil?
  { surface: surface.gsub(/#{inout[0]}/, ''), inout: inout[0] }
end
divide_tournament_date(date) click to toggle source

Before: String “2011.01.03 - 2011.01.08” After: Hash { start: 2011.01.03, end: 2011.01.08 }

# File lib/atp_scraper/activities/tournament.rb, line 35
def divide_tournament_date(date)
  date = date.split('-').map(&:strip)
  { start: date[0], end: date[1] }
end
pickup_category() click to toggle source
# File lib/atp_scraper/activities/tournament.rb, line 44
def pickup_category
  # ex) /~/media/images/tourtypes/categorystamps_itf_118x64.png?xxxxx
  badge_url = @tournament.css(".tourney-badge-wrapper img").attr("src").value
  badge_url.match(/categorystamps_(.*)_[0-9]*x[0-9]*.png/)[1]
end
pickup_player_rank(tournament_caption) click to toggle source
# File lib/atp_scraper/activities/tournament.rb, line 65
def pickup_player_rank(tournament_caption)
  rank = tournament_caption.match(/ATP Ranking:(.+), Prize/)
  rank[1].strip
end
pickup_surface() click to toggle source
# File lib/atp_scraper/activities/tournament.rb, line 50
def pickup_surface
  surface = @tournament
            .css(".tourney-details")[1]
            .css(".item-details")
            .first.content.gsub(/\t|\s/, "")
  divide_surface(surface)
end
pickup_text(selector) click to toggle source
# File lib/atp_scraper/activities/tournament.rb, line 40
def pickup_text(selector)
  @tournament.css(selector).first.content.strip
end