Class: TypeformData::Typeform
- Inherits:
-
Object
- Object
- TypeformData::Typeform
- Includes:
- ComparableByIdAndConfig, ValueClass
- Defined in:
- lib/typeform_data/typeform.rb,
lib/typeform_data/typeform/by_id.rb,
lib/typeform_data/typeform/field.rb,
lib/typeform_data/typeform/stats.rb,
lib/typeform_data/typeform/answer.rb,
lib/typeform_data/typeform/question.rb,
lib/typeform_data/typeform/response.rb
Defined Under Namespace
Modules: ById Classes: Answer, Field, Question, Response, ResponsesRequest, Stats
Instance Method Summary collapse
- #fields ⇒ Object
- #hidden_fields ⇒ Object
-
#name ⇒ String
This method will make an AJAX request if this Typeform’s name hasn’t already been set during initialization.
-
#questions ⇒ TypeformData::Typeform::Question
Typeform’s ‘question’ concept (as expressed in the API) has the following disadvantages: - Each choice in a multi-select is treated as its own ‘question’ - Hidden Fields are included as ‘questions’ - Statements are included as ‘questions’.
-
#responses(params = {}) ⇒ Object
See www.typeform.com/help/data-api/ under “Filtering Options” for the full list of options.
- #statements ⇒ Object
- #stats ⇒ Object
Methods included from ComparableByIdAndConfig
Methods included from ValueClass
#as_json, included, #initialize, #marshal_dump, #marshal_load, #reconfig
Instance Method Details
#fields ⇒ Object
69 70 71 |
# File 'lib/typeform_data/typeform.rb', line 69 def fields @_fields ||= Field.from_questions(config, questions) end |
#hidden_fields ⇒ Object
73 74 75 |
# File 'lib/typeform_data/typeform.rb', line 73 def hidden_fields questions.select(&:hidden_field?) end |
#name ⇒ String
This method will make an AJAX request if this Typeform’s name hasn’t already been set during initialization.
Since the readable_attributes call above calls attr_reader(:name) for us, this method redefines name, and causes an (expected) “warning: method redefined” message when running tests.
93 94 95 |
# File 'lib/typeform_data/typeform.rb', line 93 def name @name ||= client.all_typeforms.find { |typeform| typeform.id == id }.name end |
#questions ⇒ TypeformData::Typeform::Question
Typeform’s ‘question’ concept (as expressed in the API) has the following disadvantages:
- Each choice in a multi-select is treated as its own 'question'
- Hidden Fields are included as 'questions'
- Statements are included as 'questions'
In practice, I recommend using TypeformData::Typeform#field instead, as it addresses these issues. Typeform#quesions is here so you have access to the underlying data if you need it.
65 66 67 |
# File 'lib/typeform_data/typeform.rb', line 65 def questions @_questions ||= fetch_questions end |
#responses(params = {}) ⇒ Object
See www.typeform.com/help/data-api/ under “Filtering Options” for the full list of options.
The “Ordering Options” are not yet implemented.
In general, this method will make multiple HTTP requests to the API in order to fetch all matching responses.
Stats and questions are cached across requests. Responses aren’t cached, but we plan to add some form of response caching in the future.
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/typeform_data/typeform.rb', line 24 def responses(params = {}) response = responses_request(collapse_and_validate_responses_params(params)) set_stats(response['stats']['responses']) # It's important that we set the questions first, since the Answer constructor (called # inside the Response constructor) looks up and denormalizes the question text. set_questions(response['questions']) response['responses'].map { |api_hash| Response.new( config, api_hash.dup.tap { |hash| hash[:typeform_id] = id # Sadly, the API returns [] if there aren't any Hidden Fields, and { ... } if there # are. unless hash['hidden'].is_a?(Hash) unless hash['hidden'] == [] config.logger.error "Received an unexpected value for a responses's hidden fields "\ "from the API: #{hash['hidden']}. Using {} instead. The full hash is:\n"\ "#{api_hash}" end hash['hidden'] = {} end }, fields, ) } end |
#statements ⇒ Object
77 78 79 |
# File 'lib/typeform_data/typeform.rb', line 77 def statements questions.select(&:statement?) end |
#stats ⇒ Object
81 82 83 |
# File 'lib/typeform_data/typeform.rb', line 81 def stats @_stats ||= fetch_stats end |