Class: TypedForm::Form

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/typed_form/form.rb

Overview

A representation of the Typeform Form Data for a single form response.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json:) ⇒ Form

Creates a new instance of a Form, to allow querying

Parameters:

  • json (String)

    Typeform Data API JSON input for form.



22
23
24
# File 'lib/typed_form/form.rb', line 22

def initialize(json:)
  @raw_json = json
end

Instance Attribute Details

#raw_jsonString (readonly)

Unformatted JSON data from an incoming Typeform Webhook.

Returns:

  • (String)

    the current value of raw_json



6
7
8
# File 'lib/typed_form/form.rb', line 6

def raw_json
  @raw_json
end

Class Method Details

.find_form_by(client:, form_id:, token:) ⇒ Form

Uses the Typeform API client to query/find the form based on the form_id and token, then builds a new Form from that JSON request.

Parameters:

  • client (API::Client)

    Typeform API client instance

  • form_id (String)

    Form ID you're querying

  • token (String)

    The token for the response you're retrieving

Returns:

  • (Form)

    A Form, via JSON fetched from Typeform's API



36
37
38
39
# File 'lib/typed_form/form.rb', line 36

def self.find_form_by(client:, form_id:, token:)
  json = client.find_form_by(form_id: form_id, token: token)
  new(json: json)
end

Instance Method Details

#jsonObject



26
27
28
# File 'lib/typed_form/form.rb', line 26

def json
  @_json ||= Util.normalize_spaces(@raw_json)
end

#questionsObject

See Also:

  • FormSubmission#questions


15
# File 'lib/typed_form/form.rb', line 15

def_delegators :submission, :questions

#responseObject

Raises:

  • (StandardError)


47
48
49
50
# File 'lib/typed_form/form.rb', line 47

def response
  raise StandardError, "Form expects a single response" if multi_response?
  responses.first
end

#responsesObject



11
# File 'lib/typed_form/form.rb', line 11

def_delegators :parsed_json, :responses

#to_hashHash

Builds a hash of Questions matched with Answers.

Returns:

  • (Hash)

    A Hash matching { "Question" => "Answer" } format.



43
44
45
# File 'lib/typed_form/form.rb', line 43

def to_hash
  questions.each_with_object({}) { |q, hash| hash[q.text] = q.answer }
end