class FBScrape::Client

Attributes

conversations[RW]
id[RW]
name[RW]
page_name[RW]
posts[RW]

Public Class Methods

new(page_name, token_secret, id=nil, limit=nil, load_on_init=true) click to toggle source
# File lib/fb_scrape/client.rb, line 7
def initialize(page_name, token_secret, id=nil, limit=nil, load_on_init=true)
  @page_name = page_name
  @token_secret = token_secret
  @id = id
  @posts = []
  @conversations = []
  @loaded_initial = false
  @loaded_initial_conversations = false
  @limit = limit
  @conversations_page_info = nil
  if @id && load_on_init
    load_initial_posts
  elsif !@id
    get_page_id
  end
end

Public Instance Methods

can_load_more_conversations?() click to toggle source
# File lib/fb_scrape/client.rb, line 46
def can_load_more_conversations?
  !is_limited? || (@conversations.count < @limit.to_i && has_more_conversations?)
end
has_more_conversations?() click to toggle source
# File lib/fb_scrape/client.rb, line 50
def has_more_conversations?
  @conversations_page_info && next_conversation_cursor
end
is_limited?() click to toggle source
# File lib/fb_scrape/client.rb, line 38
def is_limited?
  !@limit.nil?
end
is_under_limit?() click to toggle source
# File lib/fb_scrape/client.rb, line 34
def is_under_limit?
  !is_limited? || @posts.count < @limit.to_i
end
load(limit=nil) click to toggle source
# File lib/fb_scrape/client.rb, line 24
def load(limit=nil)
  load_initial_posts
  @limit = limit if limit != @limit

  while has_more_posts? && is_under_limit? do
    # load more posts
    load_more_posts
  end
end
load_conversations(limit=nil) click to toggle source
# File lib/fb_scrape/client.rb, line 54
def load_conversations(limit=nil)
  @limit = limit if limit != @limit
  load_initial_conversations

  while has_more_conversations? && can_load_more_conversations? do
    load_more_conversations
  end
end

Private Instance Methods

get_page_id() click to toggle source
# File lib/fb_scrape/client.rb, line 134
def get_page_id
  url = "https://graph.facebook.com/#{@page_name}?access_token=#{@token_secret}"
  resp = HTTParty.get(url)

  case resp.code
    when 200
      response = JSON.parse(resp.body)
      @name = response["name"]
      @id = response["id"]
    when 400
      handle_error(resp)
  end
end
handle_error(resp) click to toggle source
# File lib/fb_scrape/client.rb, line 120
def handle_error resp
  response = JSON.parse(resp.body)
  error = response["error"]["message"]
  raise ArgumentError.new(error)
end
load_conversations_from_url(url) click to toggle source
# File lib/fb_scrape/client.rb, line 94
def load_conversations_from_url url
  resp = HTTParty.get(url)
  case resp.code
    when 200
      response = JSON.parse(resp.body)
      response['data'].collect { |c| FBScrape::Conversation.new(c['id'], @id, @token_secret, false) }
      @conversations = @conversations.concat(response['data'].collect { |c| FBScrape::Conversation.new(c['id'], @id, @token_secret, false) })
      @conversations_page_info = response["paging"]
    when 400
      handle_error(resp)
  end
end
load_initial_conversations() click to toggle source
# File lib/fb_scrape/client.rb, line 65
def load_initial_conversations
  if !@loaded_initial_conversations
    url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/conversations?access_token=#{@token_secret}"
    if is_limited?
      url += "&limit=#{@limit}"
    end
    load_conversations_from_url url
    @loaded_initial_conversations = true
  end
end
load_initial_posts() click to toggle source
# File lib/fb_scrape/client.rb, line 81
def load_initial_posts
  if !@loaded_initial
    url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/posts?fields=link,message,created_time&access_token=#{@token_secret}"
    load_posts_from_url url
    @loaded_initial = true
  end
end
load_more_conversations() click to toggle source
# File lib/fb_scrape/client.rb, line 76
def load_more_conversations
  url = "https://graph.facebook.com/v#{FBScrape::GRAPH_VERSION}/#{@id}/conversations?access_token=#{@token_secret}&limit=25&after=#{next_conversation_cursor}"
  load_conversations_from_url url
end
load_posts_from_url(url) click to toggle source
# File lib/fb_scrape/client.rb, line 107
def load_posts_from_url url
  resp = HTTParty.get(url)
  case resp.code
    when 200
      response = JSON.parse(resp.body)
      more_posts = response["data"].collect { |p| FBScrape::Post.new(p) }
      @posts = @posts.concat(more_posts)
      @page_info = response["paging"]
    when 400
      handle_error(resp)
  end
end
next_conversation_cursor() click to toggle source
# File lib/fb_scrape/client.rb, line 130
def next_conversation_cursor
  @conversations_page_info["cursors"]["after"]
end
next_cursor() click to toggle source
# File lib/fb_scrape/client.rb, line 126
def next_cursor
  @page_info["cursors"]["after"]
end