class Fbwish::Wisher

Attributes

graph[RW]
matcher[RW]
replies[RW]
verbose[RW]
wish_count[RW]

Public Class Methods

new(options={}) click to toggle source
# File lib/fbwish.rb, line 9
def initialize(options={})
  required_options = [:access_token, :matcher, :replies, :wish_count]
  unspecified_options = required_options.reject{ |key| options[key] }

  unless unspecified_options.empty?
    raise ArgumentError, "Following options are required: #{unspecified_options.join(', ')}"
  end

  self.graph = Koala::Facebook::API.new(options[:access_token])
  self.matcher = options[:matcher]
  self.replies = options[:replies]
  self.wish_count = options[:wish_count]
  self.verbose = options[:verbose] || false
end

Public Instance Methods

should_log?() click to toggle source
# File lib/fbwish.rb, line 37
def should_log?
  verbose
end
wish_em_all!() click to toggle source
# File lib/fbwish.rb, line 24
def wish_em_all!
  wishes = nil
  iteration_count = (wish_count.to_f / 25).ceil

  1.upto(iteration_count) do |idx|
    wishes = wishes.next_page rescue graph.get_connections('me', 'feed')

    wishes.each do |wish|
      like_and_comment(wish)
    end
  end
end

Private Instance Methods

comment(wish, replies) click to toggle source
# File lib/fbwish.rb, line 73
def comment(wish, replies)
  # If it is an array pick random else pick itself(assuming it is a string)
  reply = replies.is_a?(Array) ?
          replies[rand(replies.length-1)] :
          replies
  reply = "#{wisher(wish)}: " + reply
  graph.put_comment(wish['id'], reply)

  return reply
end
like(wish) click to toggle source
# File lib/fbwish.rb, line 69
def like(wish)
  graph.put_like(wish['id'])
end
like_and_comment(wish) click to toggle source
# File lib/fbwish.rb, line 42
def like_and_comment(wish)
  did_reply = false

  if matcher.is_a?(Hash)
    matcher.each do |locale, regex|
      did_reply = reply_if_match_found(regex, wish, replies[locale])
      break if did_reply
    end
  else
    reply_if_match_found(matcher, wish, replies)
  end
end
log_failure(wish) click to toggle source
# File lib/fbwish.rb, line 92
def log_failure(wish)
  puts "#{wisher(wish)}'s wish '#{wish["message"]}' did not match the pattern, Hence ignored."
end
log_result(wish, reply) click to toggle source
# File lib/fbwish.rb, line 88
def log_result(wish, reply)
  puts "Liked & replied '#{reply}' to '#{wisher(wish)}'"
end
reply_if_match_found(regex, wish, replies) click to toggle source
# File lib/fbwish.rb, line 55
def reply_if_match_found(regex, wish, replies)
  if regex.match(wish['message'])
    like(wish)
    my_reply = comment(wish, replies)
    log_result(wish, my_reply) if should_log?

    true
  else
    log_failure(wish) if should_log?

    false
  end
end
wisher(wish) click to toggle source
# File lib/fbwish.rb, line 84
def wisher(wish)
  wish['from']['name']
end