google_forms 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/google_form.rb +110 -0
- data/lib/google_form_input.rb +17 -0
- data/lib/google_forms.rb +1 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ac8065484bf64be707159c0525e865336672991ed37325d7a9bf7e02bf4cd16f
|
4
|
+
data.tar.gz: 22555253d4c4ed60939e7448784303474873caaa57e6f103d8e1815b38879faa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 14121db7fa919144dec6ec5115d425825fd7d22745f4fc4f070405b6aa1ad1010c1554cc9da70a171e5a07cccc77eb27cfba4aff364cf41d6c9e9fc048cf59f5
|
7
|
+
data.tar.gz: 030c0043b60eb14665862e73e27566523fe5c95f42d10e46b31f63974881cd52a870917bb55bff8f527df1b43ce11261b9b72ea289552bea9d4aa3f11c21c7fb
|
data/lib/google_form.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'curb'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
require_relative 'google_form_input'
|
5
|
+
|
6
|
+
class GoogleForm
|
7
|
+
BASE_URL = 'https://docs.google.com/forms'
|
8
|
+
|
9
|
+
def initialize(google_form_url_or_id)
|
10
|
+
@id = if is_an_url?(google_form_url_or_id)
|
11
|
+
form_id_from_url(google_form_url_or_id)
|
12
|
+
else
|
13
|
+
google_form_url_or_id
|
14
|
+
end
|
15
|
+
|
16
|
+
define_input_methods
|
17
|
+
end
|
18
|
+
|
19
|
+
def post(data = {})
|
20
|
+
params_for_curb = params_from_data(data).merge(params_from_inputs.compact)
|
21
|
+
|
22
|
+
Curl.post(form_response_url, params_for_curb)
|
23
|
+
end
|
24
|
+
|
25
|
+
def inputs
|
26
|
+
input_nodes = nokogiri_doc
|
27
|
+
.css('form input')
|
28
|
+
.reject { |i| i.attribute('aria-label').nil? }
|
29
|
+
|
30
|
+
@inputs ||= input_nodes.map do |i|
|
31
|
+
GoogleFormInput.new(
|
32
|
+
i.attribute('type').value,
|
33
|
+
i.attribute('aria-label').value,
|
34
|
+
i.attribute('name').value
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def params_from_data(data)
|
40
|
+
data.map { |k, v| [self.send(k).name, v] }.to_h
|
41
|
+
end
|
42
|
+
|
43
|
+
def params_from_inputs
|
44
|
+
inputs.map(&:to_param).reduce({}, :merge)
|
45
|
+
end
|
46
|
+
|
47
|
+
def form_body
|
48
|
+
@form_body ||= Curl.get(view_form_url).body
|
49
|
+
end
|
50
|
+
|
51
|
+
def nokogiri_doc
|
52
|
+
@nokogiri_doc ||= Nokogiri::HTML(form_body)
|
53
|
+
end
|
54
|
+
|
55
|
+
def form_response_url
|
56
|
+
"#{BASE_URL}/u/0/d/e/#{@id}/formResponse"
|
57
|
+
end
|
58
|
+
|
59
|
+
def view_form_url
|
60
|
+
"#{BASE_URL}/d/e/#{@id}/viewform"
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_s
|
64
|
+
inspect
|
65
|
+
end
|
66
|
+
|
67
|
+
def inspect
|
68
|
+
hidden_variables = %i[@inputs @form_body @nokogiri_doc]
|
69
|
+
|
70
|
+
variables_to_string =
|
71
|
+
(self.instance_variables - hidden_variables).map do |variable|
|
72
|
+
"#{variable}=\\\"#{instance_variable_get(variable)}\\\""
|
73
|
+
end
|
74
|
+
|
75
|
+
"#<#{self.class}:#{object_id} #{variables_to_string.join(', ')}>"
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def define_input_methods
|
81
|
+
inputs.each do |input|
|
82
|
+
self.class.send(:define_method, input.label.to_sym) do
|
83
|
+
input
|
84
|
+
end
|
85
|
+
|
86
|
+
self.class.send(:define_method, "#{input.label.to_sym}=") do |*args|
|
87
|
+
input.value = args.first
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def form_id_from_url(form_url)
|
93
|
+
response = Curl.get(form_url)
|
94
|
+
|
95
|
+
if response.response_code == 302
|
96
|
+
return form_id_from_viewform_url(response.redirect_url)
|
97
|
+
end
|
98
|
+
|
99
|
+
@form_body ||= response.body
|
100
|
+
form_id_from_viewform_url(form_url)
|
101
|
+
end
|
102
|
+
|
103
|
+
def form_id_from_viewform_url(url)
|
104
|
+
url.match(/docs.google.com\/forms\/d\/e\/(.*)\/viewform/)[1]
|
105
|
+
end
|
106
|
+
|
107
|
+
def is_an_url?(string)
|
108
|
+
string =~ /\A#{URI::regexp(['http', 'https'])}\z/
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class GoogleFormInput
|
2
|
+
attr_reader :input_type, :label, :name, :value
|
3
|
+
|
4
|
+
def initialize(input_type, label, name)
|
5
|
+
@input_type = input_type
|
6
|
+
@label = label
|
7
|
+
@name = name
|
8
|
+
end
|
9
|
+
|
10
|
+
def value=(value)
|
11
|
+
@value = value
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_param
|
15
|
+
{@name => @value}
|
16
|
+
end
|
17
|
+
end
|
data/lib/google_forms.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'google_form'
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_forms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vadim Venediktov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: curb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
description:
|
42
|
+
email: install.vv@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/google_form.rb
|
48
|
+
- lib/google_form_input.rb
|
49
|
+
- lib/google_forms.rb
|
50
|
+
homepage: https://github.com/installero/google_forms_ruby
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.1.2
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Posting to Google Forms from your ruby scripts
|
73
|
+
test_files: []
|