From abdaa4fff05049d5bca2bb0ca416c0d9aa2ba761 Mon Sep 17 00:00:00 2001 From: Kang-Kyu Lee Date: Tue, 11 Feb 2025 15:49:04 -0800 Subject: [PATCH 1/2] Add businesses, business pages, revoke --- lib/fb.rb | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/lib/fb.rb b/lib/fb.rb index 6d69d48..bf3a5b3 100644 --- a/lib/fb.rb +++ b/lib/fb.rb @@ -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] @@ -26,23 +70,38 @@ 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 + puts page_data 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 + puts business_data + Business.new symbolize_keys(business_data) + end + end end private From 1824bb8e6f1ad17fdc533fe8ad1f44d269b8e073 Mon Sep 17 00:00:00 2001 From: Kang-Kyu Lee Date: Tue, 11 Feb 2025 15:49:59 -0800 Subject: [PATCH 2/2] Remove puts --- lib/fb.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/fb.rb b/lib/fb.rb index bf3a5b3..15a3978 100644 --- a/lib/fb.rb +++ b/lib/fb.rb @@ -84,7 +84,6 @@ def pages # unless page_data.key?("access_token") # page_data.merge! access_token: @access_token # end - puts page_data Page.new symbolize_keys(page_data) end end @@ -98,7 +97,6 @@ def businesses unless business_data.key?("access_token") business_data.merge! access_token: @access_token end - puts business_data Business.new symbolize_keys(business_data) end end