forked from tuandnacv/challenge_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInformation.rb
79 lines (64 loc) · 1.63 KB
/
Information.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# please install gem 'httparty', 'terminal-table' for show output
require 'rubygems'
require 'httparty'
require 'terminal-table'
class General
include HTTParty
base_uri 'https://sample-accounts-api.herokuapp.com'
def initialize(id)
@id = id
end
def get_data_from_api_user
self.class.get('/users/' + @id.to_s)
end
def user
@user ||= get_data_from_api_user.parsed_response['attributes']
end
end
class User < General
def get_data_from_api_accounts
self.class.get('/users/' + @id.to_s + '/accounts')
end
def list_accounts
@list_accounts ||= get_data_from_api_accounts.parsed_response
end
def information_accounts
list_accounts.each_with_object([]) do |account, array|
array << [account['attributes']['id'], account['attributes']['name'], account['attributes']['balance']]
end
end
def information_user
[[user['id'], user['name']]]
end
def output_information_user
puts Terminal::Table.new title: 'Information User', headings: ['ID', 'Name'], rows: information_user
puts Terminal::Table.new title: 'Information Accounts of User', headings: ['ID', 'Name', 'Balance'], rows: information_accounts
end
end
def puts_question
puts 'Please Input Number ID from Keyboard or Enter for exit'
end
def not_found_user
puts 'Cannot found User with id'
puts_question
get_user
end
def get_user
id = gets.chomp
if id.to_i > 0
begin
new_user = User.new(id)
new_user.output_information_user
puts_question
get_user
rescue
not_found_user
end
elsif id == ''
puts 'You were exit'
else
not_found_user
end
end
puts_question
get_user