dev.to karma rank

Davide Santangelo - May 20 '19 - - Dev Community

Hi

just for fun I implemented a very simple algorithm that allow to calculate "Karma" rank of dev.to website, based on actual dev.to v0 API.

This is a simple ruby code ( you must install rest-client gem )

class DevToKarma 
  API_URL = 'https://dev.to/api'

  def initialize(username)
    @username = username
  end

  attr_reader :username

  def posts
    page = 1
    result = []

    loop do
      response = RestClient.get "#{API_URL}/articles?username=#{username}&page=#{page}"
      posts = JSON.parse(response.body)
      result << posts

      page = page + 1
      break if posts.size == 0 
    end

    result.flatten
  end

  def user
    response = RestClient.get "#{API_URL}/users/by_username?url=#{username}"
    JSON.parse(response.body)
  end

  def karma
    joined_at_timestamp = Time.parse(user.dig('joined_at')).to_i 

    posts_count = posts.size
    total_comments_count = posts.map { |r| r['comments_count'] }.sum
    total_positive_reactions_count = posts.map { |r| r['positive_reactions_count'] }.sum

    karma = ( posts_count + total_comments_count + total_positive_reactions_count ).to_f / ( joined_at_timestamp / 1000 / 1000 ) * 100

    karma.to_i
  end
end
Enter fullscreen mode Exit fullscreen mode

Examples

  DevToKarma.new('daviduco').karma # My own karma
  => 23 
Enter fullscreen mode Exit fullscreen mode
  DevToKarma.new('ben').karma # Ben Halper karma
  => 3529 
Enter fullscreen mode Exit fullscreen mode

obviously it is a "stupid" and little mathematical solution. Many other considerations and variables to use are missing. But it's a start!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player