Top Level Namespace

Defined Under Namespace

Modules: ActionController, Clearance, MiniFB Classes: ClearanceMailer

Constant Summary collapse

FB =

Load the configuration for miniFB

YAML.load_file("#{RAILS_ROOT}/config/facebook.yml")
FB_API_KEY =

Set the value in constants for easy use

FB[:api_key]
FB_APP_ID =
FB[:app_id]
FB_SECRET =
FB[:secret]
MAILER_SENDER =
FB[:mailer_sender]
FB_CALLBACK_URL =

This routed will be name with clearance routes as /facebook

"#{FB[:base_url]}/facebook"
FB_CLOSED_URL =

This routed will be name with clearance routes as /facebookclosed

"#{FB[:base_url]}/fbclosed"
LOGGED_PATH =
FB[:after_login_path]
URL_AFTER_CREATE =
FB[:url_after_create]

Instance Method Summary collapse

Instance Method Details

#authenticated_fbu?Boolean

Si da false entonces el usuario se le deniega el acceso

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/facebook_helpers.rb', line 15

def authenticated_fbu?
  token = cookies[:fb_token]
  if token.nil? then return false end
  if token_user(token) == current_user.fbid then return true else return false end 
  #The user is authenticated if the UID than own the token is the same as the one in current user
end

#facebook_fql(fql_query, options = {}) ⇒ Object

Executes an FQL query



68
69
70
71
72
73
74
75
76
# File 'lib/facebook_helpers.rb', line 68

def facebook_fql(fql_query, options={})
    url = "https://api.facebook.com/method/fql.query"
    params = options[:params] || {}
    params["metadata"] = "1" if options[:metadata]
    params["format"] = "JSON"
    params["query"] = fql_query
    options[:params] = params
    return fetch_url(url, options)
end

#facebook_jsObject



34
35
36
# File 'lib/facebook_helpers.rb', line 34

def facebook_js
  render :partial => "facebook/fbjs"
end

#facebook_loginObject



38
39
40
# File 'lib/facebook_helpers.rb', line 38

def 
    return "<fb:login-button scope='publish_stream,email'></fb:login-button>"
end

#facebook_pic_urlObject

TBD: Save the url in the DB. 50x50 px



52
53
54
# File 'lib/facebook_helpers.rb', line 52

def facebook_pic_url
  return "http://graph.facebook.com/#{current_user.fbid}/picture?type=square"
end

#fetch_url(url, options = {}) ⇒ Object

Fetch something fro FB



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/facebook_helpers.rb', line 79

def fetch_url(url, options={})
    begin
        if options[:method] == :post
            puts 'url_post=' + url #if @@logging
            resp = RestClient.post url, options[:params]
        else
            if options[:params] && options[:params].size > 0
                url += '?' + options[:params].map { |k, v| URI.escape("%s=%s" % [k, v]) }.join('&')
            end
            puts 'url_get=' + url #if @@logging
            resp = RestClient.get url
        end

        puts 'resp=' + resp.to_s #if @@logging

        begin
            res_hash = JSON.parse(resp.to_s)
        rescue
            # quick fix for things like stream.publish that don't return json
            res_hash = JSON.parse("{\"response\": #{resp.to_s}}")
        end

        if res_hash.is_a? Array # fql  return this
            res_hash.collect! { |x| Hashie::Mash.new(x) }
        else
            res_hash = Hashie::Mash.new(res_hash)
        end

        if res_hash.include?("error_msg")
            raise FaceBookError.new(res_hash["error_code"] || 1, res_hash["error_msg"])
        end

        return res_hash
    rescue RestClient::Exception => ex
        puts ex.http_code.to_s
        puts 'ex.http_body=' + ex.http_body #if @@logging
        res_hash = JSON.parse(ex.http_body) # probably should ensure it has a good response
        raise MiniFB::FaceBookError.new(ex.http_code, "#{res_hash["error"]["type"]}: #{res_hash["error"]["message"]}")
    end

end

#like_count(myurl) ⇒ Object

CODE FOR FB LIKE

query = “SELECT like_count FROM link_stat WHERE url=#myurl”



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

def like_count(myurl)
  #myurl = "http://challenge.mistter.me:3000/projects/1"
  query = "SELECT like_count FROM link_stat WHERE url=\"#{myurl}\""
  return facebook_fql(query)[0].like_count  
end

Here I’ve written the code to offer methods to simplify FB connections, like login links for example OR methods required outside of the facebook controller inside the gem



4
5
6
# File 'lib/facebook_helpers.rb', line 4

def parse_fb_cookie
  return MiniFB.parse_cookie_information FB_APP_ID, cookies
end

#parse_signed_request(request) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/request_parser.rb', line 3

def parse_signed_request(request)
  encoded_sig, payload = request.split('.', 2)
  sig = urldecode64(encoded_sig)
  data = JSON.parse(urldecode64(payload))
  if data['algorithm'].to_s.upcase != 'HMAC-SHA256'
    raise "Bad signature algorithm: %s" % data['algorithm']
  end
  expected_sig = OpenSSL::HMAC.digest('sha256', FB[:secret], payload)
  if expected_sig != sig
    raise "Bad signature"
  end
  data
end


42
43
44
45
46
47
48
# File 'lib/facebook_helpers.rb', line 42

def sign_out_link(msg="Sign out")
  if user_from_fb? then
    return "<a href='#' onClick='javascript:FB.logout();'>#{msg}</a>"
  else
    return "<a href='/sign_out'>#{msg}</a>"  
  end
end

#token_user(token) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/facebook_helpers.rb', line 22

def token_user(token)
  begin
    @uid = MiniFB.rest(token, "users.getLoggedInUser", {})
    return @uid.to_hash["response"]
  rescue MiniFB::FaceBookError #Is this error happen the token expired
    return nil
  end
end

#urldecode64(str) ⇒ Object



18
19
20
21
22
# File 'lib/request_parser.rb', line 18

def urldecode64(str)
  encoded_str = str.tr('-_', '+/')
  encoded_str += '=' while !(encoded_str.size % 4).zero?
  Base64.decode64(encoded_str)
end

#user_from_fb?Boolean

Returns:

  • (Boolean)


8
9
10
11
12
# File 'lib/facebook_helpers.rb', line 8

def user_from_fb?
  if signed_in? then
  return !current_user.fbid.blank? #If that's not blank then its a FB user
  else return false end
end