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.



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

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

Instance Attribute Details

#lessonObject

Returns the value of attribute lesson.



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

def lesson
  @lesson
end

#netrcObject

Returns the value of attribute netrc.



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

def netrc
  @netrc
end

Instance Method Details

#getAttr(attr) ⇒ Object



44
45
46
47
48
# File 'lib/commit-live/lesson/current.rb', line 44

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

#getCurrentLesson(puzzle_name) ⇒ Object



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

def getCurrentLesson(puzzle_name)
	url = '/v1/current_track'
	if !puzzle_name.empty?
		url = "/v1/user/track/#{puzzle_name}"
	end
	getLesson(url)
end

#getLesson(url) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/commit-live/lesson/current.rb', line 21

def getLesson(url)
	begin
		Timeout::timeout(15) do
			netrc.read
			token = netrc.password
			response = CommitLive::API.new().get(
				url,
				headers: { 'access-token' => "#{token}" }
			)
			if response.status == 200
				@lesson = JSON.parse(response.body)
			else
				puts "Something went wrong. Please try again."
				exit 1
			end
		end
	rescue Timeout::Error
		puts "Error while getting current lesson."
		puts "Please check your internet connection."
		exit
	end
end