jsontodb 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +72 -0
- data/bin/jsontodb +1 -1
- data/lib/jsontodb.rb +14 -0
- data/lib/jsontodb/cli.rb +1 -15
- data/lib/jsontodb/processor.rb +37 -0
- data/lib/jsontodb/rest.rb +35 -28
- data/lib/jsontodb/version.rb +1 -4
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a85df10c0761ac2587b7fd32c186ea145e474a97
|
4
|
+
data.tar.gz: 53df02340f70f52609c36562215584239ce38543
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2de1b5fc9fa5b1b126a2c6c0c418275e1bede4ff60ae4a8c14dc45dba1f4249df04cd4e9b2889ad94da7e2b2b22817736566079ef8912c8a2ff0eeba9c0c56ba
|
7
|
+
data.tar.gz: 5e074e81c5e7c8895bd84bb41365c773f7e8718c43836459f099ad6e12ade4c888c98a75bc91a36aca99aec888e95a74b29bee21ff963f1003d647591b53f27b
|
data/README.md
CHANGED
@@ -0,0 +1,72 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/jsontodb.svg)](http://badge.fury.io/rb/jsontodb)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/HHS-SpecialTopics/jsontodb/badges/gpa.svg)](https://codeclimate.com/github/HHS-SpecialTopics/jsontodb)
|
3
|
+
|
4
|
+
# JSON to Database
|
5
|
+
**JSONtoDB** is a command-line tool allowing HTTP methods to be called from a terminal or other program. It was originally intended
|
6
|
+
to be used to send requests from a client computer to a [Wordpress](https://wordpress.com/) server using the [REST-API plugin](https://wordpress.org/plugins/rest-api/).
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
To install, use the standard RubyGems syntax:
|
10
|
+
```sh
|
11
|
+
$ gem install jsontodb
|
12
|
+
```
|
13
|
+
Or if necessary, you can add it to your `Gemfile`:
|
14
|
+
```rb
|
15
|
+
gem 'jsontodb'
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
**JSONtoDB** provides a command-line interface, which can be invoked using:
|
20
|
+
```sh
|
21
|
+
$ jsontodb
|
22
|
+
```
|
23
|
+
This command will open an interactive shell for **JSONtoDB** after prompting for authentication credentials (see [Authentication](#authentication)).
|
24
|
+
|
25
|
+
You can also pass your authentication credentials to the command then open an interactive shell:
|
26
|
+
```sh
|
27
|
+
$ jsontodb [username] [password]
|
28
|
+
```
|
29
|
+
|
30
|
+
Running a full **JSONtoDB** command is possible directly from the command-line as well:
|
31
|
+
```sh
|
32
|
+
$ jsontodb [username] [password] [command]
|
33
|
+
```
|
34
|
+
|
35
|
+
Functionality is planned for placing credentials in a file to avoid passwords being visible, as they are when passed to the `jsontodb` command (though
|
36
|
+
they are hidden in the interactive shell).
|
37
|
+
|
38
|
+
## Commands
|
39
|
+
Four commands are provided by default, and they represent their respective HTTP method:
|
40
|
+
|
41
|
+
### Get
|
42
|
+
Get represents the GET HTTP method, and its only parameter is the URL to GET:
|
43
|
+
```sh
|
44
|
+
$ [parent command] get [url]
|
45
|
+
```
|
46
|
+
|
47
|
+
### Delete
|
48
|
+
Delete represents the DELETE HTTP method, and its only parameter is the URL to DELETE:
|
49
|
+
```sh
|
50
|
+
$ [parent command] delete [url]
|
51
|
+
```
|
52
|
+
|
53
|
+
### Put
|
54
|
+
Put represents the PUT HTTP method. Its first parameter is the URL to PUT to, and its second parameter is the source of the data (a file path, see [Source Files](#source-files)):
|
55
|
+
```sh
|
56
|
+
$ [parent command] put [url] [source file]
|
57
|
+
```
|
58
|
+
|
59
|
+
### Post
|
60
|
+
Post represents the POST HTTP method. Its first parameter is the URL to POST to, and its second parameter is the source of the data (a file path, see [Source Files](#source-files)):
|
61
|
+
```sh
|
62
|
+
$ [parent command] post [url] [source file]
|
63
|
+
```
|
64
|
+
|
65
|
+
## Source Files
|
66
|
+
The only source data type (or content type) that **JSONtoDB** currently supports is [JSON](http://www.json.org/), hence the name **JSONtoDB**.
|
67
|
+
|
68
|
+
## Authentication
|
69
|
+
**JSONtoDB** offers [basic authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) functionality only at this time. If you are using the interactive shell mode, it will prompt for credentials. Otherwise, you can pass them in as the first two parameters to the `jsontodb` command.
|
70
|
+
|
71
|
+
## Copyright
|
72
|
+
Copyright (c) 2016 Christopher Lutz. See [LICENSE.txt](LICENSE.txt) for details.
|
data/bin/jsontodb
CHANGED
data/lib/jsontodb.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'cogwheels'
|
2
|
+
|
1
3
|
# This library provides functionality to perform REST operations from the
|
2
4
|
# command-line. It also provides the capability to use JSON in HTTP requests.
|
3
5
|
#
|
@@ -27,4 +29,16 @@ module JSONtoDB
|
|
27
29
|
autoload :IO, 'jsontodb/io'
|
28
30
|
autoload :Processor, 'jsontodb/processor'
|
29
31
|
autoload :REST, 'jsontodb/rest'
|
32
|
+
|
33
|
+
module_function
|
34
|
+
|
35
|
+
CONFIG = if ARGV.empty?
|
36
|
+
Cogwheels.load(File.join(File.dirname(__FILE__), '/../config/generic.yml'))
|
37
|
+
else
|
38
|
+
Cogwheels.load(ARGV.shift)
|
39
|
+
end
|
40
|
+
|
41
|
+
def start
|
42
|
+
JSONtoDB::Processor.run_config
|
43
|
+
end
|
30
44
|
end
|
data/lib/jsontodb/cli.rb
CHANGED
@@ -6,21 +6,7 @@ module JSONtoDB
|
|
6
6
|
module CLI
|
7
7
|
module_function
|
8
8
|
|
9
|
-
|
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
|
9
|
+
attr_reader :user, :pass
|
24
10
|
|
25
11
|
def authentication_credentials(user = nil, pass = nil)
|
26
12
|
if user.nil? || pass.nil?
|
data/lib/jsontodb/processor.rb
CHANGED
@@ -4,6 +4,43 @@ module JSONtoDB
|
|
4
4
|
module Processor
|
5
5
|
module_function
|
6
6
|
|
7
|
+
def run_config
|
8
|
+
check_auth
|
9
|
+
|
10
|
+
unless JSONtoDB::CONFIG['Matrix'].nil?
|
11
|
+
JSONtoDB::CONFIG['Matrix'].each do |hash|
|
12
|
+
files = hash['Files']
|
13
|
+
url = hash['Url']
|
14
|
+
command = hash['Command']
|
15
|
+
|
16
|
+
if files.nil?
|
17
|
+
run_command([command, url], @user, @pass)
|
18
|
+
elsif files.is_a?(Array)
|
19
|
+
files.each do |file|
|
20
|
+
Dir[file].each do |f|
|
21
|
+
run_command([command, url, f], @user, @pass)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
else
|
25
|
+
Dir[files].each do |f|
|
26
|
+
run_command([command, url, f], @user, @pass)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
JSONtoDB::CLI.continuous_cli if JSONtoDB::CONFIG['Interactive']
|
33
|
+
end
|
34
|
+
|
35
|
+
def check_auth
|
36
|
+
@user = JSONtoDB::CONFIG['User']
|
37
|
+
@pass = JSONtoDB::CONFIG['Pass']
|
38
|
+
return unless JSONtoDB::CONFIG['CredPrompt']
|
39
|
+
JSONtoDB::CLI.authentication_credentials(@user, @pass)
|
40
|
+
@user = JSONtoDB::CLI.user
|
41
|
+
@pass = JSONtoDB::CLI.pass
|
42
|
+
end
|
43
|
+
|
7
44
|
def run_command(args, user, pass)
|
8
45
|
return if args.empty?
|
9
46
|
command = args.shift
|
data/lib/jsontodb/rest.rb
CHANGED
@@ -6,49 +6,56 @@ module JSONtoDB
|
|
6
6
|
module_function
|
7
7
|
|
8
8
|
def get(url, user, pass)
|
9
|
-
|
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)
|
9
|
+
no_data_request(Net::HTTP::Get, url, user, pass)
|
18
10
|
end
|
19
11
|
|
20
12
|
def delete(url, user, pass)
|
21
|
-
|
22
|
-
|
23
|
-
request.basic_auth(user, pass)
|
13
|
+
no_data_request(Net::HTTP::Delete, url, user, pass)
|
14
|
+
end
|
24
15
|
|
25
|
-
|
26
|
-
|
27
|
-
end
|
16
|
+
def no_data_request(klass, url, user, pass)
|
17
|
+
request = basic_request(klass, url, user, pass)
|
28
18
|
|
29
|
-
|
19
|
+
make_request(url, request)
|
30
20
|
end
|
31
21
|
|
32
22
|
def put(url, src, user, pass)
|
33
|
-
|
34
|
-
|
35
|
-
request.basic_auth(user, pass)
|
36
|
-
request.content_type = 'application/json'
|
37
|
-
request.set_form_data(JSONtoDB::IO.read(src))
|
23
|
+
data_request(Net::HTTP::Put, url, src, user, pass)
|
24
|
+
end
|
38
25
|
|
39
|
-
|
40
|
-
|
26
|
+
def post(url, src, user, pass)
|
27
|
+
data_request(Net::HTTP::Post, url, src, user, pass)
|
28
|
+
end
|
29
|
+
|
30
|
+
def data_request(klass, url, src, user, pass)
|
31
|
+
if File.directory?(src)
|
32
|
+
|
33
|
+
Dir["#{src}/*.json"].each do |_file|
|
34
|
+
file_request(klass, url, src, user, pass)
|
35
|
+
end
|
36
|
+
|
37
|
+
elsif File.file?(src)
|
38
|
+
file_request(klass, url, src, user, pass)
|
41
39
|
end
|
40
|
+
end
|
42
41
|
|
43
|
-
|
42
|
+
def file_request(klass, url, src, user, pass)
|
43
|
+
request = basic_request(klass, url, user, pass)
|
44
|
+
request.content_type = 'application/json'
|
45
|
+
request.set_form_data(JSONtoDB::IO.read(src))
|
46
|
+
|
47
|
+
make_request(url, request)
|
44
48
|
end
|
45
49
|
|
46
|
-
def
|
50
|
+
def basic_request(klass, url, user, pass)
|
47
51
|
uri = URI(url)
|
48
|
-
request =
|
52
|
+
request = klass.new(uri)
|
49
53
|
request.basic_auth(user, pass)
|
50
|
-
request
|
51
|
-
|
54
|
+
request
|
55
|
+
end
|
56
|
+
|
57
|
+
def make_request(url, request)
|
58
|
+
uri = URI(url)
|
52
59
|
|
53
60
|
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
54
61
|
http.request(request)
|
data/lib/jsontodb/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsontodb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christopher Lutz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
12
|
-
dependencies:
|
11
|
+
date: 2016-11-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: cogwheels
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.1'
|
13
27
|
description:
|
14
28
|
email:
|
15
29
|
- lutzblox@gmail.com
|