Module: FacebookCL

Defined in:
lib/facebookcl.rb

Constant Summary collapse

APP_ID =

FacebookCL

119412046078
SERVER_PORT =

my birthday ;o)

6682
GRAPH_URL =
'graph.facebook.com'
NEXT_URL =
'127.0.0.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.uidObject

Returns the value of attribute uid.



23
24
25
# File 'lib/facebookcl.rb', line 23

def uid
  @uid
end

Class Method Details

.authorizeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/facebookcl.rb', line 25

def authorize
  if (File.exists?(config_filename))
    puts "Loading authentication data from #{config_filename}"
    data = JSON.parse(File.open(config_filename){|file| file.read})
    self.access_token = data['access_token']
    self.uid = data['uid']
  else
    perms = ['publish_stream',
             'create_event',
             'offline_access',
             'email',
             'read_stream',
             'read_mailbox']

    params = {'client_id' => APP_ID,
              'redirect_uri' => "http://#{NEXT_URL}:#{SERVER_PORT}",
              'type' => 'user_agent',
              'display' => 'page',
              'scope' => perms.join(',')}

    auth_url = "https://#{GRAPH_URL}/oauth/authorize?" +
      params.map{|key, value| "#{key}=#{value}"}.join('&')

    if (RUBY_PLATFORM.downcase.include?("darwin"))
      `open '#{auth_url}'`
    else
      puts 'Welcome New User! ' +
        'You need to authorize FacebookCL. ' +
        "Please visit this URL in your browser of choice: \n\n" +
        auth_url
    end

    server = FacebookAuthServer.new(NEXT_URL, SERVER_PORT)
    self.access_token =
      server.serve_and_extract_access_token_from_user_agent_flow
    server.close

    self.uid = get('me')['id']

    puts "Saving authentication data to #{config_filename}"
    File.open(config_filename, 'w') { |file|
      data = {'access_token' => access_token,
              'uid' => uid}
      file.puts data.to_json
    }

    puts "Type 'help' for details."
  end
end

.get(url, parameters = {}) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/facebookcl.rb', line 75

def get(url, parameters = {})
  url = "https://#{GRAPH_URL}/#{url}/?access_token=#{access_token}"
  parameters.each {|key, value|
    url += "&#{key}=#{value}"
  }
  data = JSON.parse(open(URI.parse(url)).read)
  return data
end

.post(url, parameters = {}) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/facebookcl.rb', line 84

def post(url, parameters = {})
  parameters['access_token'] = access_token
  http = Net::HTTP.new(GRAPH_URL, 443)
  http.use_ssl = true
  http.post("/#{url}",
            parameters.map{|key, value|"#{key}=#{value}"}.join('&'))
end