Class: TypedForm::FormData::Question

Inherits:
Object
  • Object
show all
Defined in:
lib/typed_form/form_data/question.rb

Overview

Question value objects represent the Typeform concept of a question, but expose an interface for querying questions and answers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ids:, field_id:, original_text:) ⇒ Question

Creates a new Question value object, which represents any number of questions in a Typeform Form that can be logically represented as a single question. This includes both single question fields and fields like multiple choice, picture choice, etc.

Parameters:

  • ids (Array<Strong>)

    The Typeform IDs for the question. This is ids instead of id because of the way Typeform represents multiple choice questions.

  • field_id (Integer)

    The Typeform Field ID for the question

  • original_text (String)

    The original text for the question before answers are extrapolated back into the question.



29
30
31
32
33
# File 'lib/typed_form/form_data/question.rb', line 29

def initialize(ids:, field_id:, original_text:)
  @ids = ids
  @field_id = field_id
  @original_text = original_text
end

Instance Attribute Details

#answerString

The answer of the question.

Returns:

  • (String)

    the current value of answer



14
15
16
# File 'lib/typed_form/form_data/question.rb', line 14

def answer
  @answer
end

#field_idInteger (readonly)

The Typeform Field ID for the question

Returns:

  • (Integer)

    the current value of field_id



14
15
16
# File 'lib/typed_form/form_data/question.rb', line 14

def field_id
  @field_id
end

#idsArray<Strong> (readonly)

The Typeform IDs for the question. This is ids instead of id because of the way Typeform represents multiple choice questions.

Returns:

  • (Array<Strong>)

    the current value of ids



14
15
16
# File 'lib/typed_form/form_data/question.rb', line 14

def ids
  @ids
end

#original_answerObject

Returns the value of attribute original_answer.



16
17
18
# File 'lib/typed_form/form_data/question.rb', line 16

def original_answer
  @original_answer
end

#original_textObject (readonly)

Returns the value of attribute original_text.



15
16
17
# File 'lib/typed_form/form_data/question.rb', line 15

def original_text
  @original_text
end

#textObject

Returns the value of attribute text.



16
17
18
# File 'lib/typed_form/form_data/question.rb', line 16

def text
  @text
end

#TheString (readonly)

original text for the question before answers are extrapolated back into the question.

Returns:

  • (String)

    the current value of The



14
15
16
# File 'lib/typed_form/form_data/question.rb', line 14

def The
  @The
end

Class Method Details

.with_response_data(question:, text:, answer:, original_answer:) ⇒ Question

Creates a new Question with existing data from a previous question.

Returns:

  • (Question)

    Question with extrapolated text in questions and with the answer to the question as an attribute.



48
49
50
51
52
53
54
# File 'lib/typed_form/form_data/question.rb', line 48

def self.with_response_data(question:, text:, answer:, original_answer:)
  question.dup.tap do |new_question|
    new_question.answer = answer
    new_question.original_answer = original_answer
    new_question.text = text
  end.freeze
end

Instance Method Details

#typeObject

Splits into an array based on the id of the question, to determine the Type of object. This information can be queried in order to allow users to handle various types of Typeform data differently.

Raises:

  • (StandardError)


38
39
40
41
42
# File 'lib/typed_form/form_data/question.rb', line 38

def type
  detected_type = ids.map { |id| id.split("_")[0] }.uniq
  return detected_type.first if detected_type.size == 1
  raise StandardError, "Cannot detect type of question ids #{ids}"
end