Created
December 21, 2012 18:03
-
-
Save anonymous/4354586 to your computer and use it in GitHub Desktop.
HGet: easy way to get a Net:HTTP reponse
FBPublic: shortcut to do public (without access token) Facebook Graph Api requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# "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