zyxt 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/request.rb +12 -2
  3. data/lib/request_client.rb +63 -12
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58786dd2a040a7bc920364eabc49f5810dc4dffaf2dc28fc3fa6d195c5713053
4
- data.tar.gz: 32d7615d2b07bcb3251383ef8f13977400221ab3586ae4544c322e9fcbc00a89
3
+ metadata.gz: a7a31df54bffa5395e874ddf4cfff1bb129a9eec56a30cc351c94322a1cbf29a
4
+ data.tar.gz: 101c5573e5fada7d28d5fd7dbe59ddb5a893810aa2f7ac851d78d104241a54df
5
5
  SHA512:
6
- metadata.gz: c495422a60c48c353c50029b4f46d7c37dc5857dd9c20d76e42603cd91cf312f8d6b8f99dbcf3ef9d78eef48f2d893d6bfa0b4287f3219ab9985486adb63f8a6
7
- data.tar.gz: 3b368a160910b098e8d1f1cb0387ea5867327be49e19ac8eda8887ce871c9dd48731931c937e688556e381294fd28567a8e9b6c012b383eb261ad21f615f4d35
6
+ metadata.gz: af0336b994f9ea27906fa4af93f19ba26554d4960af9e111e089349fdd4616f3db9f898b540aa083ee4c8624d529795231d9039833ea376b4dee809d894d5f5e
7
+ data.tar.gz: ec470652b15cda87f7d138f88cd8b49c59ed9a5b0292e7c0bb0ad6e8c022b402bf823ea5e6aa70fbb413ea54943e3a0588778d89d48129c452baba49a42926e3
@@ -4,7 +4,7 @@ require "json"
4
4
 
5
5
  module Request
6
6
  class Http
7
- def self.send_request(method, url, body = nil)
7
+ def self.send_request(method, url, body = nil, headers = nil)
8
8
  methods = { "get" => Net::HTTP::Get, "post" => Net::HTTP::Post, "delete" => Net::HTTP::Delete, "put" => Net::HTTP::Put }
9
9
  uri = URI.parse(url)
10
10
  http = Net::HTTP.new(uri.host, uri.port)
@@ -13,10 +13,20 @@ module Request
13
13
  request["User-Agent"] = "zyxt tool"
14
14
  request["Accept"] = "*/*"
15
15
  request["Content-Type"] = "application/json"
16
+ if !headers.nil?
17
+ headers.each do | key, value |
18
+ request[key] = value
19
+ end
20
+ end
16
21
  if !body.nil?
17
22
  request.body = body.to_json
18
23
  end
19
- http.request(request)
24
+ response = http.request(request)
25
+ end
26
+
27
+ def self.log(response)
28
+ puts "Status Code: #{response.code}"
29
+ puts "Body: #{response.body}"
20
30
  end
21
31
  end
22
32
  end
@@ -1,40 +1,91 @@
1
1
  require "thor"
2
+ require "json"
3
+ require "leveldb"
4
+ require "securerandom"
2
5
  require_relative "./request"
3
6
 
4
7
  module RequestClient
5
8
  class Zyxt < Thor
9
+ attr_accessor :db
6
10
  def self.exit_on_failure?
7
11
  true
8
12
  end
9
13
 
14
+ desc "history", "show history of requests"
15
+ def history
16
+ @db = LevelDB::DB.new '/tmp/foo'
17
+ @db.each { |key, val| puts "Key: #{key}, Val: #{val}" }
18
+ end
19
+
10
20
  desc "get URL", "execute GET request to URL"
21
+ option :headers, :type => :hash, :desc => "Headers"
22
+ option :curl, :type => :boolean, :desc => "Print command as cURL"
11
23
  def get(url)
12
- response = Request::Http.send_request("get", url)
13
- puts "Status Code: #{response.code}"
14
- puts "Body: #{response.body}"
24
+ if options[:curl]
25
+ puts "curl -X GET #{url}"
26
+ return nil
27
+ end
28
+
29
+ response = Request::Http.send_request("get", url, nil, options[:headers])
30
+ Request::Http.log(response)
31
+ save_request("get")
15
32
  end
16
33
 
17
34
  desc "post URL", "execute POST request to URL"
18
35
  option :body, :type => :hash, :desc => "Body of the request"
36
+ option :headers, :type => :hash, :desc => "Headers"
37
+ option :curl, :type => :boolean, :desc => "Print command as cURL"
19
38
  def post(url)
20
- response = Request::Http.send_request("post", url, options[:body])
21
- puts "Status Code: #{response.code}"
22
- puts "Body: #{response.body}"
39
+ if options[:curl]
40
+ puts "curl -X POST #{url} -H 'Content-Type: application/json' -d #{options[:body]}"
41
+ return nil
42
+ end
43
+
44
+ response = Request::Http.send_request("post", url, options[:body], options[:headers])
45
+ Request::Http.log(response)
46
+ save_request("post")
23
47
  end
24
48
 
25
49
  desc "put URL", "execute PUT request to URL"
26
50
  option :body, :type => :hash, :desc => "Body of the request"
51
+ option :headers, :type => :hash, :desc => "Headers"
52
+ option :curl, :type => :boolean, :desc => "Print command as cURL"
27
53
  def put(url)
28
- response = Request::Http.send_request("put", url, options[:body])
29
- puts "Status Code: #{response.code}"
30
- puts "Body: #{response.body}"
54
+ if options[:curl]
55
+ puts "curl -X PUT #{url} -H 'Content-Type: application/json' -d #{options[:body]}"
56
+ return nil
57
+ end
58
+
59
+ response = Request::Http.send_request("put", url, options[:body], options[:headers])
60
+ Request::Http.log(response)
61
+ save_request("put")
31
62
  end
32
63
 
33
64
  desc "delete URL", "execute DELETE request to URL"
65
+ option :headers, :type => :hash, :desc => "Headers"
66
+ option :curl, :type => :boolean, :desc => "Print command as cURL"
34
67
  def delete(url)
35
- response = Request::Http.send_request("delete", url)
36
- puts "Status Code: #{response.code}"
37
- puts "Body: #{response.body}"
68
+ if options[:curl]
69
+ puts "curl -X DELETE #{url}"
70
+ return nil
71
+ end
72
+
73
+ response = Request::Http.send_request("delete", url, nil, options[:headers])
74
+ Request::Http.log(response)
75
+ save_request("delete")
76
+ end
77
+
78
+
79
+ no_commands do
80
+ def save_request(method)
81
+ if @db.nil?
82
+ @db = LevelDB::DB.new '/tmp/foo'
83
+ end
84
+
85
+ tempHash = {"hash" => "Command executed #{method}", "executed_at" => Time.now}
86
+
87
+ @db.put(SecureRandom.hex, tempHash)
88
+ end
38
89
  end
39
90
  end
40
91
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zyxt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Matsumoto