Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 62 additions & 5 deletions lib/fb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,50 @@
module Fb
class Error < StandardError; end

class Business
attr_reader :id, :name

def initialize(options = {})
@access_token = options[:access_token]
@id = options[:id]
@name = options[:name]
end

def owned_pages
@pages ||= begin
params = { access_token: @access_token, fields: "category,name" }
request = HTTPRequest.new path: "/#{@id}/owned_pages", params: params
request.run.body['data'].map do |page_data|
unless page_data.key?("access_token")
page_data.merge! access_token: @access_token
end
Page.new symbolize_keys(page_data)
end
end
end

def client_pages
@client_pages ||= begin
params = { access_token: @access_token, fields: "category,name" }
request = HTTPRequest.new path: "/#{@id}/client_pages", params: params
request.run.body['data'].map do |page_data|
unless page_data.key?("access_token")
page_data.merge! access_token: @access_token
end
Page.new symbolize_keys(page_data)
end
end
end

private

def symbolize_keys(hash)
{}.tap do |new_hash|
hash.each_key{|key| new_hash[key.to_sym] = hash[key]}
end
end
end

class User
def initialize(options = {})
@access_token = options[:access_token]
Expand All @@ -26,23 +70,36 @@ def email
end
end

def revoke_permissions
params = { access_token: @access_token }
request = HTTPRequest.new path: '/me/permissions', params: params, method: :delete
request.run.body['success']
end

def pages
@pages ||= begin
params = { access_token: @access_token }
request = HTTPRequest.new path: '/me/accounts', params: params
request.run.body['data'].map do |page_data|
# unless page_data.key?("access_token")
# page_data.merge access_token: @access_token
# page_data.merge! access_token: @access_token
# end
Page.new symbolize_keys(page_data)
end
end
end

def revoke_permissions
params = { access_token: @access_token }
request = HTTPRequest.new path: '/me/permissions', params: params, method: :delete
request.run.body['success']
def businesses
@pages ||= begin
params = { access_token: @access_token }
request = HTTPRequest.new path: '/me/businesses', params: params
request.run.body['data'].map do |business_data|
unless business_data.key?("access_token")
business_data.merge! access_token: @access_token
end
Business.new symbolize_keys(business_data)
end
end
end

private
Expand Down
Loading