Created
January 3, 2015 15:58
-
-
Save guipdutra/e421ae17a329aa604332 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'koala' | |
require 'httpclient' | |
require 'json' | |
class Event | |
def initialize params | |
@id = params[:id] | |
@graph = Koala::Facebook::API.new('CAACEdEose0cBACFZAz8GA45hmXCy6s0spYUbMCHsfZAcoe6UANu5ZAYJmr7DzNZCLO8aL8rZB6gjbNRzvFg5oD9PIsjVjKK3IXDmRcaesgOm0GSqGtye87pC6fjPz7WseSgI4VU8Sids7IwQrUphHkGbZAvAPj7wO5IEOeKbyv03giFvAKYJWrvonwJOCB9WMzNW1EFVQGGEPfhEFeuuS9') | |
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 => "773544329347897") | |
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