Class: Quby::Compiler::Outputs::RoquaSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/quby/compiler/outputs/roqua_serializer.rb

Constant Summary collapse

QUBY_TYPE_TO_ROQUA_TYPE =
{
  check_box: 'multi_select',
  date: 'date_parts',
  float: 'float',
  integer: 'integer',
  radio: 'single_select',
  scale: 'single_select',
  select: 'single_select',
  string: 'text',
  textarea: 'text'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(questionnaire) ⇒ RoquaSerializer

Returns a new instance of RoquaSerializer.



19
20
21
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 19

def initialize(questionnaire)
  @questionnaire = questionnaire
end

Instance Attribute Details

#questionnaireObject (readonly)

Returns the value of attribute questionnaire.



17
18
19
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 17

def questionnaire
  @questionnaire
end

Instance Method Details

#as_json(options = {}) ⇒ Object



23
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
52
53
54
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 23

def as_json(options = {})
  {
    key: questionnaire.key,
    name: questionnaire.title,
    short_description: questionnaire.short_description,
    description: questionnaire.description,
    renderer:,
    versions: versions,
    editable_by_professionals: questionnaire.editable_by_professionals,
    editable_from_version: questionnaire.editable_from_version,
    environments: questionnaire.environments,
    keys: questionnaire.roqua_keys,
    roqua_keys: questionnaire.roqua_keys,
    sbg_key: questionnaire.sbg_key,
    sbg_domains: questionnaire.sbg_domains,
    outcome_regeneration_requested_at: questionnaire.outcome_regeneration_requested_at,
    deactivate_answers_requested_at: questionnaire.deactivate_answers_requested_at,
    respondent_types: questionnaire.respondent_types,
    tags: questionnaire.tags,
    charts: charts,
    original: questionnaire.original,
    outcome_tables_schema: outcome_tables_schema,
    outcome_description: questionnaire.outcome_description,
    questions: questions,
    scores: scores,
    anonymous_conditions: questionnaire.anonymous_conditions.as_json,
    flags: flags,
    textvars: textvars,
    license: questionnaire.license,
    licensor: questionnaire.licensor,
  }.compact
end

#chart_baseline(baseline) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 108

def chart_baseline(baseline)
  case baseline
  when nil
    nil
  when Float, Integer
    {default: {default: baseline}}
  when Hash
    baseline
  end
end

#chartsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 70

def charts
  {
    overview: questionnaire.charts.overview && {
      subscore: questionnaire.charts.overview.subscore,
      y_max: questionnaire.charts.overview.y_max,
    },
    others: questionnaire.charts.map do |chart|
      case chart
      when Quby::Compiler::Entities::Charting::LineChart
        {
          tonality: chart.tonality,
          baseline: chart_baseline(chart.baseline),
          clinically_relevant_change: chart.clinically_relevant_change,
        }
      when Quby::Compiler::Entities::Charting::OverviewChart
        {
          subscore: chart.subscore,
          y_max: chart.y_max,
        }
      else
        {}
      end.merge(
        key: chart.key,
        type: chart.type,
        title: chart.title,
        plottables: chart.plottables,
        y_categories: chart.y_categories,
        y_range_categories: chart.y_range_categories,
        chart_type: chart.chart_type,
        y_range: chart.y_range,
        tick_interval: chart.tick_interval,
        plotbands: chart.plotbands,
        plotlines: chart.plotlines
      )
    end
  }
end

#date_parts_for(question) ⇒ Object

{key: “v_date_yyyy”, ..} key [string]



225
226
227
228
229
230
231
232
233
234
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 225

def date_parts_for(question)
  return nil unless question.type == :date

  question.components.map { |component|
    {
      part: component,
      key: question.send("#{component}_key")
    }
  }
end

#flagsObject



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 280

def flags
  questionnaire.flags.map do |key, flag|
    {
      key: flag.key,
      description: flag.description,
      description_true: flag.description_true,
      description_false: flag.description_false,
      internal: flag.internal,
      trigger_on: flag.trigger_on,
      shows_questions: flag.shows_questions,
      hides_questions: flag.hides_questions,
      depends_on: flag.depends_on,
      default: flag.default,
      default_in_interface: flag.default # TODO: Remove this when Roqua supports default natively
    }
  end
end

#options_for(question) ⇒ Object

{..} key [string] value [nil/string] nil for check_box, string otherwise description [nil/string] context_free_description or parsed/stripped description, no html child_question_keys [nil/]



241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 241

def options_for(question)
  return nil if question.options.empty?

  question.all_options.reject { |option| option.inner_title || option.placeholder }
                  .map { |option|
    {
      key: option.key,
      value: (option.value.to_s unless question.type == :check_box),
      description: option.context_free_description || parse_markdown_and_strip_tags(option.label || option.description),
      child_question_keys: option.questions.map(&:key).presence,
      unchecks_all_others: question.try(:uncheck_all_option) && question.uncheck_all_option == option.key
    }.compact
  }
end

#outcome_tables_from_definitionObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 135

def outcome_tables_from_definition
  # hash of tables, with the score keys (rows) and subscore keys (columns) used for each
  tables = []
  # hash of `subscore_key: subscore_label` pairs used in tables
  headers = {}

  questionnaire.outcome_tables.each do |table|
    tables << {
      key: table.key,
      name: table.name,
      default_collapsed: table.default_collapsed,
      score_keys: table.score_keys,
      subscore_keys: table.subscore_keys
    }.compact

    table.subscore_keys.each do |subscore_key|
      table.score_keys.find do |score_key|
        subschema = questionnaire.score_schemas[score_key].subscore_schemas.find do |subschema|
          subschema.key == subscore_key
        end
        headers[subscore_key] = subschema&.label
      end
    end
  end

  {
    headers: headers,
    tables: tables,
  }
end

#outcome_tables_from_score_schemasObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 166

def outcome_tables_from_score_schemas
  # hash of tables, with the score keys (rows) and subscore keys (columns) used for each
  tables = Hash.new{ |hash, key| hash[key] = {score_keys: Set.new, subscore_keys: Set.new } }
  # hash of `subscore_key: subscore_label` pairs used in tables
  headers = {}

  questionnaire.score_schemas.values.each do |schema|
    schema.subscore_schemas.each do |subschema|
      next if subschema.outcome_table.blank?
      tables[subschema.outcome_table][:key] = subschema.outcome_table
      tables[subschema.outcome_table][:subscore_keys] << subschema.key
      tables[subschema.outcome_table][:score_keys] << schema.key
      headers[subschema.key] = subschema.label
    end
  end

  {
    headers: headers,
    tables: tables.values,
  }
end

#outcome_tables_schemaObject

configuration for outcome tables. tables:

<outcome_table_name:Symbol>: # each entry is a table.
  score_keys: Set[<schema.key:Symbol>] # rows in the table
  subscore_keys: Set[<subschema.key:Symbol>] # columns  in the table

headers:

<subscore_key:Symbol>: <subscore.label:String> # headers for each subscore key for all tables.


127
128
129
130
131
132
133
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 127

def outcome_tables_schema
  if questionnaire.outcome_tables.present?
    outcome_tables_from_definition
  else
    outcome_tables_from_score_schemas
  end
end

#parse_markdown_and_strip_tags(markdown_or_html) ⇒ Object

Only parses markdown for layout v1



310
311
312
313
314
315
316
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 310

def parse_markdown_and_strip_tags(markdown_or_html)
  if questionnaire.layout_version == :v2 # only supports markdown in `text`-method, which doesn't show up in roqua.json
    strip_tags_without_html_encode(markdown_or_html)
  else
    strip_tags_without_html_encode(Quby::Compiler::MarkdownParser.new(markdown_or_html).to_html)
  end
end

#questionsObject

{.., options: {.., date_parts: ..}, ..} nils are removed from hash. key [string] sbg_key [nil/string] not defaulted to key type [string] title [nil/string] no html context_free_title [nil/string] no html, not defaulted to title! unit [nil/string] no html (not in quby either) deprecated [nil/true] only show if filled for old answers default_invisible [nil/true] only show if filled or when all questions are shown parent_question_key [nil/string] title_questions or question defined under option parent_option_key [nil/string] question defined under option title_question_key [nil/string] date_parts [nil/..] options: [nil/..]



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 203

def questions
  questionnaire.sorted_questions.reject{_1.type == :hidden}.map { |question|
    {
      key: question.key,
      sbg_key: question.sbg_key,
      type: QUBY_TYPE_TO_ROQUA_TYPE.fetch(question.type),
      title: parse_markdown_and_strip_tags(question.title),
      context_free_title: question.context_free_title,
      unit: question.unit,
      deprecated: question.hidden.presence,
      default_invisible: question.default_invisible.presence,
      parent_question_key: question.parent&.key,
      parent_option_key: question.parent_option_key,
      title_question_key: question.title_question&.key,
      date_parts: date_parts_for(question),
      options: options_for(question)
    }.compact
  }
end

#rendererObject



56
57
58
59
60
61
62
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 56

def renderer
  case questionnaire.layout_version
  when :v2 then 'quby2'
  when :v1 then 'quby'
  else nil
  end
end

#scoresObject

{ key: { .., subscores: { subkey: .. } } }


257
258
259
260
261
262
263
264
265
266
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 257

def scores
  questionnaire.score_schemas.values.map { |score|
    {
      key: score.key,
      label: score.label,
      sbg_key: score.sbg_key,
      subscores: subscores_for(score)
    }.compact
  }
end

#strip_tags_without_html_encode(html) ⇒ Object



318
319
320
321
322
323
324
325
326
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 318

def strip_tags_without_html_encode(html)
  Nokogiri::HTML(html).tap do |doc|
    doc.css('br, hr, img').each { |node| node.replace(' ') }
  end \
    .text
    .gsub(/\s+/, ' ')
    .strip
    .presence
end

#subscores_for(score) ⇒ Object

{ subkey: .. }



269
270
271
272
273
274
275
276
277
278
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 269

def subscores_for(score)
  score.subscore_schemas.map { |subscore|
    {
      key: subscore.key,
      label: subscore.label,
      export_key: subscore.export_key,
      type: subscore.type
    }.compact
  }
end

#textvarsObject



298
299
300
301
302
303
304
305
306
307
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 298

def textvars
  questionnaire.textvars.map do |key, textvar|
    {
      key: textvar.key,
      description: textvar.description,
      default: textvar.default,
      depends_on_flag: textvar.depends_on_flag
    }
  end
end

#versionsObject



64
65
66
67
68
# File 'lib/quby/compiler/outputs/roqua_serializer.rb', line 64

def versions
  questionnaire.versions.map do |version|
    version.as_json(only: %w[number release_notes regenerate_outcome deactivate_answers])
  end
end