class FBScrape::Post
Attributes
comments[RW]
created_at[RW]
id[RW]
link[RW]
message[RW]
Public Class Methods
load_from_id(id, access_token, page_id=nil)
click to toggle source
# File lib/fb_scrape/post.rb, line 17 def self.load_from_id id, access_token, page_id=nil post = FBScrape::Post.new({ 'id' => id }, page_id, access_token) post.load_comments post end
new(payload, page_id=nil, token=nil)
click to toggle source
# File lib/fb_scrape/post.rb, line 7 def initialize payload, page_id=nil, token=nil @comments = [] @page_id = page_id @token = token if payload load_from_payload(payload) end end
Public Instance Methods
has_more_comments?()
click to toggle source
# File lib/fb_scrape/post.rb, line 28 def has_more_comments? @page_info && next_cursor end
load_all_comments(limit=nil)
click to toggle source
# File lib/fb_scrape/post.rb, line 32 def load_all_comments(limit=nil) while has_more_comments? && is_below_limit?(limit) do load_more_comments end end
load_comments()
click to toggle source
# File lib/fb_scrape/post.rb, line 23 def load_comments url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/comments?access_token=#{@token}" load_from_url url end
to_json(*args)
click to toggle source
# File lib/fb_scrape/post.rb, line 40 def to_json(*args) JSON.pretty_generate({ id: @id, created_at: @created_at, message: @message, link: @link }) end
Private Instance Methods
is_below_limit?(limit)
click to toggle source
# File lib/fb_scrape/post.rb, line 52 def is_below_limit?(limit) is_below_limit = limit.nil? || @comments.length < limit end
load_from_payload(payload)
click to toggle source
# File lib/fb_scrape/post.rb, line 80 def load_from_payload payload @id = payload["id"] @created_at = payload["created_time"] @message = payload["message"] @link = payload["link"] end
load_from_url(url)
click to toggle source
# File lib/fb_scrape/post.rb, line 56 def load_from_url url resp = HTTParty.get(url) case resp.code when 200 response = JSON.parse(resp.body) @comments = @comments.concat(response["data"].collect{ |c| FBScrape::Comment.new(c, @token, @page_id) }) @page_info = response["paging"] when 400 response = JSON.parse(resp.body) error = response["error"]["message"] raise ArgumentError.new(error) end end
load_more_comments()
click to toggle source
# File lib/fb_scrape/post.rb, line 70 def load_more_comments url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/comments?access_token=#{@token}&limit=15&after=#{next_cursor}" load_from_url url end
next_cursor()
click to toggle source
# File lib/fb_scrape/post.rb, line 76 def next_cursor @page_info["cursors"]["after"] end