Class: Item

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/game/item.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, description, level, colour) ⇒ Item

Returns a new instance of Item.



8
9
10
11
12
13
14
# File 'lib/minitest/game/item.rb', line 8

def initialize(id, name, description, level, colour)
  @id = id
  @name = name
  @description = description
  @level = level
  @colour = colour
end

Instance Attribute Details

#colourObject

Returns the value of attribute colour.



6
7
8
# File 'lib/minitest/game/item.rb', line 6

def colour
  @colour
end

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/minitest/game/item.rb', line 4

def description
  @description
end

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/minitest/game/item.rb', line 2

def id
  @id
end

#levelObject

Returns the value of attribute level.



5
6
7
# File 'lib/minitest/game/item.rb', line 5

def level
  @level
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/minitest/game/item.rb', line 3

def name
  @name
end

Class Method Details

.all(credentials) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/minitest/game/item.rb', line 18

def all(credentials)
  options = { headers: { "Content-Type" => "application/json" } }
  url = NagaApiClient.BASE_URL + "items?" + "authentication_token=" + credentials[:authentication_token] + "&email=" + credentials[:email]
  response = HTTParty.get(url, options)
  items = {}
  response.parsed_response["items"].each do |i|
    item = Item.new(i["id"], i["name"], i["description"])
    items[item.name] = item
  end
  items
end

.drop(credentials) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/minitest/game/item.rb', line 38

def drop(credentials)
  options = { headers: { "Content-Type" => "application/json" } }
  url = NagaApiClient.BASE_URL + "items/drop/?authentication_token=" + credentials[:authentication_token] + "&email=" + credentials[:email]
  response = HTTParty.get(url, options)
  i = response.parsed_response["item"]
  if i
    item = Item.new(i["id"], i["name"], i["description"], i["level"], i["colour"])
  else
    item = nil
  end
end

.get(name, credentials) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/minitest/game/item.rb', line 30

def get(name, credentials)
  options = { headers: { "Content-Type" => "application/json" } }
  url = NagaApiClient.BASE_URL + "items/show/" + URI::encode(name) + "?authentication_token=" + credentials[:authentication_token] + "&email=" + credentials[:email]
  response = HTTParty.get(url, options)
  i = response.parsed_response["item"]
  item = Item.new(i["id"], i["name"], i["description"], i["level"], i["colour"])
end

.starter(credentials) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/minitest/game/item.rb', line 50

def starter(credentials)
  options = { headers: { "Content-Type" => "application/json" } }
  url = NagaApiClient.BASE_URL + "items/starter?" + "authentication_token=" + credentials[:authentication_token] + "&email=" + credentials[:email]
  response = HTTParty.get(url, options)
  starter_items = {}
  response.parsed_response["starter_items"].each do |i|
    item = Item.new(i["id"], i["name"], i["description"])
    starter_items[item.name] = item
  end
  starter_items
end

Instance Method Details

#to_sObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/minitest/game/item.rb', line 63

def to_s
  if self
    colorized_name = self.name.upcase
    case self.level
      when 1
        colorized_name = colorized_name.colorize(:white)
      when 2
        colorized_name = colorized_name.colorize(:green)
      when 3
        colorized_name = colorized_name.colorize(:blue)
      when 4
        colorized_name = colorized_name.colorize(:light_white)
      when 5
        colorized_name = colorized_name.colorize(:yellow)
    end
    say("You found a #{colorized_name}!")
    say("It has a description: " + self.description + " Level: " + self.level.to_s)
  end
end