Sha256: f85c62265b545e9a362f59c60d32558532dcbf8f83eb19674af07b7ecaad1738

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'nokogiri'
require 'open-uri'
require 'date'
# Author:   Lee Chen (mailto: chung1350@hotmail.com)
# License:  MIT
module CodeBadges
  # This class get the user account as an input
  # return a hash of user's badges information
  class CodecademyBadges
    TITLE_XPATH = "//div[@class = 'grid-row']//h5[@class = 'margin-top--1']"
    DATE_XPATH  = "//small[@class = 'text--ellipsis']"
    USERS_URL = 'https://www.codecademy.com/users'
    ACHIEVEMENTS_DIR = 'achievements'

    def self.get_badges(username)
      doc = get_html(username)
      titles = get_titles(doc)
      dates = get_dates(doc)
      integrate(titles, dates)
    end

    def self.get_html(username)
      url = "#{USERS_URL}/#{username}/#{ACHIEVEMENTS_DIR}"
      Nokogiri::HTML(open(url))
    end

    def self.get_titles(document)
      titles = document.xpath(TITLE_XPATH)
      titles.map { |t| t.text }
    end

    def self.get_dates(document)
      dates = document.xpath(DATE_XPATH)
      dates.map { |d| Date.parse(d) }
    end

    def self.integrate(titles, dates)
      badge_array = titles.each_with_index.map do |_, index|
        [titles[index], dates[index]]
      end
      Hash[badge_array]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codebadges-1.0.0 lib/codebadges/badges.rb