Class: CommitLive::Current

Inherits:
Object
  • Object
show all
Defined in:
lib/commit-live/lesson/current.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCurrent

Returns a new instance of Current.



10
11
12
13
# File 'lib/commit-live/lesson/current.rb', line 10

def initialize()
	@netrc = CommitLive::NetrcInteractor.new()
	@sentry = CommitLive::Sentry.new()
end

Instance Attribute Details

#lessonObject

Returns the value of attribute lesson.



8
9
10
# File 'lib/commit-live/lesson/current.rb', line 8

def lesson
  @lesson
end

#netrcObject

Returns the value of attribute netrc.



8
9
10
# File 'lib/commit-live/lesson/current.rb', line 8

def netrc
  @netrc
end

#sentryObject

Returns the value of attribute sentry.



8
9
10
# File 'lib/commit-live/lesson/current.rb', line 8

def sentry
  @sentry
end

Instance Method Details

#getAttr(attr) ⇒ Object



53
54
55
56
57
# File 'lib/commit-live/lesson/current.rb', line 53

def getAttr(attr)
	if !attr.nil?
		lesson.fetch(attr)
	end
end

#getCurrentLesson(puzzle_name) ⇒ Object



15
16
17
# File 'lib/commit-live/lesson/current.rb', line 15

def getCurrentLesson(puzzle_name)
	getLesson(puzzle_name)
end

#getLesson(track_slug) ⇒ Object



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
# File 'lib/commit-live/lesson/current.rb', line 24

def getLesson(track_slug)
	begin
		Timeout::timeout(15) do
			response = CommitLive::API.new().get(
				"/v2/user/track/#{track_slug}",
				headers: { 'Authorization' => "#{token}" }
			)
			if response.status == 200
				@lesson = JSON.parse(response.body)
			elsif response.status == 404
				puts "Oops you are trying to access a non-existent lesson."
				exit
			else
				sentry.log_message("Get Lesson Failed",
					{
						'url' => "/v2/user/track/#{track_slug}",
						'response-body' => response.body,
						'response-status' => response.status
					}
				)
			end
		end
	rescue Timeout::Error
		puts "Error while getting current lesson."
		puts "Please check your internet connection."
		exit
	end
end

#getValue(key) ⇒ Object



59
60
61
62
# File 'lib/commit-live/lesson/current.rb', line 59

def getValue(key)
	lessonData = getAttr('data')
	lessonData[key]
end

#tokenObject



19
20
21
22
# File 'lib/commit-live/lesson/current.rb', line 19

def token
	netrc.read
	netrc.password
end