module FBK

Constants

Error
FACEBOOK_GRAPH_URL
FACEBOOK_URL

Attributes

client_id[W]
client_secret[W]

Public Instance Methods

authorize_uri(params) click to toggle source
# File lib/fbk.rb, line 18
def authorize_uri(params)
  params[:client_id] = @client_id
  params[:scope] = params[:scope].join(",")

  query_string = params_to_query_string(params)

  "#{FACEBOOK_URL}/dialog/oauth?#{query_string}"
end
configure() { |self| ... } click to toggle source
# File lib/fbk.rb, line 14
def configure
  yield self
end
get_access_token(params) click to toggle source
# File lib/fbk.rb, line 27
def get_access_token(params)
  params[:client_id]     = @client_id
  params[:client_secret] = @client_secret

  query_string = params_to_query_string(params)

  get("#{FACEBOOK_GRAPH_URL}/oauth/access_token?#{query_string}")[:access_token]
end
get_user_friends(token) click to toggle source
# File lib/fbk.rb, line 40
def get_user_friends(token)
  json = get("#{FACEBOOK_GRAPH_URL}/me/friends?access_token=#{token}")

  return [] if json[:data].empty?

  friends = get_friend_ids(json)

  while json[:paging][:next] do
    json = get(json[:paging][:next])
    friends.concat(get_friend_ids(json))
  end

  friends
end
get_user_info(token) click to toggle source
# File lib/fbk.rb, line 36
def get_user_info(token)
  get("#{FACEBOOK_GRAPH_URL}/me?access_token=#{token}&fields=id,email")
end
get_user_picture(token, params = {}) click to toggle source
# File lib/fbk.rb, line 55
def get_user_picture(token, params = {})
  params[:access_token]  = token
  params[:client_id]     = @client_id
  params[:client_secret] = @client_secret
  params[:redirect]      = false

  query_string = params_to_query_string(params)

  get("#{FACEBOOK_GRAPH_URL}/me/picture?#{query_string}")[:data]
end

Private Instance Methods

get(endpoint) click to toggle source
# File lib/fbk.rb, line 72
def get(endpoint)
  response = Nestful::Request.new(endpoint, timeout: 3).execute
  JSON.parse(response.body, symbolize_names: true)
rescue Nestful::BadRequest,
       Nestful::SSLError,
       Nestful::ServerError,
       Nestful::ErrnoError,
       Nestful::TimeoutError => error
  raise FBK::Error.new(error.message)
end
get_friend_ids(json) click to toggle source
# File lib/fbk.rb, line 68
def get_friend_ids(json)
  json[:data].map { |friend| friend[:id] }
end
params_to_query_string(params) click to toggle source
# File lib/fbk.rb, line 83
def params_to_query_string(params)
  params.map { |k,v| "#{k}=#{v}" }.join("&")
end