Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jfgomez86
Created October 11, 2012 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfgomez86/3872841 to your computer and use it in GitHub Desktop.
Save jfgomez86/3872841 to your computer and use it in GitHub Desktop.
Simple (and incomplete) Facebook Graph API wrapper for public objects. It is incomplete on purpose for a ruby lesson :)
require 'rubygems'
require 'open-uri'
require 'json'
require 'ostruct'
def get_graph_object(object_id)
graph_json = open("https://graph.facebook.com/#{object_id}").read
data_hash = JSON.parse(graph_json)
parsed_hash = parse_hash(data_hash)
parsed_data = OpenStruct.new(parsed_hash)
end
def parse_hash(data_hash)
data_hash.inject({}) do |new_hash, data_array|
result_data = data_array[1]
if result_data.is_a? Array
result_data = result_data.map {|hash| parse_hash(hash)}
elsif result_data.is_a? Hash
result_data = OpenStruct.new(parse_hash(result_data))
end
new_hash[data_array[0]] = result_data
new_hash
end
end
# GOAL:
result = get_graph_object("99394368305") #.-> #<OpenStruct name="Facebook Developers", is_published=true, ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment