Class: Tutorial

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/nexmo_developer/app/models/tutorial.rb

Defined Under Namespace

Classes: FileLoader, Metadata, Prerequisite, Task

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, current_step:, current_product: nil, code_language: nil) ⇒ Tutorial

Returns a new instance of Tutorial.



9
10
11
12
13
14
15
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 9

def initialize(name:, current_step:, current_product: nil, code_language: nil)
  @name         = name
  @current_step = current_step
  @product      = current_product
  @language     = code_language
  @file_loader  = load_file!
end

Instance Attribute Details

#current_stepObject (readonly)

Returns the value of attribute current_step.



4
5
6
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 4

def current_step
  @current_step
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 4

def name
  @name
end

Class Method Details

.load(name, current_step, current_product = nil, code_language = nil) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 163

def self.load(name, current_step, current_product = nil, code_language = nil)
  new(
    name: name,
    current_step: current_step,
    current_product: current_product,
    code_language: code_language
  )
end

.load_prerequisites(prerequisites, current_step) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 107

def self.load_prerequisites(prerequisites, current_step)
  return [] unless prerequisites

  prerequisites.map do |t|
    t_path = Nexmo::Markdown::DocFinder.find(
      root: task_content_path,
      document: t,
      language: ::I18n.locale
    ).path
    raise "Prerequisite not found: #{t}" unless File.exist? t_path

    content = File.read(t_path)
    prereq = YAML.safe_load(content)
    {
      'path' => t,
      'title' => prereq['title'],
      'description' => prereq['description'],
      'is_active' => t == current_step,
      'content' => content,
    }
  end
end

.task_content_pathObject



180
181
182
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 180

def self.task_content_path
  "#{Nexmo::Markdown::Config.docs_base_path}/_tutorials"
end

.tutorials_pathObject



184
185
186
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 184

def self.tutorials_path
  "#{Nexmo::Markdown::Config.docs_base_path}/config/tutorials"
end

Instance Method Details

#code_languageObject



25
26
27
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 25

def code_language
  @code_language ||= @language || .code_language
end

#conclusion_taskObject



152
153
154
155
156
157
158
159
160
161
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 152

def conclusion_task
  return unless yaml['conclusion']

  Task.new(
    name: 'conclusion',
    title: yaml['conclusion']['title'],
    description: yaml['conclusion']['description'],
    current_step: current_step
  )
end

#content_for(step_name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 47

def content_for(step_name)
  if ['introduction', 'conclusion'].include? step_name
    raise "Invalid step: #{step_name}" unless yaml[step_name]

    return yaml[step_name]['content']
  end

  path = Nexmo::Markdown::DocFinder.find(
    root: self.class.task_content_path,
    document: step_name,
    language: ::I18n.locale,
    code_language: code_language
  ).path

  File.read(path)
end

#current_productObject



21
22
23
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 21

def current_product
  @current_product ||= @product || .default_product
end

#descriptionObject



33
34
35
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 33

def description
  @description ||= yaml['description'] || .description
end

#first_stepObject



64
65
66
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 64

def first_step
  subtasks.first&.name
end

#introduction_taskObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 141

def introduction_task
  return unless yaml['introduction']

  Task.new(
    name: 'introduction',
    title: yaml['introduction']['title'],
    description: yaml['introduction']['description'],
    current_step: current_step
  )
end

#load_file!Object



172
173
174
175
176
177
178
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 172

def load_file!
  Tutorial::FileLoader.new(
    root: self.class.tutorials_path,
    code_language: code_language,
    doc_name: name
  )
end

#metadataObject



17
18
19
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 17

def 
  @metadata ||= Metadata.new(name: name)
end

#next_stepObject



72
73
74
75
76
77
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 72

def next_step
  current_task_index = subtasks.map(&:name).index(@current_step)
  return nil unless current_task_index

  subtasks[current_task_index + 1]
end

#prerequisite?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 68

def prerequisite?
  prerequisites.map(&:name).include?(@current_step)
end

#prerequisite_taskObject



130
131
132
133
134
135
136
137
138
139
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 130

def prerequisite_task
  return if prerequisites.empty?

  Task.new(
    name: 'prerequisites',
    title: 'Prerequisites',
    description: 'Everything you need to complete this task',
    current_step: current_step
  )
end

#prerequisitesObject



41
42
43
44
45
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 41

def prerequisites
  @prerequisites ||= (yaml['prerequisites'] || []).map do |prereq|
    Prerequisite.new(name: prereq, code_language: code_language, current_step: current_step)
  end
end

#previous_stepObject



79
80
81
82
83
84
85
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 79

def previous_step
  current_task_index = subtasks.map(&:name).index(@current_step)
  return nil unless current_task_index
  return nil if current_task_index <= 0

  subtasks[current_task_index - 1]
end

#productsObject



37
38
39
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 37

def products
  @products ||= yaml['products'] || .products
end

#subtasksObject



97
98
99
100
101
102
103
104
105
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 97

def subtasks
  @subtasks ||= begin
    tasks.unshift(prerequisite_task)
    tasks.unshift(introduction_task)
    tasks.push(conclusion_task)

    tasks.compact
  end
end

#tasksObject



87
88
89
90
91
92
93
94
95
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 87

def tasks
  @tasks ||= (yaml['tasks'] || []).map do |t|
    Task.make_from(
      name: t,
      code_language: code_language,
      current_step: current_step
    )
  end
end

#titleObject



29
30
31
# File 'lib/nexmo_developer/app/models/tutorial.rb', line 29

def title
  @title ||= yaml['title'] || .title
end