hippie 0.6.0 → 0.6.1
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 +4 -4
- metadata +5 -33
- data/LICENSE +0 -20
- data/README.md +0 -61
- data/hippie.gemspec +0 -23
- data/lib/cacert.pem +0 -5026
- data/lib/hippie.rb +0 -89
- data/lib/hippie/response.rb +0 -43
- data/tests/hippie_test.rb +0 -81
data/lib/hippie.rb
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'net/http'
|
4
|
-
require 'openssl'
|
5
|
-
require_relative 'hippie/response'
|
6
|
-
|
7
|
-
module Hippie
|
8
|
-
CA_FILE = ENV.fetch('REQUESTS_CA_FILE') {
|
9
|
-
File.expand_path('../cacert.pem', __FILE__)
|
10
|
-
}
|
11
|
-
|
12
|
-
def self.get(url, **kwargs)
|
13
|
-
request('GET', url, **kwargs)
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.post(url, data: nil, **kwargs)
|
17
|
-
request('POST', url, data: data, **kwargs)
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.put(url, data: nil, **kwargs)
|
21
|
-
request('PUT', url, data: data, **kwargs)
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.delete(url, **kwargs)
|
25
|
-
request('DELETE', url, **kwargs)
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.head(url, **kwargs)
|
29
|
-
request('HEAD', url, **kwargs)
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.options(url, **kwargs)
|
33
|
-
request('OPTIONS', url, **kwargs)
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.patch(url, **kwargs)
|
37
|
-
request('PATCH', url, **kwargs)
|
38
|
-
end
|
39
|
-
|
40
|
-
def self.trace(url, **kwargs)
|
41
|
-
request('TRACE', url, **kwargs)
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
|
46
|
-
def self.request(method, url, headers: {}, data: nil, params: nil, auth: nil)
|
47
|
-
uri = URI.parse(URI.encode(url))
|
48
|
-
|
49
|
-
unless ['http', 'https'].include? uri.scheme
|
50
|
-
fail URI::InvalidURIError, "No HTTP(S) scheme in: #{url}"
|
51
|
-
end
|
52
|
-
|
53
|
-
uri.query = URI.encode_www_form(params) if params
|
54
|
-
body = process_params(headers: headers, data: data) if data
|
55
|
-
basic_auth(headers, *auth) if auth
|
56
|
-
|
57
|
-
response = Net::HTTP.start(uri.host, uri.port, opts(uri)) do |http|
|
58
|
-
if method == 'HEAD'
|
59
|
-
http.head(uri)
|
60
|
-
else
|
61
|
-
http.send_request(method, uri, body, headers)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
Hippie::Response.new(response.code, response.to_hash, response.body)
|
66
|
-
end
|
67
|
-
|
68
|
-
def self.opts(uri)
|
69
|
-
if uri.scheme == 'https'
|
70
|
-
{ use_ssl: true,
|
71
|
-
verify_mode: OpenSSL::SSL::VERIFY_PEER,
|
72
|
-
ca_file: CA_FILE
|
73
|
-
}
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def self.basic_auth(headers, user, pass)
|
78
|
-
headers['Authorization'] = 'Basic ' + ["#{user}:#{pass}"].pack('m0')
|
79
|
-
end
|
80
|
-
|
81
|
-
def self.process_params(headers: nil, data: nil)
|
82
|
-
if data.is_a?(Enumerable)
|
83
|
-
headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
84
|
-
URI.encode_www_form(data)
|
85
|
-
else
|
86
|
-
data
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
data/lib/hippie/response.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
module Hippie
|
6
|
-
class Response
|
7
|
-
attr_reader :status, :headers, :body
|
8
|
-
|
9
|
-
def initialize(status, headers, body)
|
10
|
-
@status = status.to_i
|
11
|
-
@headers = headers
|
12
|
-
@body = body.to_s
|
13
|
-
end
|
14
|
-
|
15
|
-
def json
|
16
|
-
JSON.parse(@body)
|
17
|
-
end
|
18
|
-
|
19
|
-
def information?
|
20
|
-
@status.between?(100, 199)
|
21
|
-
end
|
22
|
-
|
23
|
-
def success?
|
24
|
-
@status.between?(200, 299)
|
25
|
-
end
|
26
|
-
|
27
|
-
def redirection?
|
28
|
-
@status.between?(300, 399)
|
29
|
-
end
|
30
|
-
|
31
|
-
def client_error?
|
32
|
-
@status.between?(400, 499)
|
33
|
-
end
|
34
|
-
|
35
|
-
def server_error?
|
36
|
-
@status.between?(500, 599)
|
37
|
-
end
|
38
|
-
|
39
|
-
def error?
|
40
|
-
!success?
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
data/tests/hippie_test.rb
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'minitest/autorun'
|
4
|
-
require_relative '../lib/hippie'
|
5
|
-
|
6
|
-
class TestHippie < Minitest::Test
|
7
|
-
def test_get
|
8
|
-
r = Hippie.get('http://httpbin.org/get', params: { foo: 'bar' })
|
9
|
-
|
10
|
-
assert_equal true, r.success?
|
11
|
-
assert_equal ['application/json'], r.headers['content-type']
|
12
|
-
assert(r.json['args'] && r.json['args']['foo'] == 'bar')
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_post
|
16
|
-
r = Hippie.post('http://httpbin.org/post', data: { 'plan' => 'test' })
|
17
|
-
|
18
|
-
assert_equal true, r.success?
|
19
|
-
assert_equal ['application/json'], r.headers['content-type']
|
20
|
-
assert(r.json['form'] && r.json['form'] == { 'plan' => 'test' })
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_put
|
24
|
-
r = Hippie.put('http://httpbin.org/put', data: { 'plan' => 'test' })
|
25
|
-
|
26
|
-
assert_equal true, r.success?
|
27
|
-
assert_equal ['application/json'], r.headers['content-type']
|
28
|
-
assert(r.json['form'] && r.json['form'] == { 'plan' => 'test' })
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_delete
|
32
|
-
r = Hippie.delete('http://httpbin.org/delete', data: { 'plan' => 'test' })
|
33
|
-
|
34
|
-
assert_equal true, r.success?
|
35
|
-
assert_equal ['application/json'], r.headers['content-type']
|
36
|
-
assert(r.json['form'] && r.json['form'] == { 'plan' => 'test' })
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_head
|
40
|
-
r = Hippie.head('http://httpbin.org/headers')
|
41
|
-
|
42
|
-
assert_equal true, r.success?
|
43
|
-
assert_equal ['application/json'], r.headers['content-type']
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_options
|
47
|
-
# TODO
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_patch
|
51
|
-
r = Hippie.patch('http://httpbin.org/patch', data: { 'plan' => 'test' })
|
52
|
-
|
53
|
-
assert_equal true, r.success?
|
54
|
-
assert_equal ['application/json'], r.headers['content-type']
|
55
|
-
assert(r.json['form'] && r.json['form'] == { 'plan' => 'test' })
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_trace
|
59
|
-
# TODO
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_basic_auth
|
63
|
-
r = Hippie.get('http://httpbin.org/basic-auth/u/p', auth: ['u', 'p'])
|
64
|
-
|
65
|
-
assert_equal true, r.success?
|
66
|
-
assert_equal true, r.json['authenticated']
|
67
|
-
assert_equal 'u', r.json['user']
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_ssl
|
71
|
-
r = Hippie.get('https://httpbin.org/get')
|
72
|
-
|
73
|
-
assert_equal true, r.success?
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_raises_invalid_uri
|
77
|
-
assert_raises(URI::InvalidURIError) {
|
78
|
-
Hippie.get('www.httpbin.org/get')
|
79
|
-
}
|
80
|
-
end
|
81
|
-
end
|