Skip to content

Instantly share code, notes, and snippets.

@fabianoleittes
Forked from serradura/fgraph.rb
Created December 21, 2012 18:19
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 fabianoleittes/4354706 to your computer and use it in GitHub Desktop.
Save fabianoleittes/4354706 to your computer and use it in GitHub Desktop.
# require_relative "hget"
# client = FBPublic.new("penaltybr")
# client.data # returns the Graph API public data
# if you need see response details use:
# client.response # returns a Net::HTTPFound instance
# if you need an alias to get the api response use:
# fb_data = FBPublic.get_data("penaltybr") # returns the Graph API public data
require 'json'
class FBPublic
attr_reader :path
def self.get_data(path)
client = self.new(path)
client.data
end
def initialize(path)
@path = path
end
def response
@response ||= client.response
end
def data
@data ||= JSON.parse(response.body)
end
def client
@client ||= HGet.new(api_url)
end
def api_url
@api_url ||= "http://graph.facebook.com/#{path}"
end
end
# "http://graph.facebook.com/penaltybr"
# client = HGet.new("http://graph.facebook.com/penaltybr")
# client.response # returns a Net::HTTPFound instance
require "net/http"
require "uri"
class HGet
attr_reader :url
def initialize(url)
@url = url
end
def uri
@uri ||= URI.parse(@url)
end
def response
@response ||= http.request(Net::HTTP::Get.new(uri.request_uri))
end
def http
@http ||= Net::HTTP.new(uri.host, uri.port)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment