xcurl 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51c16433d41b618dd392ee5c5947abe0ff49f1e4ae85d2be57fb01397e796be5
4
- data.tar.gz: 1e8b22f003337513720186006f6bf48f15d177f2d8857d91b198e52d3777bc48
3
+ metadata.gz: 6db538ae75b6f2038a6e73b2a46fe2574cf93056ca022075f704a479af1b885d
4
+ data.tar.gz: a2ca6ae2b4a9a57c2572001311ab8e69dff7b74e52b4aec475c23893cd6db058
5
5
  SHA512:
6
- metadata.gz: 2964a90ef8a8bfe770a0f67f12f7e47b01d246618078e283c5c7b1132907213733498a1fc41b92c0e89f2263d28b209e12e5b8cf06711164eea0c9eb27a3ee3e
7
- data.tar.gz: 6e55675649b012750edd9dd7f325e266bf4c9e5b8f40da2f0d3e2244192ecb84235d26ae2d2721919f354c771eb7bf280572caadeb307054e9115152aefcb277
6
+ metadata.gz: 706f6e2b5dc70e76713fc185f2352d72342982fc977bf73fa498ab39fd416c9c3abc4897d1bb429a5df4fc0c43c59119693ca993c6e300217e9de85185876585
7
+ data.tar.gz: 0d1f09b3f28435c0a3d627f2d8816bd01326422f2487c42c3850b62926e9e3bfa746536c136a628c9ebd8b161ac024c6f7a8aab4ae7c7a32c0e4fd22efff70b4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- xcurl (1.0.6)
4
+ xcurl (1.0.7)
5
5
  pastel (~> 0.8)
6
6
 
7
7
  GEM
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "xcurl"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require "irb"
11
+ IRB.start(__FILE__)
data/lib/log.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'pastel'
2
+
3
+ class Log
4
+ def initialize
5
+ @p = Pastel.new
6
+ end
7
+
8
+ def deleted(d)
9
+ puts @p.green("part deleted #{@p.yellow(d)}")
10
+ end
11
+
12
+ def url_set(u)
13
+ puts @p.green("url set #{@p.yellow(u)}")
14
+ end
15
+
16
+ def added(a)
17
+ puts @p.green("added #{@p.yellow(a)}")
18
+ end
19
+
20
+ def show_current_session(session)
21
+ puts @p.green.bold("URL:\n #{@p.yellow(session['url'])}")
22
+ puts @p.green.bold("Parts:")
23
+ session['parts'].each_with_index do |part, index|
24
+ puts " #{@p.blue(index)} - #{@p.yellow(part)}"
25
+ end
26
+ end
27
+
28
+ def result(cmd, data)
29
+ puts @p.cyan("==> #{cmd}")
30
+ puts ''
31
+ puts @p.green(data)
32
+ puts ''
33
+ end
34
+
35
+ def error(cmd, data)
36
+ puts @p.cyan("==> #{cmd}")
37
+ puts ''
38
+ puts @p.red(data)
39
+ puts ''
40
+ end
41
+ end
data/lib/xcurl.rb CHANGED
@@ -1,16 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'pastel'
3
+ require 'ostruct'
4
+ require 'open3'
5
+ require 'json'
6
+ require_relative './log'
4
7
 
5
8
  class Xcurl
6
- def self.start(session)
7
- pastel = Pastel.new
8
-
9
- current_session = session || 'untitled'
10
- parts = []
9
+ def self.start(session_id = nil)
10
+ log = Log.new
11
+ session = load_session(session_id || 'untitled')
11
12
 
12
13
  while true
13
- print "[#{current_session}] "
14
+ print "[#{session['id']}] "
14
15
 
15
16
  cmd = gets.chomp
16
17
 
@@ -18,29 +19,87 @@ class Xcurl
18
19
  when 'quit'
19
20
  break
20
21
 
21
- when /init /
22
- current_session = cmd.scan(/init (.*)/).flatten.first
22
+ when /save /
23
+ new_id = cmd.scan(/save (.*)/).flatten.first
24
+
25
+ session = save_session(new_id, session)
23
26
 
24
27
  when 'show'
25
- parts.each_with_index do |part, index|
26
- puts "#{pastel.blue(index)} - #{pastel.green("'#{pastel.bold(part)}'")}"
27
- end
28
+ log.show_current_session(session)
28
29
 
29
- when /delete /
30
+ when /^delete /
30
31
  index_part = cmd.scan(/delete (.*)/).flatten.first.to_i
31
32
 
32
- parts.delete_at(index_part)
33
+ deleted = session['parts'].delete_at(index_part)
34
+
35
+ log.deleted(deleted)
36
+
37
+ when /^url /
38
+ url = cmd.scan(/url (.*)/).flatten.first
39
+
40
+ session['url'] = url
41
+
42
+ log.url_set(url)
33
43
 
34
- when /add /
44
+ when /^add /
35
45
  part = cmd.scan(/add (.*)/).flatten.first
36
46
 
37
- parts << part
47
+ session['parts'] << part
38
48
 
39
- puts pastel.green("added '#{pastel.bold(part)}'")
49
+ log.added(part)
40
50
 
41
- when /curl /
42
- puts "curl #{parts.join(' ')}"
51
+ when /^curl /
52
+ path = cmd.scan(/curl (.*)/).flatten.first
53
+
54
+ curl_cmd = "curl #{session['url']}/#{path} #{session['parts'].join(' ')}"
55
+
56
+ out, err, status = Open3.capture3(curl_cmd)
57
+
58
+ if status.success?
59
+ data =
60
+ begin
61
+ JSON.pretty_generate(JSON.parse(out))
62
+ rescue JSON::ParserError => e
63
+ out
64
+ end
65
+
66
+ log.result(curl_cmd, data)
67
+ else
68
+ log.error(curl_cmd, data)
69
+ end
43
70
  end
44
71
  end
45
72
  end
73
+
74
+ def self.load_session(id)
75
+ ensure_home
76
+
77
+ if File.exist?("#{dir}/#{id}")
78
+ JSON.parse(File.read("#{dir}/#{id}"))
79
+ else
80
+ {
81
+ 'id' => 'untitled',
82
+ 'url' => nil,
83
+ 'parts' => []
84
+ }
85
+ end
86
+ end
87
+
88
+ def self.save_session(id, session)
89
+ ensure_home
90
+
91
+ session['id'] = id
92
+
93
+ File.write("#{dir}/#{id}", JSON.pretty_generate(session))
94
+
95
+ session
96
+ end
97
+
98
+ def self.dir
99
+ "#{ENV['HOME']}/.xcurl"
100
+ end
101
+
102
+ def self.ensure_home
103
+ Dir.mkdir(dir) unless Dir.exist?(dir)
104
+ end
46
105
  end
data/xcurl.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "xcurl"
5
- spec.version = '1.0.7'
5
+ spec.version = '1.0.8'
6
6
  spec.authors = ["Andrew S Aguiar"]
7
7
  spec.email = ["andrewaguiar6@gmail.com"]
8
8
  spec.summary = "Convenient wrapper for curl"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xcurl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew S Aguiar
@@ -41,9 +41,11 @@ files:
41
41
  - LICENSE.txt
42
42
  - README.md
43
43
  - Rakefile
44
+ - bin/console
44
45
  - bin/setup
45
46
  - bin/xcurl
46
47
  - generate-gem
48
+ - lib/log.rb
47
49
  - lib/xcurl.rb
48
50
  - xcurl.gemspec
49
51
  homepage: https://github.com/andrewaguiar/xcurl