gfrom 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA512:
3
+ data.tar.gz: c5e46b00558b4cd0f36cbd4ef302751b2984574f259733341351e1da399333efb831f2471f9525d817520a90647b5c2fbe88d4bb7ad1f520745509552c01423d
4
+ metadata.gz: f42dc1217f90ab3e1d7cb92ec95e3c504c85bbb52b8ff4e863e214e759080ecdb74c511a4f024913850657e488c1fe5c6a89a948de60cf010729601dec4577f0
5
+ SHA1:
6
+ data.tar.gz: 600f5cb31e561a45f978e5fadb510e46efae1d66
7
+ metadata.gz: 64144fa40594f506a47709c4e494e9d860409c04
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in google_form.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Marvin Marcelo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.platform = Gem::Platform::RUBY
7
+ spec.name = "gfrom"
8
+ spec.version = "0.1.0"
9
+ spec.authors = ["Marvin Marcelo"]
10
+ spec.email = ["mrclmrvn@gmail.com"]
11
+ spec.description = %q{Retrieve Google form and fields from a given form URL into array of hashes}
12
+ spec.summary = %q{Google Forms Renderer}
13
+ spec.homepage = "http://www.marvinmarcelo.com"
14
+ spec.license = "MIT"
15
+ spec.date = '2013-05-01'
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+ spec.require_path = "lib"
22
+
23
+ spec.add_dependency "curb", ">= 0"
24
+ spec.add_dependency "nokogiri", ">= 0"
25
+ spec.add_dependency "json", ">= 0"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.3"
28
+ spec.add_development_dependency "rake"
29
+ end
30
+
@@ -0,0 +1,72 @@
1
+ require "curb"
2
+ require "nokogiri"
3
+ require "json"
4
+ require 'digest/sha1'
5
+ require "tmpdir"
6
+
7
+ class Gfrom
8
+
9
+ attr_accessor :form, :fields
10
+
11
+ MATCHERS = '//input[@type="text"] | //input[@type="hidden"] | //textarea | //form'
12
+
13
+ def initialize(url, reset = false)
14
+ @form = Hash.new
15
+ @fields = []
16
+
17
+ cache = "#{Dir.tmpdir}/#{Digest::SHA1.hexdigest(url)}"
18
+ File.delete cache if reset
19
+
20
+ unless File.exists?(cache)
21
+ req = Curl.get(url)
22
+ File.open(cache, "w") do |f|
23
+ f.write req.body_str
24
+ end
25
+ end
26
+
27
+ keys = []
28
+
29
+ doc = Nokogiri::XML(File.open(cache))
30
+ doc.search(MATCHERS).each do |node|
31
+ case node.name
32
+ when "form"
33
+ @form[:action] = node.attributes["action"].value unless @form.has_key?(:action)
34
+ else
35
+ n = hash_it(node)
36
+ @fields << n
37
+ keys << n[:name]
38
+ end
39
+ end
40
+ @form[:keys] = keys.uniq
41
+
42
+ end
43
+
44
+ def submit(params)
45
+ response = Curl.post(@form[:action], params)
46
+ doc = Nokogiri::XML.parse(response.body_str)
47
+ errorheader = doc.search('//div[@class="errorheader"]')
48
+ success = errorheader.empty?
49
+ out = { :success => success, :message => doc.search('//title').first.text.strip }
50
+ unless success
51
+ out[:message] = errorheader.children.first.text.strip
52
+ end
53
+ out
54
+ end
55
+
56
+ private
57
+
58
+ def hash_it(node)
59
+ object = Hash.new
60
+ object[:element] = node.name
61
+ node.attributes.each do |k,v|
62
+ object[k.to_sym] = v.value
63
+ if k == "id"
64
+ label = node.search("//label[@for=\"#{v.value}\"]").first
65
+ object[:label] = label.children.first.text.strip if label
66
+ object[:required] = true unless label.search("//label[@for=\"#{v.value}\"]/span[contains(@class, \"required\")]").empty?
67
+ end
68
+ end
69
+ object
70
+ end
71
+
72
+ end
@@ -0,0 +1,38 @@
1
+ # Gfrom (From Google Form)
2
+
3
+ Render unathenticated Google Form in your website. Useful for collecting data from your users without a database. Just create a google form from your google account
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gfrom'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gfrom
18
+
19
+ ## Usage
20
+
21
+ GOOGLE_FORM_URL = "https://docs.google.com/a/myorganization.com/spreadsheet/embeddedform?formkey=dGlXS0ZNWVVGYWZqMVhXUENvOXQtSnc6MQ&hl=en"
22
+ @myform = Gfrom.new(GOOGLE_FORM_URL)
23
+ # fields are stored in array of hashes
24
+ # @fields=[
25
+ # {:element=>"input", :type=>"text", :name=>"entry.0.single", :value=>"", :class=>"ss-q-short", :id=>"entry_0", :label=>"Name", :required=>true},
26
+ # {:element=>"input", :type=>"text", :name=>"entry.2.single", :value=>"", :class=>"ss-q-short", :id=>"entry_2", :label=>"Email", :required=>true},
27
+ # {:element=>"textarea", :name=>"entry.1.single", :rows=>"8", :cols=>"75", :class=>"ss-q-long", :id=>"entry_1", :label=>"Message"},
28
+ # {:element=>"input", :type=>"hidden", :name=>"pageNumber", :value=>"0"}, {:element=>"input", :type=>"hidden", :name=>"backupCache", :value=>""}
29
+ # ]
30
+ puts @myform.fields
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
File without changes
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gfrom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marvin Marcelo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2013-05-01 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: curb
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - &id002
20
+ - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :runtime
24
+ version_requirements: *id001
25
+ - !ruby/object:Gem::Dependency
26
+ name: nokogiri
27
+ prerelease: false
28
+ requirement: &id003 !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - *id002
31
+ type: :runtime
32
+ version_requirements: *id003
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ prerelease: false
36
+ requirement: &id004 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - *id002
39
+ type: :runtime
40
+ version_requirements: *id004
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ prerelease: false
44
+ requirement: &id005 !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: "1.3"
49
+ type: :development
50
+ version_requirements: *id005
51
+ - !ruby/object:Gem::Dependency
52
+ name: rake
53
+ prerelease: false
54
+ requirement: &id006 !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - *id002
57
+ type: :development
58
+ version_requirements: *id006
59
+ description: Retrieve Google form and fields from a given form URL into array of hashes
60
+ email:
61
+ - mrclmrvn@gmail.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files: []
67
+
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - Rakefile
73
+ - gfrom.gemspec
74
+ - lib/gfrom.rb
75
+ - readme.md
76
+ - spec/.gitkeep
77
+ homepage: http://www.marvinmarcelo.com
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+
82
+ post_install_message:
83
+ rdoc_options: []
84
+
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - *id002
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - *id002
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Google Forms Renderer
100
+ test_files:
101
+ - spec/.gitkeep