jsontodb 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8bf558bbc5f2e98a9d2648095d8cc0c9750a15ad
4
+ data.tar.gz: 96c354df7cb34e5ceb8a8a6362deb218f7302626
5
+ SHA512:
6
+ metadata.gz: 6b7e764045cd9baeafd38b7d398770831af201bbfdaf1a5d400d139c6ed6509bf9719b387eb6f1b03bce84d3a57aa85d5cd72b3495d9d5cf5c4b15ea6429e5a8
7
+ data.tar.gz: f3b5e93072b9ceb9882d81d2ae40f3d0dc91cba77b39a2204bd3b5d0df0746f065407d66ee6cbdd8c3e84815d2d4409d60a4d96032763c42fda58778aef383af
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Christopher Lutz
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.
File without changes
@@ -0,0 +1,8 @@
1
+ #!/user/bin/env ruby
2
+ STDOUT.sync = true
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '/../lib'))
5
+
6
+ require 'jsontodb'
7
+
8
+ JSONtoDB::CLI.run
@@ -0,0 +1,30 @@
1
+ # This library provides functionality to perform REST operations from the
2
+ # command-line. It also provides the capability to use JSON in HTTP requests.
3
+ #
4
+ # Licensed under the MIT License:
5
+ #
6
+ # Copyright (c) 2016 Christopher Lutz
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files (the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be included in all
16
+ # copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ # SOFTWARE.
25
+ module JSONtoDB
26
+ autoload :CLI, 'jsontodb/cli'
27
+ autoload :IO, 'jsontodb/io'
28
+ autoload :Processor, 'jsontodb/processor'
29
+ autoload :REST, 'jsontodb/rest'
30
+ end
@@ -0,0 +1,58 @@
1
+ require 'io/console'
2
+ require 'base64'
3
+
4
+ module JSONtoDB
5
+ # This module handles the command-line interface for JSONtoDB
6
+ module CLI
7
+ module_function
8
+
9
+ def run
10
+ if !ARGV.empty?
11
+ if ARGV.length >= 2
12
+ authentication_credentials(ARGV.shift, ARGV.shift)
13
+ if ARGV.empty?
14
+ continuous_cli
15
+ else
16
+ JSONtoDB::Processor.run_command(ARGV, @user, @pass)
17
+ end
18
+ end
19
+ else
20
+ authentication_credentials
21
+ continuous_cli
22
+ end
23
+ end
24
+
25
+ def authentication_credentials(user = nil, pass = nil)
26
+ if user.nil? || pass.nil?
27
+ puts 'USER AUTHENTICATION'
28
+ puts '==================='
29
+ puts
30
+ print 'Username: '
31
+ @user = STDIN.gets.chomp
32
+ print 'Password: '
33
+ @pass = STDIN.noecho(&:gets).chomp
34
+ puts
35
+ puts
36
+ else
37
+ @user = user
38
+ @pass = pass
39
+ end
40
+ end
41
+
42
+ def continuous_cli
43
+ print 'jsontodb> '
44
+ input = STDIN.gets.chomp
45
+ while input != 'exit'
46
+ JSONtoDB::Processor.run_command(tokenize_command(input), @user, @pass)
47
+ print 'jsontodb> '
48
+ input = STDIN.gets.chomp
49
+ end
50
+ end
51
+
52
+ def tokenize_command(input)
53
+ input.split(/\s(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/)
54
+ .select { |s| !s.empty? }
55
+ .map { |s| s.gsub(/(^ +)|( +$)|(^["']+)|(["']+$)/, '') }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,14 @@
1
+ require 'json'
2
+
3
+ module JSONtoDB
4
+ # This module handles input/output operations
5
+ # for the library
6
+ module IO
7
+ module_function
8
+
9
+ def read(src)
10
+ hash = JSON.parse(File.read(src))
11
+ hash
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,36 @@
1
+ module JSONtoDB
2
+ # This module processes commands passed to the
3
+ # command-line interface
4
+ module Processor
5
+ module_function
6
+
7
+ def run_command(args, user, pass)
8
+ return if args.empty?
9
+ command = args.shift
10
+
11
+ case command
12
+ when 'get'
13
+ return unless check_args('put', 1, args)
14
+ res = JSONtoDB::REST.get(args[0], user, pass)
15
+ puts res
16
+ when 'delete'
17
+ return unless check_args('put', 1, args)
18
+ JSONtoDB::REST.delete(args[0], user, pass)
19
+ when 'put'
20
+ return unless check_args('put', 2, args)
21
+ JSONtoDB::REST.put(args[0], args[1], user, pass)
22
+ when 'post'
23
+ return unless check_args('put', 2, args)
24
+ JSONtoDB::REST.post(args[0], args[1], user, pass)
25
+ else
26
+ puts "Unknown command '#{command}'."
27
+ end
28
+ end
29
+
30
+ def check_args(command, expected, actual)
31
+ return true if actual.length == expected
32
+ puts "ERROR: Command '#{command}' expected #{expected} parameter(s) but received #{actual.length}!"
33
+ false
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,67 @@
1
+ require 'net/http'
2
+
3
+ module JSONtoDB
4
+ # This module handles REST requests
5
+ module REST
6
+ module_function
7
+
8
+ def get(url, user, pass)
9
+ uri = URI(url)
10
+ request = Net::HTTP::Get.new(uri)
11
+ request.basic_auth(user, pass)
12
+
13
+ response = Net::HTTP.start(uri.hostname, uri.port) do |http|
14
+ http.request(request)
15
+ end
16
+
17
+ handle_errors(response)
18
+ end
19
+
20
+ def delete(url, user, pass)
21
+ uri = URI(url)
22
+ request = Net::HTTP::Delete.new(uri)
23
+ request.basic_auth(user, pass)
24
+
25
+ response = Net::HTTP.start(uri.hostname, uri.port) do |http|
26
+ http.request(request)
27
+ end
28
+
29
+ handle_errors(response)
30
+ end
31
+
32
+ def put(url, src, user, pass)
33
+ uri = URI(url)
34
+ request = Net::HTTP::Put.new(uri)
35
+ request.basic_auth(user, pass)
36
+ request.content_type = 'application/json'
37
+ request.set_form_data(JSONtoDB::IO.read(src))
38
+
39
+ response = Net::HTTP.start(uri.hostname, uri.port) do |http|
40
+ http.request(request)
41
+ end
42
+
43
+ handle_errors(response)
44
+ end
45
+
46
+ def post(url, src, user, pass)
47
+ uri = URI(url)
48
+ request = Net::HTTP::Post.new(uri)
49
+ request.basic_auth(user, pass)
50
+ request.content_type = 'application/json'
51
+ request.set_form_data(JSONtoDB::IO.read(src))
52
+
53
+ response = Net::HTTP.start(uri.hostname, uri.port) do |http|
54
+ http.request(request)
55
+ end
56
+
57
+ handle_errors(response)
58
+ end
59
+
60
+ def handle_errors(response)
61
+ return response.body if response.code.start_with?('20')
62
+
63
+ puts "ERROR: '#{response.code}' response. (#{response.message} - #{response.class.name})"
64
+ ''
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,6 @@
1
+ module JSONtoDB
2
+ # This module holds the library's version information
3
+ module Version
4
+ STRING = '1.0.0'.freeze
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsontodb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Lutz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - lutzblox@gmail.com
16
+ executables:
17
+ - jsontodb
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE.txt
22
+ - README.md
23
+ - bin/jsontodb
24
+ - lib/jsontodb.rb
25
+ - lib/jsontodb/cli.rb
26
+ - lib/jsontodb/io.rb
27
+ - lib/jsontodb/processor.rb
28
+ - lib/jsontodb/rest.rb
29
+ - lib/jsontodb/version.rb
30
+ homepage: https://github.com/HHS-SpecialTopics/jsontodb
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '1.9'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.6.6
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: A Ruby library to perform REST requests from the command-line and insert
54
+ JSON into databases
55
+ test_files: []