Skip to content

Instantly share code, notes, and snippets.

@picandocodigo
Forked from vivien/coderwall.rb
Created November 11, 2012 02:37
Show Gist options
  • Save picandocodigo/4053443 to your computer and use it in GitHub Desktop.
Save picandocodigo/4053443 to your computer and use it in GitHub Desktop.
Simple and Stupid Ruby API for Coderwall.com
# Simple and Stupid Ruby API for Coderwall.com
# Vivien Didelot <[email protected]>
require "open-uri"
require "json"
module CoderWall
class Achievement
attr_reader :name, :badge, :description, :date
def initialize(hashed_badge)
@name, @description, @date, @badge = hashed_badge.values
end
end
module_function
def achievements_of(username)
raise(ArgumentError, "Invalid username") if username.empty?
url = URI.escape("http://coderwall.com/#{username}.json")
begin
response = JSON.load(open(url))
rescue OpenURI::HTTPError
raise(ArgumentError, "Invalid username")
end
response["badges"].map { |badge| Achievement.new(badge) }
end
end
#!/bin/env ruby
require "coderwall"
begin
achievements = CoderWall.achievements_of("picandocodigo")
puts achievements.class # => Array
puts achievements.count # => 5
puts achievements.first.class # => CoderWall::Achievement
puts achievements.first.name # => Walrus
puts achievements.first.badge # => http://coderwall.com/images/badges/walrus.png
puts achievements.first.description # => "The walrus is no stranger to variety..."
CoderWall.achievements_of("slkfjsldkfj") # => fail: Invalid username
rescue => e
puts "fail: " + e.message
end
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment