module Gratitude::Client::Tips

Public Instance Methods

current_tips() click to toggle source
# File lib/gratitude/tips.rb, line 6
def current_tips
  response = faraday.get(tips_path)
  raise_any_errors_based_upon(response)
  response.body
end
current_tips_total() click to toggle source
# File lib/gratitude/tips.rb, line 12
def current_tips_total
  current_tips.inject(0) { |total, tip| total + tip["amount"].to_f }
end
update_tips(array_of_hashes_with_usernames_and_amounts) click to toggle source
# File lib/gratitude/tips.rb, line 16
def update_tips(array_of_hashes_with_usernames_and_amounts)
  post_tips_to_gratipay(array_of_hashes_with_usernames_and_amounts)
end
update_tips_and_prune(array_of_hashes_with_usernames_and_amounts) click to toggle source
# File lib/gratitude/tips.rb, line 20
def update_tips_and_prune(array_of_hashes_with_usernames_and_amounts)
  post_tips_to_gratipay(
    array_of_hashes_with_usernames_and_amounts, prune: true
  )
end

Private Instance Methods

faraday_post_response(array_of_hashes, options = {}) click to toggle source
# File lib/gratitude/tips.rb, line 34
def faraday_post_response(array_of_hashes, options = {})
  faraday.post do |request|
    request.url(tips_path)
    request.body = prepared_tips_array(array_of_hashes).to_json
    request.params = { also_prune: "true" } if options[:prune] == true
  end
end
post_tips_to_gratipay(array_of_hashes, options = {}) click to toggle source
# File lib/gratitude/tips.rb, line 28
def post_tips_to_gratipay(array_of_hashes, options = {})
  response = faraday_post_response(array_of_hashes, options)
  raise_any_errors_based_upon(response)
  response.body
end
prepared_tips_array(array_of_hashes) click to toggle source
# File lib/gratitude/tips.rb, line 46
def prepared_tips_array(array_of_hashes)
  array_of_hashes.each_with_object([]) do |hash, array|
    username = hash[:username] || hash["username"]
    amount = hash[:amount] || hash["amount"]
    array << tip_hash_based_upon(username, amount)
  end
end
raise_any_errors_based_upon(response) click to toggle source
# File lib/gratitude/tips.rb, line 62
def raise_any_errors_based_upon(response)
  if response.body.is_a?(Hash) && response.body.key?("error_code")
    raise AuthenticationError
  elsif usernames_with_errors(response.body).size > 0
    raise TipUpdateError, usernames_with_errors(response.body)
  end
end
tip_hash_based_upon(username, amount) click to toggle source
# File lib/gratitude/tips.rb, line 54
def tip_hash_based_upon(username, amount)
  {
    "amount" => "#{amount}",
    "platform" => "gratipay",
    "username" => "#{username}"
  }
end
tips_path() click to toggle source
# File lib/gratitude/tips.rb, line 42
def tips_path
  "/#{username}/tips.json"
end
usernames_with_errors(response_body) click to toggle source
# File lib/gratitude/tips.rb, line 70
def usernames_with_errors(response_body)
  response_body.each_with_object([]) do |user_tip_response, array|
    if user_tip_response.key?("error")
      array << user_tip_response["username"]
    end
  end
end