http_mini 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -8
- data/lib/http_mini.rb +49 -27
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48e061b90be3cf85158f267e7b1c0e1a7b832f18
|
4
|
+
data.tar.gz: 24c7c9e5fcbe4f70197b0eae9de2ac5a329ee6a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7c985c1165e701977a4a4aa6c2f67f3a923e7c3b02b79e6763d41acbff1ada36f6ce74eb7766c9acfc714019753987fc4bc82bf7609104f0e7f5f0f2986b131
|
7
|
+
data.tar.gz: 64e7046625a0b4a9d4f63f4bb4a5251692a6209b2a6b5d96a5946a3b96ec84fec9a2d44a5fb7c262c7af3a9142006c0b85e0a88518fb328a75d6c341632cd02a
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Http mini
|
2
2
|
|
3
3
|
A truly minimalist Http client for Ruby. When all you want is
|
4
|
-
a one liner to ping or get the response.
|
4
|
+
a one liner to ping or get the response from a web server.
|
5
5
|
|
6
6
|
## Install
|
7
7
|
|
@@ -9,15 +9,31 @@ a one liner to ping or get the response.
|
|
9
9
|
|
10
10
|
## Usage
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
# Sometimes you just need to know if the site is up and running
|
13
|
+
do_something if HttpMini.new('http://www.acme.com').poke
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
You can also set values for timeouts.
|
15
|
+
# or you want to get the status code
|
16
|
+
puts HttpMini.new('http://www.google.com').head.code
|
18
17
|
|
19
|
-
|
20
|
-
puts
|
18
|
+
# You can also set values for timeouts.
|
19
|
+
puts HttpMini.new('http://www.google.com', {read_timeout: 3, open_timeout: 5}).get.body
|
20
|
+
|
21
|
+
#You can change the url and chain calls
|
22
|
+
http = HttpMini.new('www.apple.com')
|
23
|
+
apple_html = http.get.body
|
24
|
+
ruby_html = http.uri('http://www.ruby-lang.org/en/').get.body
|
25
|
+
|
26
|
+
# or just the path
|
27
|
+
http_request_html = http.path('/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTPRequest.html').get.body
|
28
|
+
|
29
|
+
## HTTP verbs Support
|
30
|
+
|
31
|
+
* HEAD
|
32
|
+
* GET
|
33
|
+
* POST
|
34
|
+
* UPDATE
|
35
|
+
* DELETE
|
36
|
+
* OPTIONS
|
21
37
|
|
22
38
|
## Author
|
23
39
|
|
data/lib/http_mini.rb
CHANGED
@@ -3,7 +3,6 @@ require "uri"
|
|
3
3
|
|
4
4
|
class HttpMini
|
5
5
|
|
6
|
-
attr_reader :uri
|
7
6
|
attr_accessor :opts
|
8
7
|
|
9
8
|
OPEN_TIMEOUT = 2
|
@@ -11,7 +10,7 @@ class HttpMini
|
|
11
10
|
IGNORE_ERROR = true
|
12
11
|
|
13
12
|
def self.VERSION
|
14
|
-
'0.
|
13
|
+
'0.2.0'
|
15
14
|
end
|
16
15
|
|
17
16
|
def initialize(url, opts = {})
|
@@ -20,69 +19,92 @@ class HttpMini
|
|
20
19
|
end
|
21
20
|
|
22
21
|
def head
|
23
|
-
request { |http| http.head(
|
22
|
+
request { |http| http.head(full_path) }
|
24
23
|
end
|
25
24
|
|
26
25
|
def get
|
27
|
-
request { |http| http.get(
|
26
|
+
request { |http| http.get(full_path) }
|
28
27
|
end
|
29
28
|
|
30
29
|
def post(data)
|
31
|
-
request { |http| http.post(
|
30
|
+
request { |http| http.post(full_path, data) }
|
32
31
|
end
|
33
32
|
|
34
33
|
def put(data)
|
35
|
-
request { |http| http.put(
|
34
|
+
request { |http| http.put(full_path, data) }
|
36
35
|
end
|
37
36
|
|
38
37
|
def delete
|
39
|
-
request { |http| http.delete(
|
38
|
+
request { |http| http.delete(full_path) }
|
40
39
|
end
|
41
40
|
|
42
41
|
def options
|
43
|
-
request { |http| http.options(
|
42
|
+
request { |http| http.options(full_path) }
|
44
43
|
end
|
45
44
|
|
46
|
-
def
|
47
|
-
success? head
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
|
52
|
-
def request
|
45
|
+
def poke
|
53
46
|
begin
|
54
|
-
|
47
|
+
success? head
|
55
48
|
rescue Exception => e
|
56
49
|
raise e unless ignore_error?
|
50
|
+
false
|
57
51
|
end
|
58
52
|
end
|
53
|
+
alias_method :ping, :poke
|
54
|
+
|
55
|
+
def uri(uri=nil)
|
56
|
+
uri.nil? ? @uri : (self.uri = uri) and self
|
57
|
+
end
|
58
|
+
|
59
|
+
def host
|
60
|
+
@uri.host || @uri.path.split('/').first
|
61
|
+
end
|
62
|
+
|
63
|
+
def port
|
64
|
+
@uri.port
|
65
|
+
end
|
66
|
+
|
67
|
+
def path(path=nil)
|
68
|
+
path.nil? ? clean_path(@uri.path) : set_path(path)
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
59
72
|
|
60
73
|
def uri=(uri)
|
61
|
-
@uri = URI.parse(uri)
|
74
|
+
@uri = URI.parse(uri) unless uri.nil? || uri.empty?
|
75
|
+
end
|
76
|
+
|
77
|
+
def request
|
78
|
+
Net::HTTP.start(host, port, :use_ssl => ssl?) {|http| set_timeout(http) and yield(http) }
|
62
79
|
end
|
63
80
|
|
64
81
|
def ssl?
|
65
|
-
uri.scheme == 'https'
|
82
|
+
@uri.scheme == 'https'
|
66
83
|
end
|
67
84
|
|
68
85
|
def set_timeout(http)
|
69
86
|
http.open_timeout, http.read_timeout = timeouts
|
70
87
|
end
|
71
88
|
|
72
|
-
def
|
73
|
-
uri.
|
89
|
+
def full_path
|
90
|
+
@uri.query ? path + '?' + @uri.query : path
|
74
91
|
end
|
75
92
|
|
76
|
-
def
|
77
|
-
|
93
|
+
def clean_path(path)
|
94
|
+
default_path remove_host_from_path(path)
|
95
|
+
end
|
96
|
+
|
97
|
+
def default_path(path)
|
98
|
+
path.to_s.empty? ? '/' : path
|
78
99
|
end
|
79
100
|
|
80
|
-
def path
|
81
|
-
|
101
|
+
def remove_host_from_path(path)
|
102
|
+
path.to_s.gsub Regexp.new('^' + host), ''
|
82
103
|
end
|
83
104
|
|
84
|
-
def
|
85
|
-
path
|
105
|
+
def set_path(path)
|
106
|
+
@uri.path = path
|
107
|
+
self
|
86
108
|
end
|
87
109
|
|
88
110
|
def timeouts
|
@@ -102,7 +124,7 @@ class HttpMini
|
|
102
124
|
end
|
103
125
|
|
104
126
|
def success?(response)
|
105
|
-
response && response.code.to_i >= 200 && response.code.to_i <
|
127
|
+
response && response.code.to_i >= 200 && response.code.to_i < 400
|
106
128
|
end
|
107
129
|
|
108
130
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_mini
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jerome Touffe-Blin
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 1.3.0
|
27
|
-
description: A
|
27
|
+
description: A thin wrapper over URI and Http::Net
|
28
28
|
email: jtblin@gmail.com
|
29
29
|
executables: []
|
30
30
|
extensions: []
|