Class: GoogleForm
- Inherits:
-
Object
- Object
- GoogleForm
- Defined in:
- lib/google_form.rb
Constant Summary collapse
- BASE_URL =
'https://docs.google.com/forms'
Instance Method Summary collapse
- #form_body ⇒ Object
- #form_response_url ⇒ Object
-
#initialize(google_form_url_or_id) ⇒ GoogleForm
constructor
A new instance of GoogleForm.
- #inputs ⇒ Object
- #inspect ⇒ Object
- #nokogiri_doc ⇒ Object
- #params_from_data(data) ⇒ Object
- #params_from_inputs ⇒ Object
- #post(data = {}) ⇒ Object
- #to_s ⇒ Object
- #view_form_url ⇒ Object
Constructor Details
#initialize(google_form_url_or_id) ⇒ GoogleForm
Returns a new instance of GoogleForm.
9 10 11 12 13 14 15 16 17 |
# File 'lib/google_form.rb', line 9 def initialize(google_form_url_or_id) @id = if is_an_url?(google_form_url_or_id) form_id_from_url(google_form_url_or_id) else google_form_url_or_id end define_input_methods end |
Instance Method Details
#form_body ⇒ Object
49 50 51 |
# File 'lib/google_form.rb', line 49 def form_body @form_body ||= Curl.get(view_form_url).body end |
#form_response_url ⇒ Object
57 58 59 |
# File 'lib/google_form.rb', line 57 def form_response_url "#{BASE_URL}/u/0/d/e/#{@id}/formResponse" end |
#inputs ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/google_form.rb', line 25 def inputs input_nodes = nokogiri_doc .css('form input') .reject { |i| i.attribute('type').value == 'hidden' } @inputs ||= input_nodes.map do |i| container = i.ancestors('.freebirdFormviewerViewNumberedItemContainer') GoogleFormInput.new( i.attribute('type').value, container.css('.freebirdFormviewerComponentsQuestionBaseHeader').text, "entry.#{container.css('[@jsmodel]').attribute('data-params').value[/\[.*\]/].delete('[]').split(',')[4]}" ) end end |
#inspect ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/google_form.rb', line 69 def inspect hidden_variables = %i[@inputs @form_body @nokogiri_doc] variables_to_string = (self.instance_variables - hidden_variables).map do |variable| "#{variable}=\\\"#{instance_variable_get(variable)}\\\"" end "#<#{self.class}:#{object_id} #{variables_to_string.join(', ')}>" end |
#nokogiri_doc ⇒ Object
53 54 55 |
# File 'lib/google_form.rb', line 53 def nokogiri_doc @nokogiri_doc ||= Nokogiri::HTML(form_body) end |
#params_from_data(data) ⇒ Object
41 42 43 |
# File 'lib/google_form.rb', line 41 def params_from_data(data) data.map { |k, v| [self.send(k).name, v] }.to_h end |
#params_from_inputs ⇒ Object
45 46 47 |
# File 'lib/google_form.rb', line 45 def params_from_inputs inputs.map(&:to_param).reduce({}, :merge) end |
#post(data = {}) ⇒ Object
19 20 21 22 23 |
# File 'lib/google_form.rb', line 19 def post(data = {}) params_for_curb = params_from_data(data).merge(params_from_inputs.compact) Curl.post(form_response_url, params_for_curb) end |
#to_s ⇒ Object
65 66 67 |
# File 'lib/google_form.rb', line 65 def to_s inspect end |
#view_form_url ⇒ Object
61 62 63 |
# File 'lib/google_form.rb', line 61 def view_form_url "#{BASE_URL}/d/e/#{@id}/viewform" end |