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



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

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

#getCurrentLesson(puzzle_name) ⇒ Object



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

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/commit-live/lesson/current.rb', line 28

def getLesson(url)
  begin
    Timeout::timeout(15) do
      response = CommitLive::API.new().get(
        url,
        headers: { 'access-token' => "#{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' => url,
            '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



63
64
65
66
67
68
69
70
# File 'lib/commit-live/lesson/current.rb', line 63

def getValue(key)
  lessonData = getAttr('data')
  if !lessonData['current_track'].nil?
    lessonData['current_track'][key]
  else
    lessonData[key]
  end
end

#tokenObject



23
24
25
26
# File 'lib/commit-live/lesson/current.rb', line 23

def token
  netrc.read
  netrc.password
end