hoick 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YjJiNTY3M2ZhZTNmYmY0YTNmMTRjODg0ZmNhYjRjOTczM2MzMjMzYQ==
5
- data.tar.gz: !binary |-
6
- Y2U0NDVhOTAyYzhhZjM1MmY5NzY4ZTQ0YzMxMmUzN2FkMWZlNGFmNA==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- ZTZiNzllYTIyNTlhMDNhNzA4OTU5MTdmMmUwNDRhMzlhNjA1ZDFlZjA0ODM4
10
- ZjU2YjE1NTRhYTZhODM3ZGU4MDJkZTM5NzAwMjNkZjg1NWZlYmRjMjgzYTAw
11
- ZWJkODlhMmE5MWM0NTFjYmY2MjM3Yzk3YjM5NmIzNGMyMjU2Nzk=
12
- data.tar.gz: !binary |-
13
- ZmZkZDM0MWUzMDM3NTBhY2E5M2Y3YWRhYWE5MjljNWViMThkMTk1NDdmYzI0
14
- MTY1YTFmNmE2ZWNkOTE4ZTA2OTliN2RjNjQwYmE5MzM5YzFkMTRhNjBlZTFj
15
- ZDlmYTNhY2E1OTIyYjQ2YTBiN2M4Y2JlNTEyMTNjOTViZDQ1ZWY=
2
+ SHA1:
3
+ metadata.gz: b2771b8314e1b92ef811e38eab01de8f9bd227c1
4
+ data.tar.gz: 561c3d1f37cdc2f8d43dc0581c96e3c6e21096cf
5
+ SHA512:
6
+ metadata.gz: f3b958749de72a41427fac673f5997a4d626136a8144d1f5046657d9e7830f547956a233c34c09fc6076e6b433fc348f20cb28db6d12ea2aeb6546e6046697eb
7
+ data.tar.gz: 66bb7e6847cf8435f0f1f44d33df706146f09c0c16534745c1eae334f9bccc59e9afffca14488e0d41cef39af527338fd09844fd76d1b71602fcd95244a01b2b
@@ -18,5 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_runtime_dependency "mime-types", "~> 1.22"
22
+
22
23
  end
@@ -1,10 +1,13 @@
1
1
  require "clamp"
2
+ require "mime/types"
2
3
  require "net/http"
3
4
 
4
5
  module Hoick
5
6
 
6
7
  class Command < Clamp::Command
7
8
 
9
+ option ["--base-url"], "URL", "base URL"
10
+
8
11
  option ["-b", "--[no-]body"], :flag, "display response body", :default => true
9
12
  option ["-h", "--[no-]headers"], :flag, "display response status and headers"
10
13
 
@@ -17,13 +20,13 @@ module Hoick
17
20
  parameter "URL", "address"
18
21
 
19
22
  def execute
20
- get_with_redirects(url) do |response|
23
+ get_with_redirects do |response|
21
24
  display_response(response)
22
25
  end
23
26
  end
24
27
 
25
- def get_with_redirects(url, &callback)
26
- with_connection_to(url) do |http, uri|
28
+ def get_with_redirects(&callback)
29
+ with_http_connection do |http, uri|
27
30
  http.request_get(uri.request_uri) do |response|
28
31
  if follow? && response.kind_of?(Net::HTTPRedirection)
29
32
  get_with_redirects(response['location'], &callback)
@@ -36,37 +39,87 @@ module Hoick
36
39
 
37
40
  end
38
41
 
42
+ module PayloadOptions
43
+
44
+ extend Clamp::Option::Declaration
45
+
46
+ option ["-F", "--file"], "FILE", "input file"
47
+ option ["-T", "--content-type"], "TYPE", "payload Content-Type" do |arg|
48
+ if arg.index("/")
49
+ arg
50
+ else
51
+ mime_type_of(arg) || raise(ArgumentError, "unrecognised type: #{arg.inspect}")
52
+ end
53
+ end
54
+
55
+ def payload
56
+ if file
57
+ File.read(file)
58
+ else
59
+ $stdin.read
60
+ end
61
+ end
62
+
63
+ protected
64
+
65
+ def default_content_type
66
+ (mime_type_of(file) if file) || "application/octet-stream"
67
+ end
68
+
69
+ def mime_type_of(filename_or_ext)
70
+ resolved_type = MIME::Types.of(filename_or_ext).first
71
+ resolved_type.to_s if resolved_type
72
+ end
73
+
74
+ end
75
+
76
+ subcommand ["post", "POST"], "HTTP POST" do
77
+
78
+ include PayloadOptions
79
+
80
+ parameter "URL", "address"
81
+
82
+ def execute
83
+ content = payload
84
+ with_http_connection do |http, uri|
85
+ post = Net::HTTP::Post.new(uri.request_uri)
86
+ post["Content-Type"] = content_type
87
+ post.body = content
88
+ http.request(post) do |response|
89
+ display_response(response)
90
+ end
91
+ end
92
+ end
93
+
94
+ end
95
+
39
96
  subcommand ["put", "PUT"], "HTTP PUT" do
40
97
 
98
+ include PayloadOptions
99
+
41
100
  parameter "URL", "address"
42
- parameter "[FILE]", "file to upload"
43
101
 
44
102
  def execute
45
- content = read_content
46
- with_connection_to(url) do |http, uri|
103
+ content = payload
104
+ with_http_connection do |http, uri|
47
105
  put = Net::HTTP::Put.new(uri.request_uri)
106
+ put["Content-Type"] = content_type
48
107
  put.body = content
49
- put["Content-Type"] = "application/octet-stream"
50
108
  http.request(put) do |response|
51
109
  display_response(response)
52
110
  end
53
111
  end
54
112
  end
55
113
 
56
- def read_content
57
- if file
58
- File.read(file)
59
- else
60
- $stdin.read
61
- end
62
- end
63
-
64
114
  end
65
115
 
66
116
  private
67
117
 
68
- def with_connection_to(url)
69
- uri = URI(url)
118
+ def uri
119
+ URI(base_url) + URI(url)
120
+ end
121
+
122
+ def with_http_connection
70
123
  http = Net::HTTP.new(uri.host, uri.port)
71
124
  http.set_debug_output($stderr) if debug?
72
125
  http.start do
@@ -1,3 +1,3 @@
1
1
  module Hoick
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hoick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-03-28 00:00:00.000000000 Z
11
+ date: 2013-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- prerelease: false
15
- version_requirements: !ruby/object:Gem::Requirement
14
+ name: mime-types
15
+ requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
20
- name: bundler
21
- requirement: !ruby/object:Gem::Requirement
19
+ version: '1.22'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
22
23
  requirements:
23
24
  - - ~>
24
25
  - !ruby/object:Gem::Version
25
- version: '1.3'
26
- type: :development
26
+ version: '1.22'
27
27
  description: A command-line HTTP client
28
28
  email:
29
29
  - mdub@dogbiscuit.org
@@ -51,12 +51,12 @@ require_paths:
51
51
  - lib
52
52
  required_ruby_version: !ruby/object:Gem::Requirement
53
53
  requirements:
54
- - - ! '>='
54
+ - - '>='
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
57
  required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ! '>='
59
+ - - '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  requirements: []