Logo

dev-resources.site

for different kinds of informations.

Building a GitHub Activity CLI - A Ruby Journey

Published at
12/26/2024
Categories
ruby
cli
github
Author
sulmanweb
Categories
3 categories in total
ruby
open
cli
open
github
open
Author
9 person written this
sulmanweb
open
Building a GitHub Activity CLI - A Ruby Journey

Links


Have you ever wanted to quickly check someone's GitHub activity right from your terminal? I recently took on this challenge by building a simple yet powerful CLI tool in Ruby. Let me walk you through how I built this GitHub Activity CLI, sharing both the technical details and lessons learned along the way.

Project Overview

The GitHub Activity CLI is a command-line tool that fetches and displays a user's recent GitHub activities. It's built with Ruby and follows clean code principles while keeping the implementation straightforward and maintainable.

The Challenge

The project requirements came from roadmap.sh's GitHub User Activity project. The main goals were to:

  • Fetch a user's recent GitHub activities using the GitHub API
  • Display the activities in a readable format
  • Handle various types of GitHub events (pushes, issues, PRs, etc.)
  • Implement proper error handling

Project Structure

I organized the code into a clean, modular structure:

โ”œโ”€โ”€ bin
โ”‚   โ””โ”€โ”€ github-activity    # Executable CLI script
โ””โ”€โ”€ lib
    โ”œโ”€โ”€ activity_formatter.rb   # Formats different event types
    โ”œโ”€โ”€ github_activity.rb      # Main application logic
    โ””โ”€โ”€ github_client.rb        # Handles GitHub API communication
Enter fullscreen mode Exit fullscreen mode

Technical Implementation

1. The GitHub Client

The heart of the application is the GitHubClient class that handles all API communications:

class GitHubClient
  BASE_URL = 'https://api.github.com'

  def fetch_user_events(username)
    uri = URI("#{BASE_URL}/users/#{username}/events")
    response = make_request(uri)

    case response
    when Net::HTTPSuccess
      JSON.parse(response.body)
    when Net::HTTPNotFound
      raise "User '#{username}' not found"
    else
      raise "Failed to fetch GitHub activity: #{response.message}"
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

I used Ruby's built-in Net::HTTP library to keep dependencies minimal. The client handles basic error cases and returns parsed JSON data.

2. Event Formatting

One of the interesting challenges was formatting different types of GitHub events. I created a dedicated ActivityFormatter class that handles this using a clean pattern:

class ActivityFormatter
  def format(event)
    case event['type']
    when 'PushEvent'
      format_push_event(event)
    when 'CreateEvent'
      format_create_event(event)
    when 'IssuesEvent'
      format_issues_event(event)
    # ... other event types
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

This approach makes it easy to add support for new event types and keeps the formatting logic organized.

3. The Main Application

The GitHubActivity class ties everything together:

class GitHubActivity
  def initialize(username)
    @username = username
    @client = GitHubClient.new
    @formatter = ActivityFormatter.new
  end

  def run
    events = @client.fetch_user_events(@username)
    if events.empty?
      puts "No recent activity found for user '#{@username}'"
      return
    end

    display_events(events)
  end
end
Enter fullscreen mode Exit fullscreen mode

User Experience

I focused on making the CLI intuitive and user-friendly. Users can simply run:

./bin/github-activity username
Enter fullscreen mode Exit fullscreen mode

The output is clean and readable:

Recent GitHub activity for sulmanweb:
--------------------------------------------------
Pushed 2 commits to sulmanweb/project
Created branch in sulmanweb/new-repo
Closed issue 'Bug fix needed' in sulmanweb/app
Enter fullscreen mode Exit fullscreen mode

Learning Outcomes

Building this CLI taught me several valuable lessons:

  1. API Integration: Working with the GitHub API showed me the importance of good error handling and response parsing.
  2. Code Organization: Breaking the functionality into separate classes made the code more maintainable and testable.
  3. User Experience: Even for a CLI tool, user experience matters. Clear output and helpful error messages make a big difference.

Supporting the Project

Want to help make the GitHub Activity CLI even better? There are several ways you can contribute:

Financial Support

  • โญ Star the repository to show your support
  • ๐Ÿ’ Consider upvoting my solution at GitHub Activity CLI solution page
  • ๐ŸŽ Share the project with your network

Technical Contributions

  • ๐Ÿ› Report bugs and suggest features through GitHub Issues
  • ๐Ÿ”ง Submit pull requests for bug fixes or enhancements
  • ๐Ÿ“š Help improve the documentation
  • ๐ŸŒ Help with translations if you speak multiple languages

Future Improvements

While the current version works well, there's always room for improvement:

  • Add authentication to handle API rate limits
  • Include more detailed event information
  • Add filtering options for specific event types
  • Implement caching for faster repeated queries

Getting Started

Want to try it out? Here's how:

  1. Clone the repository
  2. Make the CLI executable: chmod +x bin/github-activity
  3. Run it: ./bin/github-activity <username>

Conclusion

Building this GitHub Activity CLI was a great exercise in creating a focused, useful tool while maintaining clean code practices. It shows how a relatively simple idea can be turned into a practical utility that others can use and build upon.

The source code is available on GitHub, and I welcome any feedback or contributions from the community. Happy coding! ๐Ÿš€


Originally published at https://sulmanweb.com.

ruby Article's
30 articles in total
Favicon
Ruby on Rails 8 API not allowing mobile phone connection
Favicon
ruby -run
Favicon
ruby -run, again
Favicon
Ruby on Rails: Your Service Layer is a Lie
Favicon
Die Ruby-Seite von Puppet - Teil 2 - Benutzerdefinierte Funktionen
Favicon
The Ruby side of Puppet - Part 2 - Custom Functions
Favicon
Introducing Ephem
Favicon
What Is It (in Ruby 3.4)?
Favicon
Unable to find Ruby class that definitely exists
Favicon
Devise not accepting JSON Token
Favicon
Ruby on Rails - Calculating pricing based user's purchasing power parity
Favicon
Ruby on Rails 8 - Frontend Rรกpido Usando Tailwind como um Frameworks CSS Classless
Favicon
Use cases for Turbo's Custom Events
Favicon
Meta programming with Ruby Eval: A guide (Part 1)
Favicon
Tracing a method call in Ruby
Favicon
False positives of Lint/Void in assignments
Favicon
Die Ruby-Seite von Puppet - Teil 1 - Benutzerdefinierte Fakten
Favicon
Just committed to learning ruby for sonic pi and rails https://dev.to/highcenburg/2025-roadmap-mastering-ruby-for-sonic-pi-and-rails-696 wish me luck!
Favicon
Building a GitHub Activity CLI - A Ruby Journey
Favicon
I have been thinking of moving forward from Python to Ruby to align my skills with my musicality since I like to learn SonicPi.. But I'm still thinking about it so yeah
Favicon
Task Tracker CLI
Favicon
Rails 8 CRUD: Modern Development Guide 2025
Favicon
When Controllers Take on Too Much Responsibility
Favicon
Ruby on Rails 8 - Frontend Rรกpido com Frameworks CSS Classless ou Class-Light sem CDN
Favicon
Cdg Hoodie or Eric Emanuel Hoodie: The Trend You Need Now
Favicon
Brakeman LSP Support
Favicon
Docker in development: Episode 3
Favicon
Add Invite to Rails 8 Authentication
Favicon
How to Develop an Air Quality Monitoring App with Ruby on Rails
Favicon
School

Featured ones: