Sha256: a1bb3a28338aa776296b62d3440def652acaf1ff462eabc8b796025022ad61f0

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require 'open-uri'
require 'nokogiri'

##
# Module for parsing Linode employee info
module Linodians
  DATA_URL = 'https://www.linode.com/employees'
  PHOTO_URL = 'https://www.linode.com/media/images/employees/%s.png'

  class << self
    ##
    # Insert a helper .new() method for creating a new Group object
    def new(*args)
      self::Group.new(*args)
    end

    def load_data(data = nil)
      (data || download_data).map { |x| Employee.new x }
    end

    private

    def download_data
      Nokogiri::HTML(open(DATA_URL)).css('.employee-display').map do |block|
        parse_user(block).merge parse_social(block)
      end
    end

    def parse_user(block)
      {
        username: block.at_css('img')['img-name'],
        fullname: block.at_css('strong').text,
        title: block.at_css('small').text
      }
    end

    def parse_social(block)
      links = block.css('a.employee-link').map do |link|
        # Social site name from CSS class, link target
        [parse_class(link[:class]), link['href']]
      end
      links = Hash[links]
      links.each { |k, v| links[k] = parse_handle(k, v) }
      links.merge(social: links.keys)
    end

    def parse_class(text)
      text.split.last.split('-').last.to_sym
    end

    def parse_handle(type, link)
      return link if type == :linkedin
      link.split('/').last
    end
  end
end

require 'linodians/version'
require 'linodians/employee'
require 'linodians/group'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
linodians-1.0.0 lib/linodians.rb