Class: Pokemon

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

Constant Summary collapse

@@terminal_width =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pokemon_input) ⇒ Pokemon

Returns a new instance of Pokemon.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pokemon_lookup.rb', line 8

def initialize(pokemon_input)
    sanitized_input = pokemon_input.to_s.downcase
    response = HTTP.get("https://pokeapi.co/api/v2/pokemon/#{ sanitized_input }")
    @raw_data = response.parse

    @id = @raw_data["id"]
    @name = @raw_data["name"].capitalize
    @weight = @raw_data["weight"]
    @types = format_types
    @abilities = format_abilities
    @moves = format_moves
    @stats = format_stats

end

Instance Attribute Details

#abilitiesObject (readonly)

Returns the value of attribute abilities.



6
7
8
# File 'lib/pokemon_lookup.rb', line 6

def abilities
  @abilities
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/pokemon_lookup.rb', line 6

def id
  @id
end

#movesObject (readonly)

Returns the value of attribute moves.



6
7
8
# File 'lib/pokemon_lookup.rb', line 6

def moves
  @moves
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/pokemon_lookup.rb', line 6

def name
  @name
end

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



6
7
8
# File 'lib/pokemon_lookup.rb', line 6

def raw_data
  @raw_data
end

#statsObject (readonly)

Returns the value of attribute stats.



6
7
8
# File 'lib/pokemon_lookup.rb', line 6

def stats
  @stats
end

#typesObject (readonly)

Returns the value of attribute types.



6
7
8
# File 'lib/pokemon_lookup.rb', line 6

def types
  @types
end

#weightObject (readonly)

Returns the value of attribute weight.



6
7
8
# File 'lib/pokemon_lookup.rb', line 6

def weight
  @weight
end

Class Method Details

.catch(pokemon_input) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/pokemon_lookup.rb', line 23

def self.catch(pokemon_input)
  begin
    new(pokemon_input)
  rescue HTTP::Error
    puts "Unkown Pokemon, try catching a different Pokemon"
    nil
  end
end


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pokemon_lookup.rb', line 93

def self.print_list(page=1, amount=20)
  offset = (page.to_i - 1) * amount.to_i
  response = HTTP.get("https://pokeapi.co/api/v2/pokemon?offset=#{ offset }&limit=#{ amount }")
  pokemon_hashes = response.parse["results"]

  pokemon_hashes.each do |pokemon_hash|
    number = pokemon_hash["url"].split("/")[-1]
    name = pokemon_hash["name"]
    puts "  #{ number.rjust(4) } - #{ name }"
  end
  nil
end

.terminal_widthObject



85
86
87
# File 'lib/pokemon_lookup.rb', line 85

def self.terminal_width
  @@terminal_width
end

.terminal_width=(new_terminal_width) ⇒ Object



89
90
91
# File 'lib/pokemon_lookup.rb', line 89

def self.terminal_width=(new_terminal_width)
  @@terminal_width = new_terminal_width
end

Instance Method Details



38
39
40
41
42
# File 'lib/pokemon_lookup.rb', line 38

def print_abilities
  puts "Abilities"
  puts "=" * @@terminal_width
  abilities.each { |ability| puts "#{ ability }" }
end


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pokemon_lookup.rb', line 65

def print_info
  puts
  puts "Name: #{name}"
  puts "Weighs: #{weight}"

  puts 
  print_types

  puts
  print_abilities

  puts 
  print_moves

  puts 
  print_stats

  puts
end


44
45
46
47
48
49
50
51
52
53
# File 'lib/pokemon_lookup.rb', line 44

def print_moves
  column_size = 20

  puts "Moves"
  puts "=" * @@terminal_width

  moves.each_slice(@@terminal_width / column_size ) do |move_slice| 
    puts move_slice.map { |move| "#{ move }".ljust(column_size) }.join
  end
end


55
56
57
58
59
60
61
62
63
# File 'lib/pokemon_lookup.rb', line 55

def print_stats
  puts "Stats"
  puts "=" * @@terminal_width
  stats.each do |stat| 
    puts "#{ stat[:name] }"
    puts "      - Effort: #{ stat[:effort] }"
    puts "      - Base Stat: #{ stat[:base_stat] }"
  end
end


32
33
34
35
36
# File 'lib/pokemon_lookup.rb', line 32

def print_types
  puts "Types"
  puts "=" * @@terminal_width
  types.each { |type| puts "#{ type }" }
end