Skip to content

Instantly share code, notes, and snippets.

@tobynet
Forked from vivien/coderwall.rb
Created November 27, 2011 18:17
Show Gist options
  • Save tobynet/1397927 to your computer and use it in GitHub Desktop.
Save tobynet/1397927 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
def initialize(hashed_badge)
@name, @badge, @description = 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("v0n")
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