Skip to content

Instantly share code, notes, and snippets.

@guipdutra
Last active August 29, 2015 14:11
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/0e0e79f7013841e4ae51 to your computer and use it in GitHub Desktop.
Save guipdutra/0e0e79f7013841e4ae51 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('CAACEdEose0cBANQRADTYIXfbGoQaL7Fv0dheFWAxBsiemgghsACX7qkfVnvRLNGwrF0NTNxUPZBrxGXtgVZCF67yEGPiwZCgIL4cWNEY9o5Itr8cIzqTbOnyM7pBUwiFzUj5Iq62ImXPVRZAEgXo39Rvy2U4JUA3q7RNofm6DRIo9QDmJ1eFc0JqnoV2YYmN9ZBjU26ruXrBUqi9EAkoH')
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
end
def genders
@genders ||= JSON.parse(@http_client.get("http://api.genderize.io?#{query}&country_id=br").content)
end
private
def query
i = 0
query = []
@people_going.each do |name|
query << "name[#{i}]=#{name.downcase}"
i = i + 1
end
query.join("&")
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 => "635989689843436")
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