hippie 0.5.0 → 0.6.0
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
- data/README.md +32 -12
- data/hippie.gemspec +4 -4
- data/lib/hippie/response.rb +43 -0
- data/lib/hippie.rb +11 -32
- data/tests/hippie_test.rb +60 -69
- metadata +13 -13
- data/makefile +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26fcf855feebd6241785015a3171ff6a2c1d3d3d
|
4
|
+
data.tar.gz: d93ced984c7eaf59f8806c2c393ed673f438e0e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f985d283e1e55552b262de5f0cb16ad9e35c851fcdba049f197619d4e36d990ba1a5872b102b1f7c903afbb0877ea706c630ebc7170f5436cf30a1fefe23409
|
7
|
+
data.tar.gz: f0e1e7bf89aa8568bb96ed15de29233e0f993bcad952d808710cf3f68353ac370de2da50a72fc6b966052f557a179ed4071fc330cdf79b1d867bea9fd5a39b38
|
data/README.md
CHANGED
@@ -1,4 +1,10 @@
|
|
1
|
-
# Hippie
|
1
|
+
# Hippie
|
2
|
+
### _Simple HTTP client for ruby_
|
3
|
+
---
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install hippie
|
2
8
|
|
3
9
|
## Usage
|
4
10
|
Here's an example of a GET request:
|
@@ -6,36 +12,50 @@ Here's an example of a GET request:
|
|
6
12
|
```ruby
|
7
13
|
require 'hippie'
|
8
14
|
|
9
|
-
response = Hippie.get(
|
15
|
+
response = Hippie.get('http://example.com')
|
16
|
+
response.class #=> Hippie::Response
|
10
17
|
|
11
|
-
# Now you have these methods available
|
12
18
|
response.status #=> Number with the status code
|
13
19
|
response.headers #=> Hash with the response headers
|
14
20
|
response.body #=> String with the response body
|
15
|
-
response.
|
16
|
-
|
21
|
+
response.json #=> String with the response body parsed to json
|
22
|
+
|
23
|
+
response.information? #=> 1xx response
|
24
|
+
response.success? #=> 2xx response
|
25
|
+
response.redirection? #=> 3xx response
|
26
|
+
response.client_error? #=> 4xx response
|
27
|
+
response.server_error? #=> 5xx response
|
28
|
+
response.error? #=> 4xx or 5xx
|
17
29
|
```
|
18
30
|
|
19
31
|
You can also pass parameters with a query string:
|
20
32
|
|
21
33
|
```ruby
|
22
|
-
|
23
|
-
Hippie.get("http://example.com", params: { foo: "bar" })
|
34
|
+
Hippie.get('http://example.com', params: { foo: 'bar' })
|
24
35
|
```
|
25
36
|
|
26
|
-
If you want to send data with a POST request, you can add a `data`
|
27
|
-
option with the value.
|
37
|
+
If you want to send data with a POST request, you can add a `data` option with the value.
|
28
38
|
|
29
39
|
```ruby
|
30
|
-
Hippie.post(
|
40
|
+
Hippie.post('http://example.com', data: 'hello world')
|
31
41
|
```
|
32
42
|
|
33
43
|
For Basic Authentication, you can provide the option `auth`, which
|
34
44
|
should contain an array with the username and password:
|
35
45
|
|
36
46
|
```ruby
|
37
|
-
Hippie.get(
|
47
|
+
Hippie.get('http://example.com', auth: ['username', 'password'])
|
38
48
|
```
|
39
49
|
|
40
|
-
|
50
|
+
## Available methods
|
51
|
+
- GET
|
52
|
+
- POST
|
53
|
+
- PUT
|
54
|
+
- DELETE
|
55
|
+
- HEAD
|
56
|
+
- OPTIONS
|
57
|
+
- PATCH
|
58
|
+
- TRACE
|
59
|
+
|
60
|
+
### Disclaimer
|
41
61
|
This is a fork of the awesome 'requests' gem from cyx (https://github.com/cyx/requests)
|
data/hippie.gemspec
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'hippie'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '0.6.0'
|
4
4
|
s.summary = 'Simple HTTP'
|
5
|
-
s.description = 'Simple
|
5
|
+
s.description = 'Simple HTTP client for Ruby'
|
6
6
|
s.author = 'Martin Manelli'
|
7
7
|
s.email = 'manelli.ml@gmail.com'
|
8
8
|
s.homepage = 'http://github.com/manelli/hippie'
|
@@ -12,12 +12,12 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.files = Dir[
|
13
13
|
'LICENSE',
|
14
14
|
'README.md',
|
15
|
-
'makefile',
|
16
15
|
'lib/hippie.rb',
|
17
16
|
'lib/cacert.pem',
|
17
|
+
'lib/hippie/response.rb',
|
18
18
|
'tests/hippie_test.rb',
|
19
19
|
'hippie.gemspec'
|
20
20
|
]
|
21
21
|
|
22
|
-
s.add_development_dependency '
|
22
|
+
s.add_development_dependency 'minitest', '>= 2.5', '< 5.0'
|
23
23
|
end
|
@@ -0,0 +1,43 @@
|
|
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/lib/hippie.rb
CHANGED
@@ -1,36 +1,13 @@
|
|
1
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
2
3
|
require 'net/http'
|
3
4
|
require 'openssl'
|
4
|
-
|
5
|
+
require_relative 'hippie/response'
|
5
6
|
|
6
7
|
module Hippie
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
def initialize(status, headers, body)
|
11
|
-
@status = status.to_i
|
12
|
-
@headers = headers
|
13
|
-
@body = body || ''
|
14
|
-
end
|
15
|
-
|
16
|
-
def json
|
17
|
-
JSON.parse(@body)
|
18
|
-
end
|
19
|
-
|
20
|
-
def encoding
|
21
|
-
@body.encoding
|
22
|
-
end
|
23
|
-
|
24
|
-
def valid?
|
25
|
-
@status.between?(200, 299)
|
26
|
-
end
|
27
|
-
|
28
|
-
def error?
|
29
|
-
!valid?
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
CA_FILE = ENV.fetch('REQUESTS_CA_FILE') { File.expand_path('../cacert.pem', __FILE__) }
|
8
|
+
CA_FILE = ENV.fetch('REQUESTS_CA_FILE') {
|
9
|
+
File.expand_path('../cacert.pem', __FILE__)
|
10
|
+
}
|
34
11
|
|
35
12
|
def self.get(url, **kwargs)
|
36
13
|
request('GET', url, **kwargs)
|
@@ -67,10 +44,12 @@ module Hippie
|
|
67
44
|
private
|
68
45
|
|
69
46
|
def self.request(method, url, headers: {}, data: nil, params: nil, auth: nil)
|
70
|
-
fail "No HTTP(S) scheme in: #{url}" unless url =~ /^https?:\/\//i
|
71
|
-
|
72
47
|
uri = URI.parse(URI.encode(url))
|
73
48
|
|
49
|
+
unless ['http', 'https'].include? uri.scheme
|
50
|
+
fail URI::InvalidURIError, "No HTTP(S) scheme in: #{url}"
|
51
|
+
end
|
52
|
+
|
74
53
|
uri.query = URI.encode_www_form(params) if params
|
75
54
|
body = process_params(headers: headers, data: data) if data
|
76
55
|
basic_auth(headers, *auth) if auth
|
@@ -83,7 +62,7 @@ module Hippie
|
|
83
62
|
end
|
84
63
|
end
|
85
64
|
|
86
|
-
Response.new(response.code, response.to_hash, response.body)
|
65
|
+
Hippie::Response.new(response.code, response.to_hash, response.body)
|
87
66
|
end
|
88
67
|
|
89
68
|
def self.opts(uri)
|
data/tests/hippie_test.rb
CHANGED
@@ -1,90 +1,81 @@
|
|
1
|
-
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require_relative '../lib/hippie'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
class TestHippie < Minitest::Test
|
7
|
+
def test_get
|
8
|
+
r = Hippie.get('http://httpbin.org/get', params: { foo: 'bar' })
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
test 'POST' do
|
15
|
-
r = Hippie.post('http://httpbin.org/post', data: { "plan" => "test" })
|
16
|
-
|
17
|
-
assert_equal 200, r.status
|
18
|
-
assert_equal ['application/json'], r.headers['content-type']
|
19
|
-
assert_equal 'UTF-8', r.encoding.to_s
|
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
|
20
14
|
|
21
|
-
|
22
|
-
|
23
|
-
puts 'POST'
|
24
|
-
|
25
|
-
test 'PUT' do
|
26
|
-
r = Hippie.put('http://httpbin.org/put', data: { 'plan' => 'test' })
|
15
|
+
def test_post
|
16
|
+
r = Hippie.post('http://httpbin.org/post', data: { 'plan' => 'test' })
|
27
17
|
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
31
22
|
|
32
|
-
|
33
|
-
|
34
|
-
puts 'PUT'
|
23
|
+
def test_put
|
24
|
+
r = Hippie.put('http://httpbin.org/put', data: { 'plan' => 'test' })
|
35
25
|
|
36
|
-
|
37
|
-
|
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
|
38
30
|
|
39
|
-
|
40
|
-
|
41
|
-
assert_equal 'UTF-8', r.encoding.to_s
|
31
|
+
def test_delete
|
32
|
+
r = Hippie.delete('http://httpbin.org/delete', data: { 'plan' => 'test' })
|
42
33
|
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
46
38
|
|
47
|
-
|
48
|
-
|
39
|
+
def test_head
|
40
|
+
r = Hippie.head('http://httpbin.org/headers')
|
49
41
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
54
|
-
puts 'HEAD'
|
42
|
+
assert_equal true, r.success?
|
43
|
+
assert_equal ['application/json'], r.headers['content-type']
|
44
|
+
end
|
55
45
|
|
56
|
-
|
57
|
-
|
58
|
-
end
|
59
|
-
puts 'OPTIONS'
|
46
|
+
def test_options
|
47
|
+
# TODO
|
48
|
+
end
|
60
49
|
|
61
|
-
|
62
|
-
|
50
|
+
def test_patch
|
51
|
+
r = Hippie.patch('http://httpbin.org/patch', data: { 'plan' => 'test' })
|
63
52
|
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
67
57
|
|
68
|
-
|
69
|
-
|
70
|
-
|
58
|
+
def test_trace
|
59
|
+
# TODO
|
60
|
+
end
|
71
61
|
|
72
|
-
|
73
|
-
|
74
|
-
end
|
75
|
-
puts 'TRACE'
|
62
|
+
def test_basic_auth
|
63
|
+
r = Hippie.get('http://httpbin.org/basic-auth/u/p', auth: ['u', 'p'])
|
76
64
|
|
77
|
-
|
78
|
-
|
65
|
+
assert_equal true, r.success?
|
66
|
+
assert_equal true, r.json['authenticated']
|
67
|
+
assert_equal 'u', r.json['user']
|
68
|
+
end
|
79
69
|
|
80
|
-
|
81
|
-
|
82
|
-
end
|
83
|
-
puts 'Basic Auth'
|
70
|
+
def test_ssl
|
71
|
+
r = Hippie.get('https://httpbin.org/get')
|
84
72
|
|
85
|
-
|
86
|
-
|
73
|
+
assert_equal true, r.success?
|
74
|
+
end
|
87
75
|
|
88
|
-
|
76
|
+
def test_raises_invalid_uri
|
77
|
+
assert_raises(URI::InvalidURIError) {
|
78
|
+
Hippie.get('www.httpbin.org/get')
|
79
|
+
}
|
80
|
+
end
|
89
81
|
end
|
90
|
-
puts 'SSL'
|
metadata
CHANGED
@@ -1,36 +1,36 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hippie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Manelli
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: minitest
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.2'
|
20
17
|
- - ">="
|
21
18
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
19
|
+
version: '2.5'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5.0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
|
-
- - "~>"
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '1.2'
|
30
27
|
- - ">="
|
31
28
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
33
|
-
|
29
|
+
version: '2.5'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5.0'
|
33
|
+
description: Simple HTTP client for Ruby
|
34
34
|
email: manelli.ml@gmail.com
|
35
35
|
executables: []
|
36
36
|
extensions: []
|
@@ -41,7 +41,7 @@ files:
|
|
41
41
|
- hippie.gemspec
|
42
42
|
- lib/cacert.pem
|
43
43
|
- lib/hippie.rb
|
44
|
-
-
|
44
|
+
- lib/hippie/response.rb
|
45
45
|
- tests/hippie_test.rb
|
46
46
|
homepage: http://github.com/manelli/hippie
|
47
47
|
licenses:
|
data/makefile
DELETED