typeform_data 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +148 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +1 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +50 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/typeform_data/api_response.rb +17 -0
- data/lib/typeform_data/client.rb +32 -0
- data/lib/typeform_data/errors.rb +13 -0
- data/lib/typeform_data/requestor/config.rb +29 -0
- data/lib/typeform_data/requestor.rb +59 -0
- data/lib/typeform_data/typeform/question.rb +28 -0
- data/lib/typeform_data/typeform/response.rb +37 -0
- data/lib/typeform_data/typeform/stats.rb +26 -0
- data/lib/typeform_data/typeform.rb +160 -0
- data/lib/typeform_data/version.rb +4 -0
- data/lib/typeform_data.rb +21 -0
- data/typeform_data.gemspec +41 -0
- metadata +183 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 08af965b58a09c76f9fe8f5937c431cb73a4b09d
|
4
|
+
data.tar.gz: a9764f0ba29abe0b8ea53a7b82ebb037f1766c79
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b5f151253223bff48b951c368ed3822d1df15292ddf3c400ab0104ea3dd1286767497e6ec682681781abb561103a0ab642d60d5e850c9af0093fabfe5cdbc067
|
7
|
+
data.tar.gz: a8025b7f9b7557086ebf127cbe4b6756e16af1223c96bbcfd4f28c795de1131da494d697a47e760c016746b88c2a16f9d2e341c8584f481340dd5f578139721f
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
Exclude:
|
4
|
+
- 'Gemfile'
|
5
|
+
- 'Gemfile.lock'
|
6
|
+
- 'Rakefile'
|
7
|
+
|
8
|
+
Documentation:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Metrics/LineLength:
|
12
|
+
Enabled: true
|
13
|
+
Max: 100
|
14
|
+
|
15
|
+
Style/BlockDelimiters:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Metrics/AbcSize:
|
19
|
+
Enabled: true
|
20
|
+
Max: 31
|
21
|
+
|
22
|
+
Style/FormatString:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Metrics/CyclomaticComplexity:
|
26
|
+
Max: 9
|
27
|
+
|
28
|
+
Metrics/MethodLength:
|
29
|
+
Max: 24
|
30
|
+
|
31
|
+
Metrics/PerceivedComplexity:
|
32
|
+
Max: 9
|
33
|
+
|
34
|
+
Rails/TimeZone:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Style/ClassVars:
|
38
|
+
Enabled: true
|
39
|
+
|
40
|
+
# Chaining multiple blocks together is actively encouraged. It leads to a more functional style
|
41
|
+
# based on map/select and helps avoid unintentional side-effects.
|
42
|
+
Style/MultilineBlockChain:
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
Style/Next:
|
46
|
+
Enabled: true
|
47
|
+
MinBodyLength: 1
|
48
|
+
|
49
|
+
Style/PredicateName:
|
50
|
+
Enabled: true
|
51
|
+
|
52
|
+
Style/RaiseArgs:
|
53
|
+
EnforcedStyle: compact
|
54
|
+
|
55
|
+
Style/StructInheritance:
|
56
|
+
Enabled: true
|
57
|
+
|
58
|
+
Style/VariableName:
|
59
|
+
Enabled: true
|
60
|
+
|
61
|
+
Style/ClassAndModuleChildren:
|
62
|
+
Enabled: true
|
63
|
+
|
64
|
+
Style/EmptyLinesAroundBlockBody:
|
65
|
+
Enabled: false
|
66
|
+
|
67
|
+
# TODO: these empty lines should be possibly required for long classes, to visually differentiate
|
68
|
+
# the first method from the class declaration
|
69
|
+
Style/EmptyLinesAroundClassBody:
|
70
|
+
Enabled: false
|
71
|
+
|
72
|
+
Style/SpaceInsideParens:
|
73
|
+
Enabled: false
|
74
|
+
|
75
|
+
Style/AlignHash:
|
76
|
+
Enabled: true
|
77
|
+
|
78
|
+
Style/WordArray:
|
79
|
+
Enabled: false
|
80
|
+
|
81
|
+
Style/EmptyLines:
|
82
|
+
Enabled: false
|
83
|
+
|
84
|
+
Style/AndOr:
|
85
|
+
Enabled: true
|
86
|
+
|
87
|
+
Style/SpaceInsideBrackets:
|
88
|
+
Enabled: false
|
89
|
+
|
90
|
+
# Disabled so we have more flexibility around aligning operators, e.g. for multiline ternary
|
91
|
+
# expressions.
|
92
|
+
Style/SpaceAroundOperators:
|
93
|
+
Enabled: false
|
94
|
+
|
95
|
+
Style/MultilineTernaryOperator:
|
96
|
+
Enabled: false
|
97
|
+
|
98
|
+
Style/EmptyLinesAroundModuleBody:
|
99
|
+
Enabled: false
|
100
|
+
|
101
|
+
Style/TrailingCommaInLiteral:
|
102
|
+
Enabled: false
|
103
|
+
|
104
|
+
Style/TrailingCommaInArguments:
|
105
|
+
Enabled: false
|
106
|
+
|
107
|
+
Style/TrailingWhitespace:
|
108
|
+
Enabled: true
|
109
|
+
|
110
|
+
Metrics/ClassLength:
|
111
|
+
Enabled: false
|
112
|
+
|
113
|
+
Style/SpaceAfterNot:
|
114
|
+
Enabled: true
|
115
|
+
|
116
|
+
Metrics/ModuleLength:
|
117
|
+
Enabled: false
|
118
|
+
|
119
|
+
Style/SpaceInsideRangeLiteral:
|
120
|
+
Enabled: true
|
121
|
+
|
122
|
+
# Disabling this cop allows us to say:
|
123
|
+
#
|
124
|
+
# SomeModuleWithA.really_long_method_name_that_goes_all_the_way_over_to_the_other(side, of_the
|
125
|
+
# screen, that_has, many_arguments, which_also_have_long_names)
|
126
|
+
#
|
127
|
+
# Instead of the following (or similar... obviously this example is a bit exaggerated):
|
128
|
+
#
|
129
|
+
# SomeModuleWithA.really_long_method_name_that_goes_all_the_way_over_to_the_other(side, of_the
|
130
|
+
# screen, that_has,
|
131
|
+
# many_arguments,
|
132
|
+
# which_also_have_long_names)
|
133
|
+
Style/AlignParameters:
|
134
|
+
Enabled: false
|
135
|
+
|
136
|
+
Style/ClosingParenthesisIndentation:
|
137
|
+
Enabled: false
|
138
|
+
|
139
|
+
Style/RaiseArgs:
|
140
|
+
Enabled: false
|
141
|
+
|
142
|
+
Style/CaseIndentation:
|
143
|
+
Enabled: true
|
144
|
+
|
145
|
+
# I hate !!, but there's no good alternative. There are legitimiate circumstances where
|
146
|
+
# !something.nil? isn't a good solution. I wish Ruby had a to_b ("to boolean") method.
|
147
|
+
Style/DoubleNegation:
|
148
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1 - Initial release
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at maxfield.wallace@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Max Wallace
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# TypeformData
|
2
|
+
|
3
|
+
A Ruby client for Typeform's Data API (https://www.typeform.com/help/data-api/).
|
4
|
+
|
5
|
+
This is alpha software and doesn't currently cover all the use cases you'd probably expect. I've just finished implementing response fetching (including de-pagination, so you can fetch _all_ the responses in one method call), but the full data model isn't built out yet.
|
6
|
+
|
7
|
+
Unless you're eager to dive into the code, I'd suggest waiting until next week to check out this gem.
|
8
|
+
|
9
|
+
TODO:
|
10
|
+
- Add more detail, and example method calls.
|
11
|
+
- Add an explanation: why another gem? What makes this gem different?
|
12
|
+
|
13
|
+
### Notes on the API
|
14
|
+
|
15
|
+
- ID vs. UID: they aren't used consistently across the docs and actual API responses.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'typeform_data'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install typeform_data
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
TODO: Write usage instructions here
|
36
|
+
|
37
|
+
## Development
|
38
|
+
|
39
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
40
|
+
|
41
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/typeform_data. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
46
|
+
|
47
|
+
|
48
|
+
## License
|
49
|
+
|
50
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'typeform_data'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
require 'pry'
|
12
|
+
Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# As the code says, we're using a simple decorator/delegator here to signal intent to possibly
|
4
|
+
# modify the TypeformData::ApiResponse API.
|
5
|
+
module TypeformData
|
6
|
+
class ApiResponse < SimpleDelegator
|
7
|
+
|
8
|
+
def json
|
9
|
+
body
|
10
|
+
end
|
11
|
+
|
12
|
+
def parsed_json
|
13
|
+
@_parsed_json ||= JSON.parse(body)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'typeform_data/version'
|
3
|
+
|
4
|
+
module TypeformData
|
5
|
+
class Client
|
6
|
+
|
7
|
+
def initialize(api_key:)
|
8
|
+
@_requestor = ::TypeformData::Requestor.new(api_key: api_key)
|
9
|
+
end
|
10
|
+
|
11
|
+
def all_typeforms
|
12
|
+
get('forms').parsed_json.map do |form_hash|
|
13
|
+
::TypeformData::Typeform.new(self, form_hash)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def typeform(id)
|
18
|
+
::TypeformData::Typeform.new(self, id)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Your API key will automatically be added to the request URL as a query param, as required by
|
22
|
+
# the API.
|
23
|
+
#
|
24
|
+
# @param String
|
25
|
+
# @param Hash
|
26
|
+
# @return TypeformData::ApiResponse
|
27
|
+
def get(endpoint, params = {})
|
28
|
+
@_requestor.get(endpoint, params)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module TypeformDataClient
|
3
|
+
class Error < StandardError; end
|
4
|
+
class InvalidApiKey < Error; end
|
5
|
+
class ConnectionRefused < Error; end
|
6
|
+
class BadRequest < Error; end
|
7
|
+
class ArgumentError < Error; end
|
8
|
+
class UnexpectedError < Error; end
|
9
|
+
|
10
|
+
# When using the 'token' field in requests, the API may return a 404 even if the endpoint path
|
11
|
+
# is correct.
|
12
|
+
class InvalidEndpointOrMissingResource < Error; end
|
13
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module TypeformData
|
3
|
+
class Requestor
|
4
|
+
|
5
|
+
class Config
|
6
|
+
attr_reader :api_key
|
7
|
+
|
8
|
+
def initialize(api_key:)
|
9
|
+
@api_key = api_key
|
10
|
+
end
|
11
|
+
|
12
|
+
# These values were determined via URI.parse('https://api.typeform.com')
|
13
|
+
|
14
|
+
def host
|
15
|
+
'api.typeform.com'
|
16
|
+
end
|
17
|
+
|
18
|
+
def port
|
19
|
+
443
|
20
|
+
end
|
21
|
+
|
22
|
+
def api_version
|
23
|
+
1
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TypeformData
|
4
|
+
class Requestor
|
5
|
+
|
6
|
+
def initialize(api_key:)
|
7
|
+
@config = ::TypeformData::Requestor::Config.new(api_key: api_key)
|
8
|
+
end
|
9
|
+
|
10
|
+
def get(endpoint, params = nil)
|
11
|
+
request(Net::HTTP::Get, request_path(endpoint), params)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def request_path(endpoint)
|
17
|
+
"/v#{@config.api_version}/#{endpoint}"
|
18
|
+
end
|
19
|
+
|
20
|
+
# rubocop:disable Metrics/MethodLength
|
21
|
+
# @return TypeformData::ApiResponse
|
22
|
+
def request(method_class, path, input_params = {})
|
23
|
+
params = input_params.dup
|
24
|
+
params[:key] = @config.api_key
|
25
|
+
|
26
|
+
response = Net::HTTP.new(@config.host, @config.port).tap { |http|
|
27
|
+
http.use_ssl = true
|
28
|
+
|
29
|
+
# Uncomment this line for debugging:
|
30
|
+
# http.set_debug_output($stdout)
|
31
|
+
}.request(
|
32
|
+
method_class.new(
|
33
|
+
path + '?' + URI.encode_www_form(params),
|
34
|
+
'Content-Type' => 'application/json'
|
35
|
+
)
|
36
|
+
)
|
37
|
+
|
38
|
+
case response
|
39
|
+
when Net::HTTPNotFound then
|
40
|
+
raise TypeformDataClient::InvalidEndpointOrMissingResource, path
|
41
|
+
when Net::HTTPForbidden then
|
42
|
+
raise TypeformDataClient::InvalidApiKey, "Invalid api key: #{@config.api_key}"
|
43
|
+
when Net::HTTPBadRequest then
|
44
|
+
raise TypeformDataClient::BadRequest, 'There was an error processing your request: '\
|
45
|
+
"#{response.body}, with params: #{params}"
|
46
|
+
when Net::HTTPSuccess
|
47
|
+
return TypeformData::ApiResponse.new(response)
|
48
|
+
else
|
49
|
+
raise TypeformDataClient::UnexpectedError, "A #{response.code} error has occurred: "\
|
50
|
+
"'#{response.message}'"
|
51
|
+
end
|
52
|
+
|
53
|
+
rescue Errno::ECONNREFUSED
|
54
|
+
raise TypeformDataClient::ConnectionRefused, 'The connection was refused'
|
55
|
+
end
|
56
|
+
# rubocop:enable Metrics/MethodLength
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module TypeformData
|
3
|
+
class Typeform
|
4
|
+
|
5
|
+
class Question
|
6
|
+
attr_reader :id
|
7
|
+
attr_reader :question
|
8
|
+
attr_reader :field_id
|
9
|
+
|
10
|
+
def initialize(typeform, id:, question:, field_id:)
|
11
|
+
@typeform = typeform
|
12
|
+
@id = id
|
13
|
+
@question = question
|
14
|
+
@field_id = field_id
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.from_hash(typeform, hash)
|
18
|
+
new(
|
19
|
+
typeform,
|
20
|
+
id: hash['id'],
|
21
|
+
question: hash['question'],
|
22
|
+
field_id: hash['field_id'],
|
23
|
+
)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module TypeformData
|
3
|
+
class Typeform
|
4
|
+
|
5
|
+
class Response
|
6
|
+
attr_reader :typeform
|
7
|
+
attr_reader :token
|
8
|
+
attr_reader :metadata
|
9
|
+
attr_reader :hidden
|
10
|
+
|
11
|
+
def completed?
|
12
|
+
@completed == 1
|
13
|
+
end
|
14
|
+
|
15
|
+
alias hidden_fields hidden
|
16
|
+
|
17
|
+
def initialize(typeform, token:, metadata:, hidden:, completed:)
|
18
|
+
@typeform = typeform
|
19
|
+
@token = token
|
20
|
+
@metadata = metadata
|
21
|
+
@hidden = hidden
|
22
|
+
@completed = completed
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.from_hash(typeform, hash)
|
26
|
+
new(
|
27
|
+
typeform,
|
28
|
+
token: hash['token'],
|
29
|
+
metadata: hash['metadata'],
|
30
|
+
hidden: hash['hidden'],
|
31
|
+
completed: hash['completed']
|
32
|
+
)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module TypeformData
|
3
|
+
class Typeform
|
4
|
+
|
5
|
+
class Stats
|
6
|
+
attr_reader :showing
|
7
|
+
attr_reader :total
|
8
|
+
attr_reader :completed
|
9
|
+
|
10
|
+
def initialize(showing:, total:, completed:)
|
11
|
+
@showing = showing
|
12
|
+
@total = total
|
13
|
+
@completed = completed
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.from_stats_hash(stats_hash)
|
17
|
+
new(
|
18
|
+
showing: stats_hash['showing'],
|
19
|
+
total: stats_hash['total'],
|
20
|
+
completed: stats_hash['completed'],
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'typeform_data/version'
|
3
|
+
|
4
|
+
module TypeformData
|
5
|
+
class Typeform
|
6
|
+
attr_reader :id
|
7
|
+
attr_reader :name
|
8
|
+
|
9
|
+
MAX_PAGE_SIZE = 1000 # This is documented at https://www.typeform.com/help/data-api/.
|
10
|
+
|
11
|
+
# @param TypeformData::Client
|
12
|
+
# @param Hash<[String, Symbol], String>]
|
13
|
+
def initialize(client, attrs)
|
14
|
+
input_id = attrs['id'] || attrs[:id]
|
15
|
+
name = attrs['name'] || attrs[:name]
|
16
|
+
|
17
|
+
unless client.is_a?(::TypeformData::Client)
|
18
|
+
raise TypeformData::Error, 'Expected to receive a TypeformData::Client'
|
19
|
+
end
|
20
|
+
|
21
|
+
str_id = ''
|
22
|
+
|
23
|
+
begin
|
24
|
+
str_id = input_id.to_s
|
25
|
+
rescue NoMethodError
|
26
|
+
raise TypeformData::Error, "The provided ID is not a String, or can't be converted to one."
|
27
|
+
end
|
28
|
+
|
29
|
+
@id = str_id
|
30
|
+
@name = name if name
|
31
|
+
@client = client
|
32
|
+
end
|
33
|
+
|
34
|
+
PERMITTED_KEYS = {
|
35
|
+
'completed' => Object,
|
36
|
+
'since' => Object,
|
37
|
+
'until' => Object,
|
38
|
+
'offset' => Fixnum,
|
39
|
+
'limit' => Fixnum,
|
40
|
+
'token' => String,
|
41
|
+
}.freeze
|
42
|
+
|
43
|
+
# See https://www.typeform.com/help/data-api/ under "Filtering Options" for the full list of
|
44
|
+
# options.
|
45
|
+
#
|
46
|
+
# The "Ordering Options" are not yet implemented.
|
47
|
+
#
|
48
|
+
# In general, this method will make multiple HTTP requests to the API in order to fetch all
|
49
|
+
# matching responses.
|
50
|
+
#
|
51
|
+
# Stats and questions are cached across requests. Responses aren't cached, but we plan to add
|
52
|
+
# some form of response caching in the future.
|
53
|
+
#
|
54
|
+
# @param Hash<[String, Symbol], [String, Symbol]> params
|
55
|
+
# @raise TypeformData::ArgumentError
|
56
|
+
def responses(params = {})
|
57
|
+
# TODO: not sure what the implementation will be here, since responses_request needs to
|
58
|
+
# handle the awkwardness of returning multiple kinds of data at the same time.
|
59
|
+
response = responses_request(collapse_and_validate_responses_params(params))
|
60
|
+
set_stats(response['stats']['responses'])
|
61
|
+
set_questions(response['questions'])
|
62
|
+
|
63
|
+
response['responses'].map { |hash|
|
64
|
+
Response.from_hash(self, hash)
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
def questions
|
69
|
+
@_questions ||= fetch_questions
|
70
|
+
end
|
71
|
+
|
72
|
+
def stats
|
73
|
+
@_stats ||= fetch_stats
|
74
|
+
end
|
75
|
+
|
76
|
+
def fetch_questions
|
77
|
+
questions = responses_request(limit: 1).parsed_json['questions'] || []
|
78
|
+
questions.map { |hash| Question.from_hash(self, hash) }
|
79
|
+
end
|
80
|
+
|
81
|
+
def fetch_stats
|
82
|
+
stats_hash = responses_request(limit: 1)['stats']['responses']
|
83
|
+
Stats.from_stats_hash(stats_hash)
|
84
|
+
end
|
85
|
+
|
86
|
+
ResponsesRequest = Struct.new(:params, :response) do
|
87
|
+
# Check if we've got everything.
|
88
|
+
# @return Boolean
|
89
|
+
def last_request?
|
90
|
+
params['limit'] || responses_count < MAX_PAGE_SIZE
|
91
|
+
end
|
92
|
+
|
93
|
+
def responses_count
|
94
|
+
response.parsed_json['responses'].length
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# TODO: make this private once the implementation is solid.
|
99
|
+
#
|
100
|
+
# It looks like sometimes the Typeform API will report stats that are out-of-date relative to
|
101
|
+
# the responses it actually returns.
|
102
|
+
def responses_request(input_params = {})
|
103
|
+
params = input_params.dup
|
104
|
+
requests = [ResponsesRequest.new(params, @client.get('form/' + id, params))]
|
105
|
+
|
106
|
+
loop do
|
107
|
+
if requests.last.last_request?
|
108
|
+
break
|
109
|
+
else
|
110
|
+
next_params = requests.last.params.dup
|
111
|
+
next_params['offset'] += requests.last.responses_count
|
112
|
+
requests << ResponsesRequest.new(next_params, @client.get('form/' + id, next_params))
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
requests.map(&:response).map(&:parsed_json).reduce do |combined, next_set|
|
117
|
+
next_set.dup.tap { |response|
|
118
|
+
response['responses'] = combined['responses'] + next_set['responses']
|
119
|
+
}
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
private
|
124
|
+
|
125
|
+
# rubocop:disable Style/AccessorMethodName
|
126
|
+
def set_questions(questions_hashes = [])
|
127
|
+
@_questions = questions_hashes.map { |hash| Question.from_hash(self, hash) }
|
128
|
+
end
|
129
|
+
|
130
|
+
# @param Hash stats_hash of the form {"responses"=>{"showing"=>2, "total"=>2, "completed"=>0}}
|
131
|
+
def set_stats(stats_hash)
|
132
|
+
@_stats = Stats.from_stats_hash(stats_hash)
|
133
|
+
end
|
134
|
+
# rubocop:enable Style/AccessorMethodName
|
135
|
+
|
136
|
+
def collapse_and_validate_responses_params(input_params)
|
137
|
+
params = input_params.dup
|
138
|
+
|
139
|
+
params.keys.select { |key| key.is_a?(Symbol) }.each do |sym|
|
140
|
+
raise ::TypeformData::ArgumentError, 'Duplicate keys' if params.key?(sym.to_s)
|
141
|
+
params[sym.to_s] = params[key]
|
142
|
+
params.delete(key)
|
143
|
+
end
|
144
|
+
|
145
|
+
params.keys.each do |key|
|
146
|
+
next if PERMITTED_KEYS.key?(key) && params[key].is_a?(PERMITTED_KEYS[key])
|
147
|
+
raise ::TypeformData::ArgumentError, "Invalid/unsupported param: #{key}"
|
148
|
+
end
|
149
|
+
|
150
|
+
if params['limit'] && params['limit'] > MAX_PAGE_SIZE
|
151
|
+
raise ::TypeformData::ArgumentError, "The maximum limit is #{MAX_PAGE_SIZE}. You "\
|
152
|
+
"provided: #{params['limit']}"
|
153
|
+
end
|
154
|
+
|
155
|
+
params['offset'] ||= 0
|
156
|
+
params
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module TypeformData
|
3
|
+
end
|
4
|
+
|
5
|
+
require 'net/http'
|
6
|
+
require 'net/https'
|
7
|
+
require 'uri'
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
require 'typeform_data/version'
|
11
|
+
require 'typeform_data/client'
|
12
|
+
require 'typeform_data/errors'
|
13
|
+
|
14
|
+
require 'typeform_data/requestor'
|
15
|
+
require 'typeform_data/requestor/config'
|
16
|
+
require 'typeform_data/api_response'
|
17
|
+
|
18
|
+
require 'typeform_data/typeform'
|
19
|
+
require 'typeform_data/typeform/stats'
|
20
|
+
require 'typeform_data/typeform/response'
|
21
|
+
require 'typeform_data/typeform/question'
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'typeform_data/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
|
9
|
+
spec.name = 'typeform_data'
|
10
|
+
spec.version = TypeformData::VERSION
|
11
|
+
spec.authors = ['Max Wallace']
|
12
|
+
spec.email = ['maxfield.wallace@gmail.com']
|
13
|
+
|
14
|
+
spec.summary = 'An opinionated client for the Typeform.com Data API'
|
15
|
+
spec.description = 'typeform_data is a minimal, opinionated client for the Typeform.com Data '\
|
16
|
+
'API (see https://www.typeform.com/help/data-api/). The goal of this '\
|
17
|
+
'project is to create a maintainable, extensible client that provides a '\
|
18
|
+
"more natural object-oriented interface to Typeform.com's Data API."
|
19
|
+
|
20
|
+
spec.homepage = 'https://github.com/shearwater-intl/typeform_data'
|
21
|
+
spec.license = 'MIT'
|
22
|
+
|
23
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f|
|
24
|
+
f.match(%r{^(test|spec|features)/})
|
25
|
+
}
|
26
|
+
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
|
31
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
34
|
+
|
35
|
+
spec.add_development_dependency 'pry', '0.10'
|
36
|
+
spec.add_development_dependency 'pry-byebug', '3.3'
|
37
|
+
spec.add_development_dependency 'pry-doc', '0.8'
|
38
|
+
spec.add_development_dependency 'byebug', '8.2'
|
39
|
+
spec.add_development_dependency 'rubocop', '0.39'
|
40
|
+
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: typeform_data
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Max Wallace
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.10'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.10'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry-doc
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.8'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.8'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: byebug
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '8.2'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '8.2'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.39'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.39'
|
125
|
+
description: typeform_data is a minimal, opinionated client for the Typeform.com Data
|
126
|
+
API (see https://www.typeform.com/help/data-api/). The goal of this project is to
|
127
|
+
create a maintainable, extensible client that provides a more natural object-oriented
|
128
|
+
interface to Typeform.com's Data API.
|
129
|
+
email:
|
130
|
+
- maxfield.wallace@gmail.com
|
131
|
+
executables: []
|
132
|
+
extensions: []
|
133
|
+
extra_rdoc_files: []
|
134
|
+
files:
|
135
|
+
- ".gitignore"
|
136
|
+
- ".rubocop.yml"
|
137
|
+
- ".travis.yml"
|
138
|
+
- CHANGELOG.md
|
139
|
+
- CODE_OF_CONDUCT.md
|
140
|
+
- Gemfile
|
141
|
+
- LICENSE.txt
|
142
|
+
- README.md
|
143
|
+
- Rakefile
|
144
|
+
- bin/console
|
145
|
+
- bin/setup
|
146
|
+
- lib/typeform_data.rb
|
147
|
+
- lib/typeform_data/api_response.rb
|
148
|
+
- lib/typeform_data/client.rb
|
149
|
+
- lib/typeform_data/errors.rb
|
150
|
+
- lib/typeform_data/requestor.rb
|
151
|
+
- lib/typeform_data/requestor/config.rb
|
152
|
+
- lib/typeform_data/typeform.rb
|
153
|
+
- lib/typeform_data/typeform/question.rb
|
154
|
+
- lib/typeform_data/typeform/response.rb
|
155
|
+
- lib/typeform_data/typeform/stats.rb
|
156
|
+
- lib/typeform_data/version.rb
|
157
|
+
- typeform_data.gemspec
|
158
|
+
homepage: https://github.com/shearwater-intl/typeform_data
|
159
|
+
licenses:
|
160
|
+
- MIT
|
161
|
+
metadata: {}
|
162
|
+
post_install_message:
|
163
|
+
rdoc_options: []
|
164
|
+
require_paths:
|
165
|
+
- lib
|
166
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
|
+
requirements:
|
173
|
+
- - ">="
|
174
|
+
- !ruby/object:Gem::Version
|
175
|
+
version: '0'
|
176
|
+
requirements: []
|
177
|
+
rubyforge_project:
|
178
|
+
rubygems_version: 2.5.1
|
179
|
+
signing_key:
|
180
|
+
specification_version: 4
|
181
|
+
summary: An opinionated client for the Typeform.com Data API
|
182
|
+
test_files: []
|
183
|
+
has_rdoc:
|