Module: FBCLI

Defined in:
lib/fbcli/auth.rb,
lib/fbcli/facebook.rb

Constant Summary collapse

API_VERSION =
"2.7"

Class Method Summary collapse

Class Method Details

.expiration_str(seconds) ⇒ Object



65
66
67
68
69
# File 'lib/fbcli/auth.rb', line 65

def self.expiration_str(seconds)
  h, m = seconds.divmod(60 * 3600)

  "#{h} hours and #{m.div 3600} minutes"
end

.get_access_token(port, app_id, auth_code, app_secret) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fbcli/auth.rb', line 52

def self.get_access_token(port, app_id, auth_code, app_secret)
  auth_uri = "https://graph.facebook.com/v#{API_VERSION}/oauth/access_token?" +
    "client_id=#{app_id}" +
    "&redirect_uri=http://localhost:#{port}/" +
    "&client_secret=#{app_secret}" +
    "&code=#{auth_code}"

  res = Net::HTTP.get_response(URI.parse(auth_uri))
  res = JSON.parse(res.body)

  [res["access_token"], res["expires_in"]]
end

.init_api(global_options) ⇒ Object



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

def self.init_api(global_options)
  # Access token passed from the command line takes precedence
  if not global_options[:token].nil?
    $config['access_token'] = global_options[:token]
  end

  if $config['access_token'].nil? or $config['access_token'].empty?
    exit_now! "You must first acquire an access token; run: #{APP_NAME} login"
  end

  Koala::Facebook::API.new($config['access_token'])
end

.listen_for_auth_code(port, app_id, app_secret) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/fbcli/auth.rb', line 9

def self.listen_for_auth_code(port, app_id, app_secret)
  uri = "https://www.facebook.com/dialog/oauth?client_id=#{app_id}" +
    "&redirect_uri=http://localhost:#{port}/" +
    "&scope=user_likes,user_friends,user_photos,user_posts,user_events"

  puts "Please open: #{uri}"
  puts
  puts "Waiting for authorization code on port #{port}..."
  puts

  server = WEBrick::HTTPServer.new(
    :Port => port,
    :SSLEnable => false,
    :Logger => WEBrick::Log.new(File.open(File::NULL, 'w')),
    :AccessLog => []
  )

  access_token = nil

  server.mount_proc '/' do |req, res|
    key, value = req.query_string.split '=', 2

    if key == "code"
      access_token = get_access_token(port, app_id, value, app_secret)
    else
      puts "Received unexpected request: #{req.query_string}"
      # TODO: Handle forseeable cases
    end

    res.body = 'You may now close this window.'
    server.shutdown
  end

  # Allow CTRL+C intervention
  trap 'INT' do server.shutdown end

  # This thread will block execution until server shuts down
  server.start

  # Return access token
  access_token
end

.page_items(global_options, cmd, separator = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fbcli/facebook.rb', line 32

def self.page_items(global_options, cmd, separator = nil)
  items = request_data(global_options, cmd)

  while not items.nil? do
    items.each_with_index { |item, idx|
      yield item

      unless separator.nil? or idx == items.size - 1
        puts separator
      end
    }

    items = items.next_page
  end
end

.request_data(global_options, cmd) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fbcli/facebook.rb', line 17

def self.request_data(global_options, cmd)
  api = init_api(global_options)

  begin
    data = api.get_connections("me", cmd)
  rescue Koala::Facebook::APIError => e
    exit_now! \
      "Koala #{e.fb_error_type} (code #{e.fb_error_code})" +
      if not e.http_status.nil? then " HTTP status: #{e.http_status}" else "" end +
      "\n  #{e.fb_error_message}"
  end

  data
end