Files
strike_me-api/lib/strike_me/api/account.rb
T

55 lines
1.4 KiB
Ruby

# frozen_string_literal: true
module StrikeMe
module API
# Factory helper object connecting the client to the models
class Account
include BaseClient
autoload_default :Profile
autoload_default :Balances
autoload_default :Balance
def profile
return @profile if @profile.present?
url = profile_url
response = client.get url
#body = response.body.presence
#body = JSON.parse(body) if body.is_a? String
if response.success?
@profile = Profile.new response.body.merge(account: self)
else
# if you see this, something went wrong, this is test code
raise ResponseError.new(url, response.body)
end
end
def profile_url
if (account_id = ENV['STRIKE_ACCOUNT_ID'].presence)
"accounts/#{account_id}/profile"
else
account_handle = ENV.fetch('STRIKE_ACCOUNT_HANDLE')
"accounts/handles/#{account_handle}/profile"
end
end
def balances
return @balances if @balances.present?
response = client.get 'balances'
if response.success?
@balances = Balances.new self, response.body
else
raise ResponseError.new('balances', response.body)
end
end
def inspect
"#<StrikeMe::API::Account profile: #{@profile}, balances: #{@balances}>"
end
end
end
end