Class: FeedieTheFeed::FeedGrabber

Inherits:
Object
  • Object
show all
Includes:
Facebook, RSS
Defined in:
lib/feedie_the_feed/feed_grabber.rb

Overview

This is the main class that does all the job.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FeedGrabber

Returns a new instance of FeedGrabber.

Parameters:

  • options (Hash) (defaults to: {})

    The options to use for the FeedGrabber constructor

Options Hash (options):

  • :facebook_appid (String)

    The Facebook AppID to be used globally in this object

  • :facebook_secret (String)

    The Facebook secret key to be used globally in this object

  • :facebook_posts_limit (Integer)

    The amount of Facebook posts to get by default (should be in 1..100)

Raises:



31
32
33
34
35
36
37
38
39
40
# File 'lib/feedie_the_feed/feed_grabber.rb', line 31

def initialize(options = {})
  # This hash is used to store default values for things like Facebook posts
  # limit.
  @defaults = { facebook_posts_limit: 10 }
  @facebook_appid_global = options[:facebook_appid]
  @facebook_secret_global = options[:facebook_secret]
  fb_posts_limit(
    options[:facebook_posts_limit] || @defaults[:facebook_posts_limit]
  )
end

Instance Method Details

#fb_appid_and_secret_key(facebook_appid, facebook_secret) ⇒ Object

Sets Facebook AppID and secret key for this object.

Parameters:

  • facebook_appid (String)

    Facebook AppID to set to this object

  • facebook_secret (String)

    Facebook secret key to set to this object

Raises:



86
87
88
89
90
91
92
93
94
95
# File 'lib/feedie_the_feed/feed_grabber.rb', line 86

def fb_appid_and_secret_key(facebook_appid, facebook_secret)
  if facebook_appid.is_a?(String) && facebook_secret.is_a?(String)
    @facebook_appid_global = facebook_appid
    @facebook_secret_global = facebook_secret
  else
    raise BadFacebookAppIDAndSecretKey, 'Facebook AppID and secret key ' \
      'must be strings (input classes: ' \
      "#{facebook_appid.class} and #{facebook_secret.class})"
  end
end

#fb_posts_limit(limit) ⇒ Object

Sets global Facebook posts limit for this object.

Parameters:

  • limit (Integer)

    The number of posts to get on Facebook queries (should be in 1..100 range)

Raises:



108
109
110
111
# File 'lib/feedie_the_feed/feed_grabber.rb', line 108

def fb_posts_limit(limit)
  valid_facebook_posts_limit?(limit)
  @facebook_posts_limit_global = limit
end

#get(url, options = {}) ⇒ Array

Gets an array of hashes that are RSS or Facebook posts.

Parameters:

  • url (String)

    The URL to be used as the feed source

  • options (Hash) (defaults to: {})

    The options to use for the get method

Options Hash (options):

  • :facebook_posts_limit (Integer)

    Amout of Facebook posts to get in this operation (should be in 1..100 range)

  • :facebook_appid (String)

    Facebook AppID value to be used with this operation

  • :facebook_secret (String)

    Facebook secret key value to be used with this operation

Returns:

  • (Array)

    The array of hashes of RSS entries or Facebook posts

Raises:

  • (FacebookAuthorisationError)

    Exception used when Facebook authorisation fails with the given credentials

  • (BadUrl)

    Exception used when the RSS or Facebook link provided isn’t a valid one

  • (BadFacebookPostsLimit)

    Exception used when Facebook posts limit is out of range or not an integer

  • (ConnectionFailed)

    Exception used when TCP connection could not be established



63
64
65
66
67
68
69
70
71
72
# File 'lib/feedie_the_feed/feed_grabber.rb', line 63

def get(url, options = {})
  url = sanitise_web_url(url)
  if facebook_url?(url)
    get_facebook_feed(url, options)
  else
    get_rss_feed(url)
  end
rescue Faraday::ConnectionFailed => e
  raise ConnectionFailed, e
end

#reset_fb_appid_and_secret_key!Object

Resets global Facebook AppID and secret key of this object.



75
76
77
78
# File 'lib/feedie_the_feed/feed_grabber.rb', line 75

def reset_fb_appid_and_secret_key!
  @facebook_appid_global = nil
  @facebook_secret_global = nil
end

#reset_fb_posts_limit!Object

Resets global Facebook posts limit of this object.



98
99
100
# File 'lib/feedie_the_feed/feed_grabber.rb', line 98

def reset_fb_posts_limit!
  @facebook_posts_limit_global = @defaults[:facebook_posts_limit]
end