Skip to content

Instantly share code, notes, and snippets.

@leftis
Created February 4, 2014 12:52
Show Gist options
  • Save leftis/8803052 to your computer and use it in GitHub Desktop.
Save leftis/8803052 to your computer and use it in GitHub Desktop.
require 'open-uri'
require 'pp'
require 'json'
require 'ostruct'
class FoursquareApiParser
attr_accessor :response
def initialize(url)
@response = JSON.load(open(url))["response"]
end
def self.find(collection, node, attributes=[])
collection[node].each do |n|
pp make_object(attributes, n)
FoursquareApiParser.find(n, node, attributes) if n[node] && n[node].size > 1
end
end
def self.make_object(attributes, values)
obj = OpenStruct.new
attributes.each do |a|
obj.send "#{a}=", values[a]
end
obj
end
end
foursquare_api = FoursquareApiParser.new 'https://api.foursquare.com/v2/venues/categories?oauth_token=KLK3CSSK2NI5FZEEMK3RIOIEVI1UH4DOJ4OGTPTD0LZ3VVUZ&v=20140204'
FoursquareApiParser.find(foursquare_api.response, "categories", ["name", "id", "pluralName"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment