Class: View

Inherits:
Object
  • Object
show all
Defined in:
lib/View.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeView

Returns a new instance of View.



11
12
13
14
# File 'lib/View.rb', line 11

def initialize
  @prompt = TTY::Prompt.new
  @pastel = Pastel.new
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



9
10
11
# File 'lib/View.rb', line 9

def input
  @input
end

Instance Method Details

#display_searchObject



86
87
88
89
90
91
92
93
94
# File 'lib/View.rb', line 86

def display_search
  line
  search = @prompt.ask('Whats the name of the country?') do |q|
    q.required true
    q.modify  :strip
    q.modify  :capitalize
  end
  search
end

#display_start_menuObject



16
17
18
19
20
21
22
# File 'lib/View.rb', line 16

def display_start_menu
  font = TTY::Font.new(:doom)
  puts font.write('Traveler')
  line
  @input = @prompt.select('What would you like to do?', ['Login', 'Sign up', 'Search for a country', 'Close'])
  @input
end

#display_user_menuObject



24
25
26
27
28
# File 'lib/View.rb', line 24

def display_user_menu
  line
  @input = @prompt.select('What would you like to do?', ['Add a travel entry', 'Search for a country', 'View my entries', 'Show me my statistics', 'Log out'])
  @input
end

#displayloginObject



100
101
102
103
104
# File 'lib/View.rb', line 100

def displaylogin
  line
  username = @prompt.ask('Username:', default: ENV['USER'])
  username
end

#displaypwObject



106
107
108
109
110
# File 'lib/View.rb', line 106

def displaypw
  line
  password = @prompt.mask('password:')
  password
end

#get_date_input(message) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/View.rb', line 138

def get_date_input(message)
  validator = lambda { |str|
    begin
      Date.parse(str)
      return str
    rescue ArgumentError
      return nil
    end
  }
  @prompt.ask(message, convert: :date) do |q|
    q.validate(validator, 'Invalid Date - Please enter in DD-MM-YYYY form')
  end
end

#get_input(message) ⇒ Object



112
113
114
115
116
117
# File 'lib/View.rb', line 112

def get_input(message)
  line
  puts message
  input = gets.strip
  input
end

#lineObject



96
97
98
# File 'lib/View.rb', line 96

def line
  puts '-' * 50
end

#promptCountry(hash) ⇒ Object



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
79
80
81
82
83
84
# File 'lib/View.rb', line 30

def promptCountry(hash)
  line
  if hash
    font = TTY::Font.new(:doom)
    puts font.write(hash['name']['common'])
    line
    puts @pastel.bold("Officially known as: #{hash['name']['official']}")
    line
    puts @pastel.bold("Name in countrys\' native language:")
    lang_code = hash['name']['native'].keys[0]
    print @pastel.bold('Official name: ')
    puts (hash['name']['native'][lang_code]['official']).to_s
    print @pastel.bold('Common name: ')
    puts (hash['name']['native'][lang_code]['official']).to_s

    line

    puts @pastel.bold.underline.on_yellow('General Information')
    general_table = TTY::Table.new header: [@pastel.bold('Language'), @pastel.bold('Web Suffix'), @pastel.bold('International Dialing'), @pastel.bold('Independence')], rows: [[hash['languages'][lang_code], hash['tld'][0], hash['idd'].values[0] + hash['idd'].values[1][0].to_s, hash['independent'].to_s]]
    puts general_table.render(:unicode)

    line

    puts @pastel.bold.underline.on_green('Currency')
    currency_holder = hash['currencies']
    currency_code = hash['currencies'].keys[0]
    currency_table = TTY::Table.new header: [@pastel.bold('Name'), @pastel.bold('Symbol'), @pastel.bold('Code')], rows: [[currency_holder[currency_code]['name'], currency_holder[currency_code]['symbol'], currency_code]]
    puts currency_table.render(:unicode)

    line

    puts @pastel.bold.underline.on_blue('Geography')
    geography_table = TTY::Table.new header: [@pastel.bold('Capital'), @pastel.bold('Region'), @pastel.bold('Sub-region'), @pastel.bold('Landlocked')], rows: [[hash['capital'][0], hash['region'], hash['subregion'], hash['landlocked']]]
    puts geography_table.render(:unicode)

    line

    puts @pastel.bold.underline.on_red('Name of Country in Different Languages')
    col = []
    hash['translations'].each do |v|
      row = []
      row << v[0]
      row << v[1]['official']
      row << v[1]['common']
      col << row
    end
    translation_table = TTY::Table.new([@pastel.bold('Language'), @pastel.bold('Official name'), @pastel.bold('Common name')], col)
    puts translation_table.render(:unicode)

    line

  else
    puts 'No country by that name'
  end
end

#show_entries(arr) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/View.rb', line 119

def show_entries(arr)
  line
  col = []
  arr.each do |v|
    row = []
    row << v['date']
    row << v['country']
    row << v['region']
    col << row
  end
  entry_table = TTY::Table.new(%w[Date Country Region], col)
  puts entry_table.render(:unicode)
end

#show_stat(num1, num2, region) ⇒ Object



133
134
135
136
# File 'lib/View.rb', line 133

def show_stat(num1, num2, region)
  line
  puts "You have been in #{num1} out of #{num2} countries in #{region}"
end