Skip to content

Instantly share code, notes, and snippets.

@michaellarrubis
Last active February 25, 2020 04:41
Show Gist options
  • Save michaellarrubis/fecf44854a025b3f5ff2d349303a75e2 to your computer and use it in GitHub Desktop.
Save michaellarrubis/fecf44854a025b3f5ff2d349303a75e2 to your computer and use it in GitHub Desktop.
plugin-facebook.rb
# Helper
# Getting Page Posts
def get_page_posts
check_fb_plugin_installed = ActiveRecord::Base.connection.data_source_exists? 'plugins_facebook_sns'
fb_plugin = current_site.plugins.where(slug: 'facebook_sn')
status = fb_plugin[0]['status'].to_i
return false if !check_fb_plugin_installed
return false if !fb_plugin.present?
return false if status == 0
fb_pages = current_site.myplugin.all
fetched_page_posts = []
fb_pages.each do |fb_page|
parsed_data = JSON.parse(
Net::HTTP.get(URI(
"https://graph.facebook.com/v3.2/#{fb_page['fb_page_id']}?
fields=
name,
picture,
feed.limit(10){
created_time,
attachments,
updated_time,
message,
description,
full_picture,
name,
permalink_url,
status_type}
&access_token=#{fb_page['fb_page_token'].gsub(/"|\[|\]/, '')}"
))
)
if parsed_data['error'].present?
update_status_message(fb_page['fb_page_id'], parsed_data['error']['message'])
else
if ActiveRecord::Base.connection.column_exists?(:plugins_facebook_sns, :status_message)
update_status_message(fb_page['fb_page_id'], "Page request is okay.")
fetched_page_posts << parsed_data
else
fetched_page_posts << parsed_data
end
end
end
all_page_posts = []
fetched_page_posts.each do |post|
if post['feed'].present?
post['feed']['data'].each do |post_item|
page_posts_item = {
'page_name' => post['name'],
'page_picture_url' => post['picture']['data']['url'],
'post_name' => post_item['name'],
'post_permalink_url' => post_item['permalink_url'],
'post_status_type' => post_item['status_type'],
'post_attachments' => post_item['attachments'],
'post_description' => post_item['description'],
'post_created' => post_item['created_time'],
'post_message' => post_item['message'],
'post_full_picture' => post_item['full_picture'],
}
all_page_posts << page_posts_item
end
end
end
return all_page_posts.sort_by {|post| post['post_created']}.reverse
end
# Settings Part
# Class
class Plugins::FacebookSn::AdminController < CamaleonCms::Apps::PluginsAdminController
include Plugins::FacebookSn::MainHelper
def index
end
# show settings form
def settings
@fb_pages = current_site.myplugin.all
end
# save values from settings form
def save_settings
if params[:fb_page_name].present?
params[:fb_page_id].present? &&
params[:fb_page_token].present?
if !current_site.myplugin.is_page_id_duplicated?(params[:fb_page_id])
fb_page = current_site.myplugin.new(
fb_page_name: params[:fb_page_name],
fb_page_id: params[:fb_page_id],
fb_page_token: params[:fb_page_token])
if fb_page.save!
redirect_to url_for(action: :settings), notice: 'FB Page credentials saved successfully'
else
redirect_to url_for(action: :settings), error: "FB Page credentials unsuccessfully saved"
end
else
redirect_to url_for(action: :settings), error: "Page ID must be unique."
end
else
redirect_to url_for(action: :settings), error: 'All fields must be filled!'
end
end
def edit
@fb_page = current_site.myplugin.find(params[:id])
end
def update
fb_page_id = current_site.myplugin.find(params[:id])
if params[:fb_page_name].present?
params[:fb_page_id].present? &&
params[:fb_page_token].present?
fb_page = fb_page_id.update(
fb_page_name: params[:fb_page_name],
fb_page_id: params[:fb_page_id],
fb_page_token: params[:fb_page_token])
if fb_page
redirect_to url_for(action: :settings), notice: 'FB Page credentials updated successfully'
else
redirect_to url_for(action: :settings), error: "FB Page credentials unsuccessfully updated"
end
else
redirect_to url_for(action: :edit), error: "Page ID must be unique."
end
end
def destroy
fb_page = current_site.myplugin.find(params[:id])
if fb_page.delete
redirect_to url_for(action: :settings), notice: 'FB Page credentials removed successfully'
else
redirect_to url_for(action: :settings), error: 'FB Page credentials unsuccessfully removed'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment