Repository URL to install this package:
|
Version:
0.9.12 ▾
|
module ActionSprout
module Facebook
class PagePosts
PER_PAGE_LIMIT = 100
DEFAULT_SINCE = 2.weeks
POST_FIELDS = 'created_time,message,name,description,type,link,shares,reactions.limit(0).summary(true),comments.limit(0).summary(true)'.freeze
def initialize(id, **options)
@id = id
raise ArgumentError.new('"id" must be a non-empty string'.freeze) unless id.present?
@options = options
end
def each(&block)
response = initial_response.parsed_response
stream_posts(response, block)
get_more_posts response, block
end
private
attr_reader :id, :options
def initial_response
api.get posts_url, query: posts_query
end
def api
@_api ||= options[:api] || API.new
end
def stream_posts(response, block)
response['data'].each(&block)
end
def get_more_posts(response, block)
while (url = next_url(response)).present?
response = api.get(url).parsed_response
stream_posts(response, block)
end
end
def posts_url
"#{id}/posts"
end
def posts_query
{
fields: options[:fields] || POST_FIELDS,
since: since_unix,
until: until_unix,
limit: options[:limit] || PER_PAGE_LIMIT,
}
end
def since_unix
options.fetch(:since) { DEFAULT_SINCE.ago }.to_i
end
def until_unix
options.fetch(:until) { Time.current }.to_i
end
def next_url(response)
response.dig('paging', 'next')
end
end
end
end