Class: Achievement

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/naga/achievement.rb

Constant Summary collapse

SPACE =
" ".colorize(:yellow).colorize( :background => :black )
MAX_LENGTH =
70
PADDING =
SPACE * 2
PADDING_LENGTH =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, description, xp, link, comment) ⇒ Achievement

Returns a new instance of Achievement.



12
13
14
15
16
17
18
19
# File 'lib/minitest/naga/achievement.rb', line 12

def initialize(id, name, description, xp, link, comment)
  @id = id
  @name = name
  @description = description
  @xp = xp
  @link = link
  @comment = comment
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/minitest/naga/achievement.rb', line 4

def description
  @description
end

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/minitest/naga/achievement.rb', line 2

def id
  @id
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/minitest/naga/achievement.rb', line 3

def name
  @name
end

#xpObject

Returns the value of attribute xp.



5
6
7
# File 'lib/minitest/naga/achievement.rb', line 5

def xp
  @xp
end

Class Method Details

.locked_achievements(credentials) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/minitest/naga/achievement.rb', line 23

def locked_achievements(credentials)
  options = { headers: { "Content-Type" => "application/json" } }
  url = NagaApiClient.BASE_URL + "achievements?" + "authentication_token=" + credentials[:authentication_token] + "&email=" + credentials[:email]
  response = HTTParty.get(url, options)
  locked_achievements = {}
  response.parsed_response["locked_achievements"].each do |locked_achievement|
    achievement = Achievement.new(locked_achievement["id"], locked_achievement["name"], locked_achievement["description"], locked_achievement["xp"], locked_achievement["link"], locked_achievement["comment"])
    locked_achievements[achievement.name] = achievement
  end
  locked_achievements
end

Instance Method Details

#to_sObject



54
55
56
57
58
59
60
61
# File 'lib/minitest/naga/achievement.rb', line 54

def to_s
  puts @id
  puts @name
  puts @description
  puts @xp
  puts @link
  puts @comment
end

#to_s_unlockObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/minitest/naga/achievement.rb', line 63

def to_s_unlock
  puts SPACE * MAX_LENGTH
  puts PADDING + "Achievement unlocked!".ljust(MAX_LENGTH-PADDING_LENGTH).colorize(:yellow).colorize( :background => :black )
  puts PADDING + @name.ljust(MAX_LENGTH-PADDING_LENGTH).colorize(:yellow).colorize( :background => :black )
  description = "> " + @description
  puts PADDING + description.ljust(MAX_LENGTH-PADDING_LENGTH).colorize(:yellow).colorize( :background => :black )
  xp = "> " + @xp.to_s + " XP"
  if @comment && !@link
    puts PADDING + @comment.ljust(MAX_LENGTH-PADDING_LENGTH).colorize(:yellow).colorize( :background => :black )
  elsif @link && @comment
    puts PADDING + @link.ljust(MAX_LENGTH-PADDING_LENGTH).colorize(:yellow).colorize( :background => :black )
    puts PADDING + @comment.ljust(MAX_LENGTH-PADDING_LENGTH).colorize(:yellow).colorize( :background => :black )
  end
  puts PADDING + xp.ljust(MAX_LENGTH-PADDING_LENGTH).colorize(:yellow).colorize( :background => :black )
  puts SPACE * MAX_LENGTH
end

#unlock(credentials) ⇒ Object

public instance methods



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/minitest/naga/achievement.rb', line 37

def unlock(credentials)
  url = NagaApiClient.BASE_URL + "achievements/unlock"
  options = { headers: { "Content-Type" => "application/x-www-form-urlencoded" } }
  options[:body] = "email=" + credentials[:email] + "&authentication_token=" + credentials[:authentication_token] + "&achievement_id=" + self.id.to_s
  response = HTTParty.post(url, options)
  parsed_response = response.parsed_response
  if parsed_response["error"]
    STDERR.puts(("Error: " + parsed_response["error"]).colorize(:red))
  else
    self.to_s_unlock
    if parsed_response["level_up"]
      puts "You just grew a level!".colorize(:black).colorize( :background => :green )
      puts "You are now Level #{parsed_response["level"]["number"].to_s} and have #{parsed_response["level"]["xp"].to_s} XP."
    end
  end
end