typed_form 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d5ead981248e841e93b35f762ad6bfa365603a40
4
- data.tar.gz: 47d2587d6a6bfc5405ae5c1263e9b912d65e0853
3
+ metadata.gz: 64eb944541e118c09d324b045c26ecde7ecf1850
4
+ data.tar.gz: 44532a73dbfa5b4df00573749f08506c8c52cfe9
5
5
  SHA512:
6
- metadata.gz: 21dc2335dac2fbc928e5e640612cda0aa05df5b88f75274351b25c916d0cfaa5e210f172efa033fdfa699c32474d3c451c1b84567797c04a1d5f4a16a28d6b91
7
- data.tar.gz: 9973d47d3053017994a482de7584dc12c8001bb1ab2a68d10bf1693c302502dc3f2f59175437efcc0aabc604cfd3a34ef1a8332b8ea2d54dbf359aa57bd20c3a
6
+ metadata.gz: 66d340a306ec8d19e732a60792738da3896a460e7e7171b24f6d9b830d70b38855a754164491781e92f72ac1322d9ad4ada0e466e058baabb44ef39c0f24108f
7
+ data.tar.gz: f5eb95cbe9472827e3c7db936c3d4f0b1ad6fddee6e54fec5911c388206e36885f14d161bee57406f23a39eaaaa4c19b11ea37d8eca286fe781cb1eccd760946
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Change Log
2
2
 
3
+ ## [0.1.2] - 2017-03-13
4
+
5
+ Strips non-breaking whitespace ("\\u00a0") character from typeform data to normalize question/answer searching. Adds support for typecasting date, datetime, and integer objects from Typeform Responses & metadata.
6
+
3
7
  ## [0.1.1] - 2017-03-12
4
8
 
5
9
  Adds support for TypedForm::VirtualModel. Adds a decorator-like model which allows user-defined VirtualModels which respond with Typeform Data values.
@@ -1,10 +1,7 @@
1
1
  module TypedForm
2
2
  # A representation of the Typeform Form Data for a single form response.
3
- #
4
- # @attr_reader [String] json JSON data using Typeform's Data API schema
5
3
  class Form
6
4
  extend Forwardable
7
- attr_accessor :json
8
5
 
9
6
  # @!method responses
10
7
  # @see FormData::ParsedJson#responses
@@ -15,8 +12,16 @@ module TypedForm
15
12
  def_delegators :submission, :questions
16
13
 
17
14
  # Creates a new instance of a Form, to allow querying
15
+ #
16
+ # @params [String] json Typeform Data API JSON input for form.
18
17
  def initialize(json:)
19
- @json = json
18
+ @raw_json = json
19
+ end
20
+
21
+ # Removes non-breaking spaces (character point 160) from TypeForm data
22
+ # before beginning processing.
23
+ def json
24
+ @_json ||= @raw_json.gsub("\\u00a0", " ")
20
25
  end
21
26
 
22
27
  # Uses the Typeform API client to query/find the form based on the form_id
@@ -42,9 +42,11 @@ module TypedForm
42
42
  # @return [Array<Question>]
43
43
  def questions
44
44
  @_questions ||= input_questions.map do |question|
45
+ text_answers = answers_for(question.ids)
46
+
45
47
  Question.with_response_data(
46
48
  question: question,
47
- answer: answers_for(question.ids),
49
+ answer: typecast_answer(answer: text_answers, type: question.type),
48
50
  text: extrapolated_question_text(question)
49
51
  )
50
52
  end
@@ -86,6 +88,31 @@ module TypedForm
86
88
  def find_answer_by_id(id)
87
89
  answers.instance_variable_get("@#{id}")
88
90
  end
91
+
92
+ def form_duplicated_on_typeform?
93
+ questions.detect { |q| q.text =~ /_[a-zA-Z]_/}
94
+ end
95
+
96
+ # Thanks for the date normalization, Typeform. Depending on whether your
97
+ # field is duplicated from another form (by copying a form), the format
98
+ # will be a stringified date, or a string like "1491091200000" - which
99
+ # represents seconds from the epoch, but with 3 zero's added for no
100
+ # explicable reason.
101
+ def normalized_date(date_string)
102
+ if date_string =~ /\d{4}-\d{2}-\d{2}/
103
+ Date.parse(date_string)
104
+ else
105
+ Time.at(date_string[0..9].to_i).utc.to_date
106
+ end
107
+ end
108
+
109
+ def typecast_answer(answer:, type:)
110
+ case type
111
+ when "date" then normalized_date(answer)
112
+ when "number" then answer.to_i
113
+ else answer
114
+ end
115
+ end
89
116
  end
90
117
  end
91
118
  end
@@ -32,11 +32,13 @@ module TypedForm
32
32
  @original_text = original_text
33
33
  end
34
34
 
35
- # Performs a regular expression based on the id of the question, to
35
+ # Splits into an array based on the id of the question, to
36
36
  # determine the Type of object. This information can be queried in order
37
37
  # to allow users to handle various types of Typeform data differently.
38
38
  def type
39
- @_type ||= determine_type
39
+ detected_type = ids.map { |id| id.split("_")[0] }.uniq
40
+ return detected_type.first if detected_type.size == 1
41
+ raise StandardError, "Cannot detect type of question ids #{ids}"
40
42
  end
41
43
 
42
44
  # Creates a new Question with existing data from a previous question.
@@ -49,14 +51,6 @@ module TypedForm
49
51
  new_question.text = text
50
52
  end.freeze
51
53
  end
52
-
53
- private
54
-
55
- def determine_type
56
- detected_type = ids.map { |id| id.split("_")[0] }.uniq
57
- return detected_type.first if detected_type.size == 1
58
- raise StandardError, "Cannot detect type of question ids #{ids}"
59
- end
60
54
  end
61
55
  end
62
56
  end
@@ -1,4 +1,4 @@
1
1
  module TypedForm
2
2
  # Up-to-date version of gem.
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
@@ -59,6 +59,14 @@ module TypedForm
59
59
  question.answer
60
60
  end
61
61
 
62
+ def typecast_response_field(message, e)
63
+ response = message.send(e)
64
+ case e
65
+ when /date/ then DateTime.parse(response)
66
+ else response
67
+ end
68
+ end
69
+
62
70
  def able_to_define?(attr)
63
71
  !self.class.method_defined?(attr) &&
64
72
  attr.to_s.match(/^[a-z_][a-zA-Z_0-9!?]*$/)
@@ -79,7 +87,7 @@ module TypedForm
79
87
 
80
88
  def response_attribute_value(messages)
81
89
  messages.split(".").inject(form.response) do |message, e|
82
- message.send(e)
90
+ typecast_response_field(message, e)
83
91
  end
84
92
  end
85
93
 
data/lib/typed_form.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "httparty"
2
2
  require "forwardable"
3
3
  require "arendelle"
4
+ require "date"
4
5
  require "typed_form/api/client"
5
6
  require "typed_form/form_data/parsed_json"
6
7
  require "typed_form/form_data/question"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Cole
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-03-13 00:00:00.000000000 Z
12
+ date: 2017-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty