tofulcrum 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZWQ4YWFmZjc0MzJiZGZkMTkyODhlNTc4YjQxZTU2YmZjNDFkZThjMw==
5
+ data.tar.gz: !binary |-
6
+ Mzg3ODkxOTU3Yzg2MDYzZWYwZjMwOTVlYzI0NDY0YzhjYWNmODY2NA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjBhNjc5OTdiMWRlNzVjMzU3NjBkNmRkMzlkNzJkMTUxYzAxNmU4NjVkYzFl
10
+ NmM0MjkwMWRlNTc2YmMyZDE1MjZhMDgyMTRkZDdiYzQ3OTAwMDAzZTM1YzRl
11
+ YzkxNzY5MThjOTVmMzk5YjBhZDc2YTZkNWRlYmUxMmNkYmFhZDA=
12
+ data.tar.gz: !binary |-
13
+ NjQwYjEwMGIzMTQ5MmYzZmRiOGE2YjRkNzUwNTU5NWVhMjA2NjI4MmYzZTZk
14
+ ZjQ0MjA5YjcxNjc5NTc5Y2E3MDcxMGZiY2E3N2E2ZGY1Yzc5MDg2YTBmODYx
15
+ ZDY3NjQ1YTdiODMxMWQ1NmI3YjViMDYyY2RkYmEzOTMxODdjMzc=
data/.gitignore ADDED
@@ -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,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Zac McCormick
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.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # tofulcrum
2
+
3
+ Import CSV data into an existing Fulcrum app from the command line.
4
+
5
+ ## Installation
6
+
7
+ $ gem install tofulcrum
8
+
9
+ ## Usage
10
+
11
+ $ tofulcrum import somefile.csv <form_id> <api_key>
12
+
13
+ ## Notes
14
+
15
+ It will automatically map the column headers to the data names in the form. If any column is not found, it will fail. So your CSV
16
+ must only contain columns with valid fields in the form. Right now this program only supports text fields and choice fields.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/tofulcrum ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+ bin_file = Pathname.new(__FILE__).realpath
5
+
6
+ $:.unshift File.expand_path("../../lib", bin_file)
7
+
8
+ require 'tofulcrum'
9
+
10
+ Tofulcrum::CLI.start(ARGV)
data/lib/tofulcrum.rb ADDED
@@ -0,0 +1,111 @@
1
+ require "tofulcrum/version"
2
+ require 'csv'
3
+ require 'fulcrum'
4
+ require 'thor'
5
+
6
+ module Tofulcrum
7
+ class CLI < Thor
8
+ desc "import", "Import a CSV into a Fulcrum app"
9
+ def import(file, form_id, api_key, mapping=nil)
10
+
11
+ Fulcrum::Api.configure do |config|
12
+ config.uri = 'https://api.fulcrumapp.com/api/v2'
13
+ config.key = api_key
14
+ end
15
+
16
+ row_index = 0
17
+
18
+ lat_index = nil
19
+ lon_index = nil
20
+ column_mapping = []
21
+
22
+ CSV.foreach(file) do |row|
23
+ is_header = row_index == 0
24
+
25
+ if is_header
26
+ lat_index, lon_index = *find_geo_columns(row).compact
27
+ raise 'Unable to find latitude/longitude columns' unless lat_index && lon_index
28
+
29
+ lat_name = row[lat_index]
30
+ lon_name = row[lon_index]
31
+
32
+ headers = row.clone
33
+ headers.delete_if {|v| [lat_name, lon_name].include?(v.downcase)}
34
+
35
+ column_mapping = find_mapping_columns(form_id, headers, row, mapping)
36
+ else
37
+
38
+ form_values = {}
39
+
40
+ column_mapping.each do |map|
41
+ value = nil
42
+
43
+ case map[:field]['type']
44
+ when 'ChoiceField'
45
+ value = { choice_values: row[map[:index]].split(',') }
46
+ else
47
+ value = row[map[:index]]
48
+ end
49
+
50
+ form_values[map[:field]['key']] = value
51
+ end
52
+
53
+ record = {
54
+ record: {
55
+ form_id: form_id,
56
+ latitude: row[lat_index].to_f,
57
+ longitude: row[lon_index].to_f,
58
+ form_values: form_values
59
+ }
60
+ }
61
+
62
+ Fulcrum::Record.create(record)
63
+ end
64
+
65
+ row_index += 1
66
+
67
+ print "#{row_index.to_s.rjust(10, ' ')} records uploaded\r"
68
+ end
69
+ end
70
+
71
+ no_tasks do
72
+ def flatten_elements(elements)
73
+ [].tap do |all|
74
+ elements.each do |element|
75
+ all << element
76
+ all += flatten_elements(element['elements']) if element['elements']
77
+ end
78
+ end
79
+ end
80
+
81
+ def find_mapping_columns(form_id, headers, full_row, mapping)
82
+ form = Fulcrum::Form.find(form_id, {})['form']
83
+
84
+ elements = flatten_elements(form['elements'])
85
+
86
+ if mapping.nil?
87
+ mapping = headers.map {|h| "#{h}=#{h}"}.join(',')
88
+ end
89
+
90
+ [].tap do |map|
91
+ mapping.split(',').each do |pair|
92
+ source, dest = *pair.split('=').map(&:strip)
93
+
94
+ source_index = full_row.index {|h| h.downcase == source.downcase} rescue nil
95
+ dest_field = elements.find {|e| e['data_name'].downcase == dest.downcase} rescue nil
96
+
97
+ raise "Unable to map column #{source} to form field #{dest}." unless source_index && dest_field
98
+
99
+ map << { index: source_index, field: dest_field }
100
+ end
101
+ end
102
+ end
103
+
104
+ def find_geo_columns(headers)
105
+ lat_columns = ['lat', 'latitude', 'y']
106
+ lon_columns = ['lon', 'long', 'longitude', 'x']
107
+ [headers.index {|h| lat_columns.include?(h.downcase)}, headers.index {|h| lon_columns.include?(h.downcase)}]
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,3 @@
1
+ module Tofulcrum
2
+ VERSION = "0.0.1"
3
+ end
data/tofulcrum.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tofulcrum/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tofulcrum"
8
+ spec.version = Tofulcrum::VERSION
9
+ spec.authors = ["Zac McCormick"]
10
+ spec.email = ["zac.mccormick@gmail.com"]
11
+ spec.description = %q{Convert data to Fulcrum}
12
+ spec.summary = %q{Import data into Fulcrum from a CSV}
13
+ spec.homepage = "https://github.com/zhm/tofulcrum"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thor"
22
+ spec.add_dependency "fulcrum"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tofulcrum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Zac McCormick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fulcrum
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Convert data to Fulcrum
70
+ email:
71
+ - zac.mccormick@gmail.com
72
+ executables:
73
+ - tofulcrum
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/tofulcrum
83
+ - lib/tofulcrum.rb
84
+ - lib/tofulcrum/version.rb
85
+ - tofulcrum.gemspec
86
+ homepage: https://github.com/zhm/tofulcrum
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.1.5
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Import data into Fulcrum from a CSV
110
+ test_files: []
111
+ has_rdoc: