Skip to content

Instantly share code, notes, and snippets.

@guipdutra
Created January 17, 2015 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guipdutra/e3b423ec24801da222d6 to your computer and use it in GitHub Desktop.
Save guipdutra/e3b423ec24801da222d6 to your computer and use it in GitHub Desktop.
require 'koala'
require 'httpclient'
require 'json'
class Event
def initialize params
@id = params[:id]
@graph = Koala::Facebook::API.new('CAACEdEose0cBAJGfwNoDHG676ZCvkxX08jcVqYxVqjZABJEL2hoNvSgWQIZCmizoRGD65uWxek6KGzLpGmuZBvGGXwtIXHTSlZC6EUdqLfgYcMiBY75z2WbFvvJR09LT0EEg03CVECU83kbQiLcG3yTCZBFk3JBg1cUCfl8BcbZAcHekN7Q9SmJkH2EtztQZBw6XTEG4kQkd27Klrz7FbAxT')
end
def people_going
attending.map {|attend| attend["name"].split.first }
end
def total_attending
people_going.size
end
private
def attending
@graph.get_object("#{@id}/attending")
end
end
class Genderize
def initialize params
@people_going = params[:people_going]
@http_client = params[:http_client] || HTTPClient.new
@queries = mount_queries
end
def genders
@genders ||= mount_genders
end
private
def mount_genders
group_genders = []
@queries.each_slice(200).to_a.each do |query|
group_genders << JSON.parse(@http_client.get("http://api.genderize.io?#{query.join("&")}").content)
end
group_genders.flatten
end
def mount_queries
i = 0
queries = []
@people_going.each do |name|
queries << "name[#{i}]=#{name.downcase}"
i = i + 1
end
queries
end
end
class GenderCount
def initialize params
@genderize = params[:genderize]
end
def female_count
count_by_gender "female"
end
def male_count
count_by_gender "male"
end
def gender_ratio
female_count.to_f/male_count
end
private
def count_by_gender gender
@genderize.genders.select {|person| person["gender"] == gender}.count
end
end
event = Event.new(:id => "727076790721312")
genderize = Genderize.new(:people_going => event.people_going)
gender_count = GenderCount.new(:genderize => genderize)
puts "female: #{gender_count.female_count.to_s}"
puts "male: #{gender_count.male_count.to_s}"
puts "ratio: #{gender_count.gender_ratio}"
puts "total: #{event.total_attending.to_s}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment