class Gratitude::UserChart

Constants

USER_CHARTS

Attributes

date[R]
npatrons[R]
number_of_patrons[R]
receipts[R]
username[R]

Public Class Methods

all_for_user(username) click to toggle source
# File lib/gratitude/user_chart.rb, line 19
def self.all_for_user(username)
  USER_CHARTS.clear if user_charts_contains_other_usernames?(username)
  collect_charts_for(username) if USER_CHARTS.empty?
  USER_CHARTS
end
new(options = {}) click to toggle source
# File lib/gratitude/user_chart.rb, line 11
def initialize(options = {})
  @username = options["username"]
  @date = Date.parse(options["date"]) if options["date"]
  @number_of_patrons = options["npatrons"]
  @receipts = options["receipts"]
  USER_CHARTS << self
end
newest_for(username) click to toggle source
# File lib/gratitude/user_chart.rb, line 25
def self.newest_for(username)
  sort_by_date_for_user(username).first
end
oldest_for(username) click to toggle source
# File lib/gratitude/user_chart.rb, line 29
def self.oldest_for(username)
  sort_by_date_for_user(username).last
end

Private Class Methods

collect_charts_for(username) click to toggle source
# File lib/gratitude/user_chart.rb, line 50
def self.collect_charts_for(username)
  user_charts_from_gratipay(username).each do |chart_hash|
    UserChart.new(chart_hash.merge("username" => username))
  end
end
sort_by_date_for_user(username) click to toggle source
# File lib/gratitude/user_chart.rb, line 33
def self.sort_by_date_for_user(username)
  all_for_user(username).sort_by(&:date).reverse
end
user_charts_contains_other_usernames?(username) click to toggle source
# File lib/gratitude/user_chart.rb, line 38
def self.user_charts_contains_other_usernames?(username)
  USER_CHARTS.collect(&:username).uniq != [username]
end
user_charts_from_gratipay(username) click to toggle source
# File lib/gratitude/user_chart.rb, line 43
def self.user_charts_from_gratipay(username)
  response = faraday.get("/#{username}/charts.json").body.to_a
  raise UsernameNotFoundError, username if response.empty?
  response
end