Skip to content

Instantly share code, notes, and snippets.

@leshill
Created February 14, 2012 20:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leshill/1830164 to your computer and use it in GitHub Desktop.
Save leshill/1830164 to your computer and use it in GitHub Desktop.
module DonorsChoose
extend self
attr_accessor :api_key
def projects_near_me(latitude, longitude)
Request.get(:centerLat => latitude, :centerLong => longitude)
end
def projects_by_zip(zipcode)
Request.get(:keyword => zipcode)
en
end
class DonorsChoose::Request
def self.get(params)
new(params).get
end
def initialize(params)
@params = params
end
def get
data = JSON.parse(fetch)["proposals"]
data.collect {|datum| OpenStruct.new(datum)}
end
private
def fetch
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=#{DonorsChoose.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