Skip to content

Instantly share code, notes, and snippets.

@vivien
Created June 4, 2011 04:50
Show Gist options
  • Select an option

  • Save vivien/1007591 to your computer and use it in GitHub Desktop.

Select an option

Save vivien/1007591 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 <vivien@didelot.org>
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
@picandocodigo
Copy link
Copy Markdown

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:

attr_reader :name, :badge, :description, :date

def initialize(hashed_badge)
  @name, @description, @date, @badge = hashed_badge.values
end

I forked and did this here: https://gist.github.com/4053443

@cbrandolino
Copy link
Copy Markdown

... 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