Class: Sinatra::FacebookObject
- Inherits:
-
Object
- Object
- Sinatra::FacebookObject
- Defined in:
- lib/sinbook.rb
Defined Under Namespace
Classes: APIProxy
Constant Summary collapse
- MimeBoundary =
"--SoMeTeXtWeWiLlNeVeRsEe\r\n"
- MimePart =
%[Content-Disposition: form-data; name="%s"\r\n\r\n%s\r\n] + MimeBoundary
- MimeImage =
%[Content-Disposition: form-data; filename="%s"\r\nContent-Type: image/%s\r\n\r\n%s\r\n] + MimeBoundary
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#app ⇒ Object
readonly
Returns the value of attribute app.
- #app_id ⇒ Object
- #callback(postfix = nil) ⇒ Object
-
#secret ⇒ Object
Returns the value of attribute secret.
- #url(postfix = nil) ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #addurl ⇒ Object
- #appurl ⇒ Object
-
#initialize(app) ⇒ FacebookObject
constructor
A new instance of FacebookObject.
- #params ⇒ Object
- #redirect(url) ⇒ Object
- #request(method, opts = {}) ⇒ Object
- #require_login! ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(app) ⇒ FacebookObject
Returns a new instance of FacebookObject.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sinbook.rb', line 17 def initialize app if app.respond_to?(:options) @app = app [ :api_key, :secret, :app_id, :url, :callback, :symbolize_keys ].each do |var| instance_variable_set("@#{var}", app..send("facebook_#{var}")) end else [ :api_key, :secret, :app_id ].each do |var| raise ArgumentError, "missing option #{var}" unless app[var] instance_variable_set("@#{var}", app[var]) end [:url, :callback, :symbolize_keys ].each do |var| instance_variable_set("@#{var}", app[var]) if app.has_key?(var) end end end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
36 37 38 |
# File 'lib/sinbook.rb', line 36 def api_key @api_key end |
#app ⇒ Object (readonly)
Returns the value of attribute app.
35 36 37 |
# File 'lib/sinbook.rb', line 35 def app @app end |
#app_id ⇒ Object
39 40 41 |
# File 'lib/sinbook.rb', line 39 def app_id @app_id || self[:app_id] end |
#callback(postfix = nil) ⇒ Object
47 48 49 |
# File 'lib/sinbook.rb', line 47 def callback postfix=nil postfix ? "#{@callback}#{postfix}" : @callback end |
#secret ⇒ Object
Returns the value of attribute secret.
36 37 38 |
# File 'lib/sinbook.rb', line 36 def secret @secret end |
#url(postfix = nil) ⇒ Object
43 44 45 |
# File 'lib/sinbook.rb', line 43 def url postfix=nil postfix ? "#{@url}#{postfix}" : @url end |
Class Method Details
.connect ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/sinbook.rb', line 259 def self.connect sock = TCPSocket.new(@api_server_ip ||= Resolv.getaddress('api.facebook.com'), 80) begin timeout = [3,0].pack('l_2') # 3 seconds sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, timeout sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, timeout rescue Exception => ex # causes issues on solaris? puts ex.inspect end sock end |
.request(data, mime = false) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/sinbook.rb', line 272 def self.request data, mime=false if @keepalive @socket ||= connect else @socket = connect end @socket.print "POST /restserver.php HTTP/1.1\r\n" @socket.print "Host: api.facebook.com\r\n" @socket.print "Connection: keep-alive\r\n" if @keepalive if mime @socket.print "Content-Type: multipart/form-data; boundary=#{MimeBoundary[2..-3]}\r\n" @socket.print "MIME-version: 1.0\r\n" else @socket.print "Content-Type: application/x-www-form-urlencoded\r\n" end @socket.print "Content-Length: #{data.length}\r\n" @socket.print "\r\n#{data}\r\n" @socket.print "\r\n\r\n" buf = '' headers = '' headers_done = false chunked = true while true line = @socket.gets headers << line unless headers_done raise Errno::ECONNRESET unless line if line == "\r\n" # end of headers/chunk unless headers_done headers_done = true if headers =~ /Encoding: chunked/i chunked = true else len = headers[/Content-Length: (\d+)/i,1].to_i buf = @socket.read(len) break # done! end end line = @socket.gets # get size of next chunk if line.strip! == '0' # 0 sized chunk @socket.gets # read last crlf break # done! end buf << @socket.read(line.to_i(16)) # read in chunk end end buf rescue Errno::EPIPE, Errno::ECONNRESET @socket = nil retry ensure @socket.close if @socket and !@keepalive end |
Instance Method Details
#[](key) ⇒ Object
101 102 103 |
# File 'lib/sinbook.rb', line 101 def [] key params[key] end |
#addurl ⇒ Object
51 52 53 |
# File 'lib/sinbook.rb', line 51 def addurl "http://apps.facebook.com/add.php?api_key=#{self.api_key}" end |
#appurl ⇒ Object
55 56 57 |
# File 'lib/sinbook.rb', line 55 def appurl "http://www.facebook.com/apps/application.php?id=#{self.app_id}" end |
#params ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sinbook.rb', line 77 def params return {} unless valid? app.env['facebook.params'] ||= \ app.env['facebook.vars'].inject({}) do |h,(k,v)| s = k.to_sym case k when 'friends' h[s] = v.split(',').map{|e|e.to_i} when /time$/ h[s] = Time.at(v.to_f) when 'expires' v = v.to_i h[s] = v>0 ? Time.at(v) : v when 'user', 'app_id', 'canvas_user' h[s] = v.to_i when /^(logged_out|position_|in_|is_|added)/ h[s] = v=='1' else h[s] = v end h end end |
#redirect(url) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/sinbook.rb', line 67 def redirect url url = self.url + url unless url =~ /^http/ if params[:in_iframe] app.body "<script type=\"text/javascript\">top.location.href=\"#{url}\"</script>" else app.body "<fb:redirect url='#{url}'/>" end throw :halt end |
#request(method, opts = {}) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/sinbook.rb', line 186 def request method, opts = {} if method == 'photos.upload' image = opts.delete :image end opts = { :api_key => self.api_key, :call_id => Time.now.to_f, :format => 'JSON', :v => '1.0', :session_key => %w[ photos.upload ].include?(method) ? nil : params[:session_key], :method => method }.merge(opts) args = opts.map{ |k,v| next nil unless v "#{k}=" + case v when Hash Yajl::Encoder.encode(v) when Array if k == :tags Yajl::Encoder.encode(v) else v.join(',') end else v.to_s end }.compact.sort sig = Digest::MD5.hexdigest(args.join+self.secret) if method == 'photos.upload' data = MimeBoundary data += opts.merge(:sig => sig).inject('') do |buf, (key, val)| if val buf << (MimePart % [key, val]) else buf end end data += MimeImage % ['upload.jpg', 'jpg', image.respond_to?(:read) ? image.read : image] else data = Array["sig=#{sig}", *args.map{|a| a.gsub('&','%26') }].join('&') end ret = self.class.request(data, method == 'photos.upload') ret = if ['true', '1'].include? ret true elsif ['false', '0'].include? ret false elsif (n = Integer(ret) rescue nil) n else Yajl::Parser.parse(ret, :symbolize_keys => @symbolize_keys) end if ret.is_a?(Hash) and (ret['error_code'] or ret[:error_code]) err = FacebookError.new(ret['error_msg'] || ret[:error_msg]) err.data = ret raise err end ret end |
#require_login! ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/sinbook.rb', line 59 def require_login! if valid? redirect addurl unless params[:user] else app.redirect url end end |
#valid? ⇒ Boolean
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/sinbook.rb', line 105 def valid? if app.nil? return false elsif app.params['fb_sig'] # canvas/iframe mode prefix = 'fb_sig' vars = app.request.POST[prefix] ? app.request.POST : app.request.GET elsif app.request.[api_key] # fbconnect mode prefix = api_key vars = app.request. else return false end if app.env['facebook.valid?'].nil? fbvars = {} sig = Digest::MD5.hexdigest(vars.map{|k,v| if k =~ /^#{prefix}_(.+)$/ fbvars[$1] = v "#{$1}=#{v}" end }.compact.sort.join+self.secret) if app.env['facebook.valid?'] = (vars[prefix] == sig) app.env['facebook.vars'] = fbvars end end app.env['facebook.valid?'] end |