zurl 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +2 -0
  3. data/README.md +44 -0
  4. data/bin/zurl +140 -0
  5. data/zurl.gemspec +11 -0
  6. metadata +76 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4385ae367f873be64bff1e48b7d8656f769a8d4c
4
+ data.tar.gz: 8231ffd39a0e672317e41eb3c837f5cebff1fc8b
5
+ SHA512:
6
+ metadata.gz: c85aa2cf5619a9c91b60abe7657064f50d2785c11e188a2944f53aa345f9d1d2eeba70e7db6aa44fa8b685fccc5459c0a3496444996a56191a01eede9b52bb41
7
+ data.tar.gz: 01073cd9f9adca141bb24748283f3161112f005ba10139f9925e989a500a48852d1141e6e7f67b706b78dacc9d3054fdf0186930ca4b38bf951ad7cf50e3d6d2
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,44 @@
1
+ # zurl!
2
+
3
+ brazen, ripoff/rewrite of [curly](https://github.com/aaronpk/curly).
4
+
5
+ adds json content-type header, posts json bodies, parses/awesomeprints json
6
+ responses, colors headers.
7
+
8
+ ## changes
9
+
10
+ #### -d
11
+
12
+ * accepts formencoded, translates to json
13
+ * accepts rubyish hash or array syntax, translates to json
14
+
15
+ ##### examples
16
+
17
+ `$ zurl -d foo=bar -d 'aaa=bbb&ccc=54321' -d 'baz: 123, bat: true' http://example.com`
18
+
19
+ this would post the following body:
20
+
21
+ ```javascript
22
+ {
23
+ "foo": "bar",
24
+ "aaa": "bbb",
25
+ "ccc": "54321",
26
+ "baz": 123,
27
+ "bat": true
28
+ }
29
+ ```
30
+
31
+ n.b. - if you want to post integers or booleans, use rubyish hash syntax as formencoded
32
+ assumes strings.
33
+
34
+ #### -W
35
+
36
+ show formatted version of json post body
37
+
38
+ #### -Z
39
+
40
+ show starred lines from curl -v output
41
+
42
+ #### -oauth [token]
43
+
44
+ add `Authorization: Bearer [token]` header
@@ -0,0 +1,140 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ CURL = `which curl`.chomp
4
+ exit 1 if CURL.empty?
5
+
6
+ require 'awesome_print'
7
+ require 'colored'
8
+ require 'json'
9
+ require 'open3'
10
+
11
+ CMD = [
12
+ CURL,
13
+ '-v',
14
+ '-s'
15
+ ]
16
+
17
+ RUBYISH = /\S+(:|\s*=>)\s*\S+/
18
+ FORM_ENCODED_KEY_VALUE = /^([^=]+?)=([^=]+?)$/
19
+
20
+ class PostData
21
+
22
+ attr_reader :data
23
+
24
+ def data?
25
+ !!@data
26
+ end
27
+
28
+ def parse_data s
29
+ case
30
+
31
+ when m = RUBYISH.match(s)
32
+ tried_hash = false
33
+ begin
34
+ d = eval s
35
+ rescue SyntaxError => se
36
+ if tried_hash
37
+ d = nil
38
+ raise se
39
+ else
40
+ s = '{' + s + '}'
41
+ tried_hash = true
42
+ retry
43
+ end
44
+ end
45
+ if @data.nil?
46
+ @data = d
47
+ else
48
+ @data.merge! d
49
+ end
50
+
51
+ when m = FORM_ENCODED_KEY_VALUE.match(s)
52
+ @data ||= {}
53
+ @data[m[1]] = m[2]
54
+
55
+ when s.index(',')
56
+ case @data
57
+ when Array
58
+ @data += s.split(',')
59
+ when Hash
60
+ raise ArgumentError.new 'adding array to existing object'
61
+ else
62
+ @data = s.split(',')
63
+ end
64
+
65
+ when s.index('&')
66
+ s.split('&').each{|kv| parse_data(kv)}
67
+
68
+ end
69
+ end
70
+
71
+ def to_json
72
+ @data.to_json
73
+ end
74
+
75
+ end
76
+
77
+ args = []
78
+ pd = PostData.new
79
+ post_data_flag = false
80
+ oauth_token_flag = false
81
+ show_post_data = false
82
+ show_starred_lines = false
83
+
84
+ ARGV.each do |a|
85
+ if post_data_flag
86
+ if a[0] == '@'
87
+ args += ['-d', a]
88
+ else
89
+ pd.parse_data a
90
+ post_data_flag = false
91
+ end
92
+ elsif oauth_token_flag
93
+ args.push '-H', "Authorization: Bearer #{a}"
94
+ oauth_token_flag = false
95
+ else
96
+ case a
97
+ when '-d'
98
+ post_data_flag = true
99
+ when '-oauth'
100
+ oauth_token_flag = true
101
+ when '-W'
102
+ show_post_data = true
103
+ when '-Z'
104
+ show_starred_lines = true
105
+ else
106
+ args << a
107
+ end
108
+ end
109
+ end
110
+
111
+ if pd.data?
112
+ args.push '-H', 'Content-Type: application/json',
113
+ '-d', pd.to_json
114
+
115
+ if show_post_data
116
+ puts 'posting:'
117
+ puts JSON.pretty_generate(pd.data).red
118
+ end
119
+ end
120
+
121
+ out,err = Open3.capture3 *(CMD + args)
122
+
123
+ err.split("\n").each do |e|
124
+ if e[0] == '*'
125
+ puts e.purple if show_starred_lines
126
+ else
127
+ puts e.__send__ case e[0]
128
+ when '>'; :cyan
129
+ when '<'; :yellow
130
+ when '{', '}'; :black
131
+ else; :blue
132
+ end
133
+ end
134
+ end
135
+
136
+ begin
137
+ ap JSON.parse out
138
+ rescue
139
+ puts out
140
+ end
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.authors = ["Kenichi Nakamura"]
3
+ gem.email = ["kenichi.nakamura@gmail.com"]
4
+ gem.description = gem.summary = "A wrapper for curl which pretty-prints JSON output"
5
+ gem.executables = ['zurl']
6
+ gem.files = `git ls-files | grep -v .gitignore`.split("\n")
7
+ gem.name = "zurl"
8
+ gem.version = '0.0.2'
9
+ gem.add_dependency 'colored'
10
+ gem.add_dependency 'awesome_print'
11
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zurl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Kenichi Nakamura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colored
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: awesome_print
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A wrapper for curl which pretty-prints JSON output
42
+ email:
43
+ - kenichi.nakamura@gmail.com
44
+ executables:
45
+ - zurl
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - Gemfile
50
+ - README.md
51
+ - bin/zurl
52
+ - zurl.gemspec
53
+ homepage:
54
+ licenses: []
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.4.5
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: A wrapper for curl which pretty-prints JSON output
76
+ test_files: []