Created
October 21, 2015 18:11
-
-
Save asterite/85d72c7d24c929ac09e0 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 "http/client" | |
require "json" | |
module Bountysource | |
class API | |
def initialize | |
@client = HTTP::Client.new "www.bountysource.com", ssl: true | |
end | |
def timeline(team_id, page = 1, per_page = 30) : Array(Event) | |
response = @client.get "/timeline?per_page=#{per_page}&page=#{page}&team_id=#{team_id}", HTTP::Headers{"Accept": "application/vnd.bountysource+json; version=2"} | |
Array(Event).from_json(response.body) | |
end | |
def all_timeline(team_id) | |
events = [] of Event | |
page = 1 | |
loop do | |
page_events = timeline 89730, page: page, per_page: 100 | |
break if page_events.empty? | |
events.concat page_events | |
page += 1 | |
end | |
events | |
end | |
end | |
class Event | |
enum Kind | |
PledgeCreated | |
SupportLevelCreated | |
TeamPayinCreated | |
def self.new(pull : JSON::PullParser) | |
parse(pull.read_string.camelcase) | |
end | |
end | |
JSON.mapping({ | |
event: Kind, | |
timestamp: {type: Time, converter: Time::Format.new("%F")}, | |
support_level: {type: Amount, nilable: true}, | |
team_payin: {type: Amount, nilable: true}, | |
pledge: {type: Amount, nilable: true}, | |
actor: Actor, | |
}) | |
def recurring? | |
!!support_level | |
end | |
def amount | |
(support_level || team_payin || pledge).try(&.amount) || 0.0 | |
end | |
end | |
class Actor | |
JSON.mapping({ | |
type: String, | |
slug: {type: String, nilable: true}, | |
display_name: String, | |
}) | |
end | |
struct Amount | |
JSON.mapping({ | |
amount: Float64, | |
}) | |
end | |
end | |
api = Bountysource::API.new | |
events = api.all_timeline 89730 | |
puts "Total: $#{events.sum &.amount}" | |
groups = events.group_by &.timestamp.to_s("%b") | |
groups.each do |group, events| | |
recurring_events, non_recurring_events = events.partition(&.recurring?).map(&.sum(&.amount)) | |
puts "#{group}: recurring: $#{recurring_events}, non-recurring: $#{non_recurring_events}" | |
end | |
# last = Time.new(1015, 8, 21) | |
# events.each do |event| | |
# if event.recurring? && (event.timestamp > last) && event.actor.slug | |
# puts "#{event.timestamp.month}: https://www.bountysource.com/people/#{event.actor.slug} $#{event.amount}" | |
# | |
# end | |
# end | |
# # puts events.sum &.amount |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment