Class: FacebookFeed::FeedDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/facebook_feed/feed_downloader.rb

Overview

TO-DO: Deal with expired access tokens, which render urls in @feed_urls useless Create FacebookFeed errors that are raised when FeedDownloader isn’t properly initialized

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ FeedDownloader

Initialize an instance of FeedDownloader like so opts = { :feed_id => 1234567, :access_token => ‘ABCDEFGHI’ } downloader = FeedDownloader.new(opts)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/facebook_feed/feed_downloader.rb', line 13

def initialize(args)
  raise FacebookFeed::InvalidFeedDownloaderError, "FeedDownloader must be instantiated with a Ruby hash" unless args.is_a?(Hash)
  unless (args.keys.length == 2) and args.keys.include?(:feed_id) and args.keys.include?(:access_token)
    raise FacebookFeed::InvalidFeedDownloaderError, "FeedDownloader must be configured with a hash with two keys only: :feed_id and :access_token"
  end

  args.each do |k, v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end

  base_url = "https://graph.facebook.com/#{@feed_id}/feed?access_token=#{@access_token}"
  @feed_urls = []
  @feed_urls << base_url
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



7
8
9
# File 'lib/facebook_feed/feed_downloader.rb', line 7

def access_token
  @access_token
end

#feed_idObject (readonly)

Returns the value of attribute feed_id.



7
8
9
# File 'lib/facebook_feed/feed_downloader.rb', line 7

def feed_id
  @feed_id
end

Instance Method Details

#download_postsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/facebook_feed/feed_downloader.rb', line 32

def download_posts
  # Return an array of posts in JSON format   
  unless @feed_urls.empty?
    current_url = @feed_urls.shift
    begin
      content_hash = get_content_hash(current_url)
    rescue RestClient::InternalServerError => e
      raise FacebookFeed::InvalidCredentialsError,
        "Invalid Facebook Group ID or access token:\nGroup ID: #{@feed_id}\nAccess Token: #{@access_token}"
    end
    add_urls_if_any(@feed_urls, content_hash)
    extract_posts(content_hash)
  end
end

#has_more_posts?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/facebook_feed/feed_downloader.rb', line 28

def has_more_posts?
  !@feed_urls.empty?
end