Created
February 10, 2017 22:58
-
-
Save israelb/8b65d665e91eff8a0307facb4f58e1e0 to your computer and use it in GitHub Desktop.
Regalli Test: Calculate the average bill amount
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
module Regalli | |
class Account | |
require 'json' | |
require "base64" | |
require 'net/http' | |
def initialize | |
@data = JSON.parse(response) | |
end | |
def send_average | |
puts "average #{average}" | |
uri = URI.parse("https://deudas.herokuapp.com/") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
jobj = {"average" => average.to_s} | |
response = http.put("/accounts/#{@data['uuid']}", JSON.dump(jobj), {"Content-Type" => "application/json"}) | |
puts "Response #{response.code} #{response.message}: #{response.body}" | |
end | |
def create_account | |
uri = URI('https://deudas.herokuapp.com/accounts') | |
response = Net::HTTP.post_form(uri, 'name' => 'israel', 'email' => '[email protected]') | |
puts "Response #{response.code} #{response.message}: #{response.body}" | |
# return a json response... | |
end | |
private | |
def total_amount_decoded | |
@data['bills'].inject(0){|sum,bill| sum + Base64.decode64(bill['amount']).to_f } | |
end | |
def average | |
total_amount_decoded / @data['bills'].size | |
end | |
# json response, harcoded | |
def response | |
'{ | |
"type": "account", | |
"uuid": "55d07c93-7503-4853-87a9-f7d6d99c4da7", | |
"bills": [ | |
{ | |
"type": "bill", | |
"statement_date": "2016-10-01", | |
"amount": "MTUuMA==\n" | |
}, | |
{ | |
"type": "bill", | |
"statement_date": "2016-11-01", | |
"amount": "MjAuNQ==\n" | |
}, | |
{ | |
"type": "bill", | |
"statement_date": "2016-12-01", | |
"amount": "MTAwLjA=\n" | |
}, | |
{ | |
"type": "bill", | |
"statement_date": "2017-01-01", | |
"amount": "MTAuMjU=\n" | |
} | |
] | |
}' | |
end | |
end | |
end | |
# Use our Module | |
r = Regalli::Account.new | |
# r.create_account | |
r.send_average |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment