class Knj::Facebook_connect

Attributes

args[R]

Public Class Methods

new(args) click to toggle source
# File lib/knj/facebook_connect.rb, line 4
def initialize(args)
  require "http2"
  
  @args = args
  
  raise "No app-ID given." if !@args[:app_id]
  raise "No app-secret given." if !@args[:app_secret]
end

Public Instance Methods

base64_urldecode(str) click to toggle source
# File lib/knj/facebook_connect.rb, line 13
def base64_urldecode(str)
  return Base64.decode64("#{str.tr("-_", "+/")}=")
end
get(http, url) click to toggle source
# File lib/knj/facebook_connect.rb, line 17
def get(http, url)
  resp = http.get(:url => url)
  
  if resp.body.length > 0
    begin
      jdata = JSON.parse(resp.body)
      
      error_type = RuntimeError
      if jdata["error"] and jdata["error"]["message"] == "Code was invalid or expired. The session is invalid because the user logged out."
        error_type = ArgumentError
      end
      
      raise error_type, "#{jdata["error"]["type"]}: #{jdata["error"]["message"]}" if jdata["error"]
    rescue JSON::ParserError
      #ignore
    end
  end
  
  return {:json => jdata, :resp => resp, :headers => resp.headers, :body => resp.body}
end
login(args = {}) click to toggle source
# File lib/knj/facebook_connect.rb, line 73
def login(args = {})
  http = Http2.new(
    :host => "graph.facebook.com",
    :ssl => true
  )
  
  atoken = self.token_from_cookie(http, @args[:cookie])
  
  url = "me?access_token=#{Knj::Web.urlenc(atoken)}"
  resp = self.get(http, url)
  data = {"user" => resp[:json]}
  
  if args[:profile_picture]
    pic_data = self.get(http, "#{data["user"]["id"]}/picture?type=large")
    pic_obj = Magick::Image.from_blob(pic_data[:body].to_s)[0]
    data["pic"] = pic_obj
  end
  
  return data
end
wall_post(args) click to toggle source
# File lib/knj/facebook_connect.rb, line 94
def wall_post(args)
  http = Http2.new(
    :host => "graph.facebook.com",
    :ssl => true
  )
  
  atoken = self.token_from_cookie(http)
  post_data = {}
  
  args_keys = [:link, :object_attachment, :picture, :caption, :name, :description, :message, :media]
  args_keys.each do |key|
    if args.key?(key) and args[key]
      post_data[key] = args[key]
    end
  end
  
  res = http.post(:url => "/me/feed?access_token=#{atoken}", :post => post_data)
  raise res.body.to_s.strip if res.code.to_s != "200"
end