hoick 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a45c81b44157ce9e833bda8f0b4a0d8822043f0
4
- data.tar.gz: 5dd7e6c5da5a8ed72684f9db9481c06e728112ef
3
+ metadata.gz: 0003e566cb04c2c3704564ddfbbbf643f4574322
4
+ data.tar.gz: efbd89eb6c6b7fbe3a2fd12bd6d8f32faf0340b9
5
5
  SHA512:
6
- metadata.gz: 16036cba8cd39cda4dd6e7e43ca3cea2dedd7e6efbccc1627978f71f5b93e80c35e495229a34a1d4b0afb89919c257072af195684673e1564a8b0225171a4f64
7
- data.tar.gz: 655d76ada1d8c7b125f201a6d06e679d430181cf96489b439b4650570ce0c9740cbd3beb0f75da11663ea669121494da24b1cc58c897dadedbc73f7c450c9909
6
+ metadata.gz: d58fdcddc91db51bc7d06be0407af0df00f376bf4daa695a72c2db36fd81b5e42b9cddb19dc516f6326052a83d21a84fde7461b1b4de1b458c2611eed9a731d5
7
+ data.tar.gz: 222734633bae1b63c7e9e1ffe9079d4d76b43a2612e7c480c52691755b449e752b324383d566c8efac2154ce22dbade0d71a6b538ec36ec2ac054ceb3fc41749
data/README.md CHANGED
@@ -1,24 +1,32 @@
1
1
  # Hoick
2
2
 
3
- TODO: Write a gem description
3
+ Hoick is a command-line HTTP client. It's intended mainly as a tool for testing RESTful APIs, but you can use for something else, if you really want to.
4
+
5
+ Hoick is designed to be simple yet useful, and to play nicely in a Unix command pipeline.
4
6
 
5
7
  ## Installation
6
8
 
7
- Add this line to your application's Gemfile:
9
+ Hoick is distributed as a Ruby gem, installable using:
8
10
 
9
- gem 'hoick'
11
+ $ gem install hoick
10
12
 
11
- And then execute:
13
+ ## Usage
12
14
 
13
- $ bundle
15
+ Hoick has subcommands modelled on HTTP verbs.
14
16
 
15
- Or install it yourself as:
17
+ ### GET
16
18
 
17
- $ gem install hoick
19
+ To fetch a resource, use GET. The response body will be printed to STDOUT.
18
20
 
19
- ## Usage
21
+ If you're interested in response headers too, add the "`-h`" flag. Add the "`--follow`" flag if you wish to follow redirects.
22
+
23
+ ### PUT and POST
24
+
25
+ The "PUT" subcommand uploads data to a specified URL. By default, the payload is read from STDIN, but you can specify the "`-F`" option to read it from a file, instead.
26
+
27
+ Hoick guesses a "Content-Type" from the file-name. If a type cannot be guessed, or if the payload is sourced from STDIN, binary data ("application/octet-stream") is assumed. Either way, the default can be overridden with "`-T`" (which can be either a file extension, or a full MIME-type string).
20
28
 
21
- TODO: Write usage instructions here
29
+ The "POST" subcommand works in a similar way.
22
30
 
23
31
  ## Contributing
24
32
 
@@ -26,4 +34,4 @@ TODO: Write usage instructions here
26
34
  2. Create your feature branch (`git checkout -b my-new-feature`)
27
35
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
36
  4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
37
+ 5. Submit a Pull Request
data/hoick.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Hoick::VERSION
9
9
  spec.authors = ["Mike Williams"]
10
10
  spec.email = ["mdub@dogbiscuit.org"]
11
- spec.description = %q{A command-line HTTP client}
12
11
  spec.summary = %q{A command-line HTTP client}
12
+ spec.description = File.read("README.md").split("\n\n")[1]
13
13
  spec.homepage = "https://github.com/mdub/hoick"
14
14
  spec.license = "MIT"
15
15
 
data/lib/hoick/command.rb CHANGED
@@ -31,28 +31,43 @@ module Hoick
31
31
 
32
32
  option ["--debug"], :flag, "debug"
33
33
 
34
+ def follow_redirects?
35
+ false
36
+ end
37
+
34
38
  subcommand ["get", "GET"], "HTTP GET" do
35
39
 
36
- option ["--follow"], :flag, "follow redirects"
40
+ option ["--follow"], :flag, "follow redirects", :attribute_name => :follow_redirects
37
41
 
38
42
  declare_url_parameter
39
43
 
40
44
  def execute
41
- get_with_redirects do |response|
42
- display_response(response)
43
- end
45
+ get(full_url, &method(:display_response))
44
46
  end
45
47
 
46
- def get_with_redirects(&callback)
47
- with_http_connection do |http, uri|
48
- http.request_get(uri.request_uri) do |response|
49
- if follow? && response.kind_of?(Net::HTTPRedirection)
50
- get_with_redirects(response['location'], &callback)
51
- else
52
- callback.call(response)
53
- end
48
+ private
49
+
50
+ def get(uri, &callback)
51
+ http_request("GET", uri) do |response|
52
+ if follow_redirects? && response.kind_of?(Net::HTTPRedirection)
53
+ raise Redirected, response['location']
54
54
  end
55
+ callback.call(response)
56
+ end
57
+ rescue Redirected => e
58
+ uri = URI(e.location)
59
+ retry
60
+ end
61
+
62
+ class Redirected < StandardError
63
+
64
+ def initialize(location)
65
+ @location = location
66
+ super("Redirected to #{location}")
55
67
  end
68
+
69
+ attr_reader :location
70
+
56
71
  end
57
72
 
58
73
  end
@@ -98,15 +113,7 @@ module Hoick
98
113
  declare_url_parameter
99
114
 
100
115
  def execute
101
- content = payload
102
- with_http_connection do |http, uri|
103
- post = Net::HTTP::Post.new(uri.request_uri)
104
- post["Content-Type"] = content_type
105
- post.body = content
106
- http.request(post) do |response|
107
- display_response(response)
108
- end
109
- end
116
+ http_request("POST", full_url, payload, content_type, &method(:display_response))
110
117
  end
111
118
 
112
119
  end
@@ -118,22 +125,14 @@ module Hoick
118
125
  declare_url_parameter
119
126
 
120
127
  def execute
121
- content = payload
122
- with_http_connection do |http, uri|
123
- put = Net::HTTP::Put.new(uri.request_uri)
124
- put["Content-Type"] = content_type
125
- put.body = content
126
- http.request(put) do |response|
127
- display_response(response)
128
- end
129
- end
128
+ http_request("PUT", full_url, payload, content_type, &method(:display_response))
130
129
  end
131
130
 
132
131
  end
133
132
 
134
133
  private
135
134
 
136
- def uri
135
+ def full_url
137
136
  if base_url
138
137
  base_url + url
139
138
  else
@@ -141,11 +140,37 @@ module Hoick
141
140
  end
142
141
  end
143
142
 
144
- def with_http_connection
143
+ def http_request(method, uri, content = nil, content_type = nil, &callback)
144
+ request = build_request(method, uri, content, content_type)
145
+ with_http_connection(uri) do |http|
146
+ http.request(request) do |response|
147
+ if follow_redirects? && response.kind_of?(Net::HTTPRedirection)
148
+ raise Redirected, response['location']
149
+ end
150
+ callback.call(response)
151
+ end
152
+ end
153
+ rescue Redirected => e
154
+ uri = URI(e.location)
155
+ retry
156
+ end
157
+
158
+ def build_request(method, uri, content, content_type)
159
+ request_class = Net::HTTP.const_get(method.to_s.capitalize)
160
+ request = request_class.new(uri.request_uri)
161
+ if content
162
+ request["Content-Type"] = content_type
163
+ request.body = content
164
+ end
165
+ request
166
+ end
167
+
168
+ def with_http_connection(uri)
145
169
  http = Net::HTTP.new(uri.host, uri.port)
170
+ http.use_ssl = (uri.scheme == "https")
146
171
  http.set_debug_output($stderr) if debug?
147
172
  http.start do
148
- yield http, uri
173
+ yield http
149
174
  end
150
175
  end
151
176
 
data/lib/hoick/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hoick
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hoick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-01 00:00:00.000000000 Z
11
+ date: 2013-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -24,7 +24,9 @@ dependencies:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.22'
27
- description: A command-line HTTP client
27
+ description: Hoick is a command-line HTTP client. It's intended mainly as a tool
28
+ for testing RESTful APIs, but you can use for something else, if you really want
29
+ to.
28
30
  email:
29
31
  - mdub@dogbiscuit.org
30
32
  executables: