csv-curl 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.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +52 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +1 -0
  5. data/bin/csv-curl +139 -0
  6. data/csv-curl.gemspec +15 -0
  7. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d04f4437667318ac6a0d869e1ebbc6b72de7d6d1c86ecd55b98dd8d74ae79a5b
4
+ data.tar.gz: 944c0ef6d3837bbd38e4d1bd11b4e3d36fab95274452cb121f9440e34b0f9748
5
+ SHA512:
6
+ metadata.gz: c432715c775b43c27f38b6e21b9410a934ce87a528a17c32f86ac906de98185d17e3e84bcb700dab4f767d7fae4f0dd3df4eda5cdb475e200e785db5f5fa0b01
7
+ data.tar.gz: db278224c3a4e4d1f702de80495f4758796916ac69ca363cebe9c94cd580948ec8a3adae0315994f4d62f3c5126831519ddac915cc21fabf59b43aab9a847035
data/.gitignore ADDED
@@ -0,0 +1,52 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+
52
+ *~
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Douglas Youch
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # csv-curl
data/bin/csv-curl ADDED
@@ -0,0 +1,139 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'csv'
4
+ require 'json'
5
+ require 'cgi'
6
+ require 'base64'
7
+ require 'shellwords'
8
+ require 'optparse'
9
+ require 'digest/md5'
10
+
11
+ options = {
12
+ template_file: nil,
13
+ csv_file: nil,
14
+ response_filter: nil,
15
+ default_format: 'json'
16
+ }
17
+
18
+ OptionParser.new do |opts|
19
+ opts.banner = "Usage: " + File.basename(__FILE__) + " [CURL OPTIONS]"
20
+
21
+ opts.on('--template REQUEST', 'Request template') do |v|
22
+ options[:template_file] = v
23
+ end
24
+
25
+ opts.on('--csv CSV_FILE', 'Request data') do |v|
26
+ options[:csv_file] = v
27
+ end
28
+
29
+ opts.on('--response-filter COMMAND', 'Command to run on each response') do |v|
30
+ options[:response_filter] = v
31
+ end
32
+ end.parse!
33
+
34
+ raise('no csv input file specified') unless options[:csv_file]
35
+ raise("csv input file #{options[:csv_file]} not found") unless File.exist?(options[:csv_file])
36
+
37
+ raise("template file #{options[:template_file]} not found") if options[:template_file] && !File.exist?(options[:template_file])
38
+
39
+ template = options[:template_file] ? File.read(options[:template_file]) : nil
40
+ input = CSV.open(options[:csv_file])
41
+ input_headers = input.shift
42
+
43
+ required_replacements = ARGV.map do |arg|
44
+ arg.scan(/\{\{(.*?)\}\}/)
45
+ end
46
+
47
+ required_replacements += options[:response_filter].scan(/\{\{(.*?)\}\}/) if options[:response_filter]
48
+ required_replacements += template.scan(/\{\{(.*?)\}\}/) if template
49
+ required_replacements.flatten!
50
+ required_replacements.map! { |v| v.split(':', 2).last }
51
+ required_replacements.uniq!
52
+
53
+ missing_replacements = required_replacements - input_headers
54
+ raise("missing replacement values for #{missing_replacements.join(', ')}") unless missing_replacements.empty?
55
+
56
+ TMP_REQUEST_BODY_FILE = 'csv-utils.request'
57
+
58
+ def format_value(value, format)
59
+ case format
60
+ when 'json'
61
+ value = value.to_json
62
+ value.sub!(/\A"/, '')
63
+ value.sub!(/"\z/, '')
64
+ value
65
+ when 'query',
66
+ 'param'
67
+ CGI.escape(value)
68
+ when 'base64'
69
+ Base64.strict_encode64(value)
70
+ when 'hex'
71
+ value.unpack('H*').first
72
+ when 'shellword',
73
+ 'shell'
74
+ Shellword.escape(value)
75
+ else
76
+ value
77
+ end
78
+ end
79
+
80
+ def generate_string(str, data)
81
+ str.gsub(/\{\{(.*?)\}\}/) do
82
+ format, key =
83
+ if $1.include?(':')
84
+ $1.split(':', 2)
85
+ else
86
+ [nil, $1]
87
+ end
88
+
89
+ format_value(data[key], format)
90
+ end
91
+ end
92
+
93
+ def run_command_safely(cmd)
94
+ res = `#{cmd}`
95
+ raise("failed to run command: #{cmd}") unless $?.success?
96
+ res
97
+ end
98
+
99
+ def build_curl_command(curl_args, data, request_file, response_file)
100
+ cmd = "curl -s"
101
+ cmd += " -d@#{request_file} " if request_file
102
+ cmd += ' ' + curl_args.map { |arg| Shellwords.escape(generate_string(arg, data)) }.join(' ')
103
+ cmd += " > #{response_file}"
104
+ cmd
105
+ end
106
+
107
+ def build_response_filter_command(response_filter, data, response_file)
108
+ cmd = "cat #{response_file} | "
109
+ cmd += generate_string(response_filter, data)
110
+ cmd
111
+ end
112
+
113
+ while (row = input.shift)
114
+ data = Hash[input_headers.zip(row)]
115
+
116
+ request_file =
117
+ if template
118
+ request_body = generate_string(template, data)
119
+ File.open(TMP_REQUEST_BODY_FILE, 'wb') { |f| f.write request_body }
120
+ TMP_REQUEST_BODY_FILE
121
+ else
122
+ nil
123
+ end
124
+
125
+ response_file = "csv-utils.response"
126
+
127
+ cmd = build_curl_command(ARGV, data, request_file, response_file)
128
+ run_command_safely(cmd)
129
+
130
+ output =
131
+ if options[:response_filter]
132
+ cmd = build_response_filter_command(options[:response_filter], data, response_file)
133
+ run_command_safely(cmd)
134
+ else
135
+ File.read(response_file)
136
+ end
137
+
138
+ puts output
139
+ end
data/csv-curl.gemspec ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'csv-curl'
5
+ s.version = '0.1.0'
6
+ s.licenses = ['MIT']
7
+ s.summary = 'CSV Curl'
8
+ s.description = 'Tools making mulitple calls using curl'
9
+ s.authors = ['Doug Youch']
10
+ s.email = 'dougyouch@gmail.com'
11
+ s.homepage = 'https://github.com/dougyouch/csv-curl'
12
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
13
+ s.bindir = 'bin'
14
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv-curl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Doug Youch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Tools making mulitple calls using curl
14
+ email: dougyouch@gmail.com
15
+ executables:
16
+ - csv-curl
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - LICENSE.txt
22
+ - README.md
23
+ - bin/csv-curl
24
+ - csv-curl.gemspec
25
+ homepage: https://github.com/dougyouch/csv-curl
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.0.3
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: CSV Curl
48
+ test_files: []