Skip to content

Instantly share code, notes, and snippets.

@gruis
Created April 7, 2016 05:47
Show Gist options
  • Save gruis/9d0a6bfdec3ecd7b7cd9a87d57d83806 to your computer and use it in GitHub Desktop.
Save gruis/9d0a6bfdec3ecd7b7cd9a87d57d83806 to your computer and use it in GitHub Desktop.
Oxfam JP Teams Summary
#!/usr/bin/env ruby
# - Retrieve data on all teams registered for Oxfam Japan trail hike
# - Print out the number of teams attempting each course
# - Print out the total fund raising target and total progress towards targets
#
# requires Faraday
# - gem install faraday
require 'json'
require 'faraday'
def format_currency(number, symbol = "¥")
symbol +
number.to_s.chars.to_a.reverse.each_slice(3).map(&:join).join(",").reverse
end
API_SERVER = 'https://oxfam-mng.com'
TEAMS = '/api/teams'
COURSES = [
"100km @06:00",
"100km @10:00",
"50km @08:00",
"50km @10:00"
]
api = Faraday.new(url: API_SERVER)
teams = JSON.parse(api.get(TEAMS).body)
courses = teams.group_by { |t| t["course_id"] }
puts "Team departure times"
courses.each do |cid, cteams|
puts " #{COURSES[cid - 1] || "Unknown"}: #{cteams.length}"
end
sums = {target: "fr_target", current: "price"}
sums.keys.each do |type|
sums[type] = teams.inject(0){ |sum, team| sum + (team[sums[type]] || 0) }
end
puts "Total fund raising: "
sums.each do |key, sum|
puts " #{key}: #{format_currency(sum)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment