Created
June 4, 2011 04:50
-
-
Save vivien/1007591 to your computer and use it in GitHub Desktop.
Simple and Stupid Ruby API for Coderwall.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
... or just use the provided hash as a hash, since the order isn't guaranteed per JSON spec.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this simple code!
Since Coderwall changed the order of the parameters in the JSON API, you need to modify the Achievement class to this:
I forked and did this here: https://gist.github.com/4053443