Class: FacebookGraphr::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/facebook_graphr.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(facebook_uid, access_token) ⇒ Session

Returns a new instance of Session.



40
41
42
43
44
45
46
# File 'lib/facebook_graphr.rb', line 40

def initialize(facebook_uid, access_token)
  self.user_id = facebook_uid
  self.access_token = access_token
  self.api_key = FacebookGraphr.config[:api_key]
  self.app_id = FacebookGraphr.config[:app_id]
  self.secret_key = FacebookGraphr.config[:secret_key]
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



27
28
29
# File 'lib/facebook_graphr.rb', line 27

def access_token
  @access_token
end

#api_keyObject

Returns the value of attribute api_key.



27
28
29
# File 'lib/facebook_graphr.rb', line 27

def api_key
  @api_key
end

#app_idObject

Returns the value of attribute app_id.



27
28
29
# File 'lib/facebook_graphr.rb', line 27

def app_id
  @app_id
end

#secret_keyObject

Returns the value of attribute secret_key.



27
28
29
# File 'lib/facebook_graphr.rb', line 27

def secret_key
  @secret_key
end

#user_idObject

Returns the value of attribute user_id.



27
28
29
# File 'lib/facebook_graphr.rb', line 27

def user_id
  @user_id
end

Class Method Details



29
30
31
32
33
34
35
36
37
38
# File 'lib/facebook_graphr.rb', line 29

def self.new_from_cookie(cookie)
  ::Rails.logger.info(cookie.inspect)
  # Sometimes cookie comes in with extra quotes in front and back
  cookie = cookie.chomp('"').reverse.chomp('"').reverse
  cookie_hash = cookie.split("&").inject({}) do |hash, pair|
    k, v = pair.split('=')
    hash.merge(k.to_sym => v)
  end
  self.new(cookie_hash[:uid], cookie_hash[:access_token])
end

Instance Method Details

#api(path, params = {}, method = :get) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/facebook_graphr.rb', line 48

def api(path, params = {}, method = :get)
  params = params.reverse_merge(:access_token => self.access_token)
  code, body = if method == :get
    param_string = params.map {|k, v| "#{k}=#{v}"}.join("&")
    api_get(URI.escape(BASE_URL + path + "?" + param_string))
  else
    api_post(URI.escape(BASE_URL + path), options)
  end
  if code == 200
    JSON.parse(body)
  end
end

#api_get(uri) ⇒ Object

Set these up to be pluggable/overridable (e.g. so you can override them in app engine)



62
63
64
65
# File 'lib/facebook_graphr.rb', line 62

def api_get(uri)
  res = HTTParty.get(uri)
  [res.code, res.body]
end

#api_post(uri, options) ⇒ Object



67
68
69
70
# File 'lib/facebook_graphr.rb', line 67

def api_post(uri, options)
  res = HTTParty.post(uri, options)
  [res.code, res.body]
end