Created
June 8, 2011 21:40
-
-
Save sterrym/1015481 to your computer and use it in GitHub Desktop.
facebook posting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Facebook | |
attr_accessor :user | |
def initialize(user) | |
self.user = user | |
end | |
def post(user, params={}) | |
params = normalize_params(params, %w(message picture link name caption description source)) | |
client = OAuth2::Client.new(app_id, app_secret, :site => 'https://graph.facebook.com/', :parse_json => true) | |
client.request(:post, "/#{user_token}/feed", fbparams.merge({ :access_token => access_token })) | |
end | |
def access_token | |
if @access_token.nil? | |
provider = user.authentications.find_by_provider("facebook") | |
@access_token = provider.token if provider.present? | |
end | |
@access_token | |
end | |
private | |
def normalize_params(params, allowed_params) | |
Hash[ *allowed_params.map do |k| | |
[k.to_sym, params[k.to_sym]] if params[k.to_sym].present? | |
end.select(&:present?).flatten ] | |
end | |
def credentials | |
@credentials ||= YAML.load_file(Rails.root.join('config', 'omniauth.yml'))['facebook'].symbolize_keys | |
end | |
def app_id | |
credentials[:app_id] | |
end | |
def app_secret | |
credentials[:app_secret] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment