facebook_feed 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+
4
+ # API operations
5
+ require 'facebook_feed/feed_downloader'
6
+
7
+ # Errors
8
+ require 'facebook_feed/errors/feed_error'
9
+ require 'facebook_feed/errors/invalid_credentials_error'
@@ -0,0 +1,4 @@
1
+ module FacebookFeed
2
+ class FeedError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module FacebookFeed
2
+ class InvalidCredentialsError < FeedError
3
+ end
4
+ end
@@ -0,0 +1,105 @@
1
+ module FacebookFeed
2
+ # TO-DO:
3
+ # Deal with expired access tokens, which render urls in @feed_urls useless
4
+ # Create FacebookFeed errors that are raised when FeedDownloader isn't properly initialized
5
+
6
+ class FeedDownloader
7
+ attr_reader :access_token, :feed_id
8
+
9
+ def initialize(args)
10
+ args.each do |k, v|
11
+ instance_variable_set("@#{k}", v) unless v.nil?
12
+ end
13
+ #feed_id = opts[:feed_id]
14
+ #access_token = opts[:access_token]
15
+
16
+ base_url = "https://graph.facebook.com/#{@feed_id}/feed?access_token=#{@access_token}"
17
+ @feed_urls = []
18
+ @feed_urls << base_url
19
+ end
20
+
21
+ def has_more_posts?
22
+ !@feed_urls.empty?
23
+ end
24
+
25
+ def download_posts
26
+ # Return an array of posts in JSON format
27
+ unless @feed_urls.empty?
28
+ current_url = @feed_urls.shift
29
+ begin
30
+ content_hash = get_content_hash(current_url)
31
+ rescue RestClient::InternalServerError => e
32
+ raise FacebookFeed::InvalidCredentialsError,
33
+ "Invalid Facebook Group ID or access token:\nGroup ID: #{@feed_id}\nAccess Token: #{@access_token}"
34
+ end
35
+ add_urls_if_any(@feed_urls, content_hash)
36
+ extract_posts(content_hash)
37
+ end
38
+ end
39
+
40
+ private
41
+ def add_urls_if_any(feed_urls, content_hash)
42
+ posts = content_hash["data"]
43
+ feed_urls << content_hash["paging"]["next"] unless posts.empty?
44
+ end
45
+
46
+ def get_content_hash(url)
47
+ # TO-DO: Write code to authenticate server
48
+ content = RestClient::Resource.new(
49
+ url,
50
+ :verify_ssl => OpenSSL::SSL::VERIFY_NONE
51
+ # :ssl_client_cert => OpenSSL::X509::Certificate.new(File.read("cert.pem")),
52
+ # :ssl_client_key => OpenSSL::PKey::RSA.new(File.read("key.pem"), "passphrase, if any"),
53
+ # :ssl_ca_file => "ca_certificate.pem",
54
+ # :verify_ssl => OpenSSL::SSL::VERIFY_PEER
55
+ ).get
56
+ JSON.parse(content)
57
+ end
58
+
59
+ def extract_posts(content_hash)
60
+ posts = content_hash["data"]
61
+ return [] if posts.nil?
62
+
63
+ docs = []
64
+ posts.each do |post|
65
+ doc = {}
66
+ doc[:poster] = post["from"]["name"]
67
+ doc[:message] = post["message"]
68
+ doc[:type] = post["type"]
69
+ doc[:created_time] = post["created_time"]
70
+ doc[:updated_time] = post["updated_time"]
71
+ unless post["likes"].nil?
72
+ doc[:like_count] = post["likes"]["count"]
73
+ else
74
+ doc[:like_count] = 0
75
+ end
76
+
77
+ # Store each comment's sender, time, number of likes, and
78
+ # the total number of comments for this post
79
+ doc[:comment_count] = post["comments"]["count"]
80
+
81
+ # Check if data is not nil rather than comment count because
82
+ # there's a Facebook bug that doesn't log all comments even
83
+ # if comment number > 0
84
+ unless post["comments"]["data"].nil?
85
+ doc[:comments] = []
86
+ comments = post["comments"]["data"]
87
+ comments.each do |comment|
88
+ comment_data = {}
89
+ comment_data[:commenter] = comment["from"]["name"]
90
+ comment_data[:message] = comment["message"]
91
+ comment_data[:created_time] = comment["created_time"]
92
+ if comment["likes"].nil?
93
+ comment_data[:like_count] = 0
94
+ else
95
+ comment_data[:like_count] = comment["likes"]
96
+ end
97
+ doc[:comments] << comment_data
98
+ end
99
+ end
100
+ docs << doc
101
+ end
102
+ docs
103
+ end
104
+ end
105
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: facebook_feed
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Carson Tang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A Ruby wrapper around Facebook feed APIs. Currently, Facebook Group and
47
+ Feed APIs are supported.
48
+ email: tang.carson@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/facebook_feed.rb
54
+ - lib/facebook_feed/feed_downloader.rb
55
+ - lib/facebook_feed/errors/feed_error.rb
56
+ - lib/facebook_feed/errors/invalid_credentials_error.rb
57
+ homepage: https://github.com/carsontang/facebook_feed
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.24
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Ruby bindings for Facebook feed APIs
81
+ test_files: []