Skip to content

Instantly share code, notes, and snippets.

@omghax
Created February 15, 2012 03:11
Show Gist options
  • Save omghax/1832850 to your computer and use it in GitHub Desktop.
Save omghax/1832850 to your computer and use it in GitHub Desktop.
DonorsChoose
require 'cgi'
require 'uri'
require 'net/http'
require 'json'
require 'ostruct'
class DonorsChoose
def initialize(api_key)
@api_key = api_key
end
def projects_near_me(latitude, longitude)
get(:centerlat => latitude, :centerlong => longitude)
end
def projects_by_zip(zipcode)
get(:keyword => zipcode)
end
private
def get(params)
data = JSON.parse(fetch(params))['proposals']
data.collect { |datum| OpenStruct.new(datum) }
end
def fetch(params)
base_uri = 'http://api.donorschoose.org/common/json_feed.html'
uri_params = params.collect do |key, value|
"#{key}=#{CGI.escape(value.to_s)}"
end
uri_params = [uri_params, "APIKey=#{@api_key}"].join("&")
Net::HTTP.get(URI(base_uri + "?" + uri_params))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment