unirest 1.0.8 → 1.1.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 +7 -7
- data/.gitignore +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +133 -0
- data/Rakefile +10 -0
- data/lib/unirest.rb +39 -21
- data/lib/unirest/http_request.rb +39 -5
- data/test/unirest_test.rb +176 -0
- data/unirest.gemspec +25 -0
- metadata +115 -51
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
---
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: a70b4b60495cfce6b76976db30267f96cb0a3d1c
|
|
4
|
+
data.tar.gz: cee47e365d0051e7eb4dbe3855c24f2f85407cb0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7ab63e4a645092001bbbf31735a1604192bd616235a7da27f090a0c0e5599efd5416f51aff7863a550d3e16fc779f513248012cb4aeae6b20e9bd41fca620b99
|
|
7
|
+
data.tar.gz: ddff0fcb181e953a92e73fc6cff2abae915d01db67f859f5f18b0d794621b6bbc04cd434cf2fcdfbcb3616bbc2eb3fc12b83529479d076558f2270668ed8aafa
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013 Mashape (http://mashape.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# Unirest for Ruby [](https://travis-ci.org/Mashape/unirest-ruby)
|
|
2
|
+
|
|
3
|
+
Unirest is a set of lightweight HTTP libraries available in multiple languages, ideal for most applications:
|
|
4
|
+
|
|
5
|
+
* Make `GET`, `POST`, `PUT`, `PATCH`, `DELETE` requests
|
|
6
|
+
* Both syncronous and asynchronous (non-blocking) requests
|
|
7
|
+
* It supports form parameters, file uploads and custom body entities
|
|
8
|
+
* Supports gzip
|
|
9
|
+
* Supports Basic Authentication natively
|
|
10
|
+
* Customizable timeout
|
|
11
|
+
* Customizable default headers for every request (DRY)
|
|
12
|
+
* Automatic JSON parsing into a native object for JSON responses
|
|
13
|
+
|
|
14
|
+
Created with love by [thefosk](https://github.com/thefosk) @ [mashape.com](https://mashape.com)
|
|
15
|
+
|
|
16
|
+
## Installing
|
|
17
|
+
|
|
18
|
+
Requirements: **Ruby >= 2.0**
|
|
19
|
+
|
|
20
|
+
To utilize unirest, install the `unirest` gem:
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
gem install unirest
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
After installing the gem package you can now begin to simplifying requests by requiring `unirest`:
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
require 'unirest'
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Creating Requests
|
|
33
|
+
|
|
34
|
+
So you're probably wondering how using Unirest makes creating requests in Ruby easier, let's start with a working example:
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
response = Unirest.post "http://httpbin.org/post",
|
|
38
|
+
headers:{ "Accept" => "application/json" },
|
|
39
|
+
parameters:{ :age => 23, :foo => "bar" }
|
|
40
|
+
|
|
41
|
+
response.code # Status code
|
|
42
|
+
response.headers # Response headers
|
|
43
|
+
response.body # Parsed body
|
|
44
|
+
response.raw_body # Unparsed body
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Asynchronous Requests
|
|
48
|
+
Unirest-Ruby also supports asynchronous requests with a callback function specified inside a block, like:
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
response = Unirest.post "http://httpbin.org/post",
|
|
52
|
+
headers:{ "Accept" => "application/json" },
|
|
53
|
+
parameters:{ :age => 23, :foo => "bar" } {|response|
|
|
54
|
+
response.code # Status code
|
|
55
|
+
response.headers # Response headers
|
|
56
|
+
response.body # Parsed body
|
|
57
|
+
response.raw_body # Unparsed body
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## File Uploads
|
|
62
|
+
```ruby
|
|
63
|
+
response = Unirest.post "http://httpbin.org/post",
|
|
64
|
+
headers:{ "Accept" => "application/json" },
|
|
65
|
+
parameters:{ :age => 23, :file => File.new("/path/to/file", 'rb') }
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Custom Entity Body
|
|
69
|
+
```ruby
|
|
70
|
+
response = Unirest.post "http://httpbin.org/post",
|
|
71
|
+
headers:{ "Accept" => "application/json" },
|
|
72
|
+
parameters:{ :age => "value", :foo => "bar" }.to_json # Converting the Hash to a JSON string
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Basic Authentication
|
|
76
|
+
|
|
77
|
+
Authenticating the request with basic authentication can be done by providing an `auth` Hash with `:user` and `:password` keys like:
|
|
78
|
+
|
|
79
|
+
```ruby
|
|
80
|
+
response = Unirest.get "http://httpbin.org/get", auth:{:user=>"username", :password=>"password"}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
# Request
|
|
84
|
+
```ruby
|
|
85
|
+
Unirest.get(url, headers: {}, parameters: nil, auth:nil, &callback)
|
|
86
|
+
Unirest.post(url, headers: {}, parameters: nil, auth:nil, &callback)
|
|
87
|
+
Unirest.delete(url, headers: {}, parameters: nil, auth:nil, &callback)
|
|
88
|
+
Unirest.put(url, headers: {}, parameters: nil, auth:nil, &callback)
|
|
89
|
+
Unirest.patch(url, headers: {}, parameters: nil, auth:nil, &callback)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
- `url` (`String`) - Endpoint, address, or uri to be acted upon and requested information from.
|
|
93
|
+
- `headers` (`Object`) - Request Headers as associative array or object
|
|
94
|
+
- `parameters` (`Array` | `Object` | `String`) - Request Body associative array or object
|
|
95
|
+
- `callback` (`Function`) - _Optional_; Asychronous callback method to be invoked upon result.
|
|
96
|
+
|
|
97
|
+
# Response
|
|
98
|
+
Upon receiving a response Unirest returns the result in the form of an Object, this object should always have the same keys for each language regarding to the response details.
|
|
99
|
+
|
|
100
|
+
- `code` - HTTP Response Status Code (Example `200`)
|
|
101
|
+
- `headers` - HTTP Response Headers
|
|
102
|
+
- `body` - Parsed response body where applicable, for example JSON responses are parsed to Objects / Associative Arrays.
|
|
103
|
+
- `raw_body` - Un-parsed response body
|
|
104
|
+
|
|
105
|
+
# Advanced Configuration
|
|
106
|
+
|
|
107
|
+
You can set some advanced configuration to tune Unirest-Ruby:
|
|
108
|
+
|
|
109
|
+
### Timeout
|
|
110
|
+
|
|
111
|
+
You can set a custom timeout value (in **seconds**):
|
|
112
|
+
|
|
113
|
+
```ruby
|
|
114
|
+
Unirest.timeout(5) # 5s timeout
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Default Request Headers
|
|
118
|
+
|
|
119
|
+
You can set default headers that will be sent on every request:
|
|
120
|
+
|
|
121
|
+
```ruby
|
|
122
|
+
Unirest.default_header('Header1','Value1')
|
|
123
|
+
Unirest.default_header('Header2','Value2')
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
You can clear the default headers anytime with:
|
|
127
|
+
|
|
128
|
+
```ruby
|
|
129
|
+
Unirest.clear_default_headers()
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
data/Rakefile
ADDED
data/lib/unirest.rb
CHANGED
|
@@ -30,41 +30,47 @@ require File.join(File.dirname(__FILE__), "/unirest/http_response.rb")
|
|
|
30
30
|
|
|
31
31
|
module Unirest
|
|
32
32
|
|
|
33
|
-
USER_AGENT = "unirest-ruby/1.
|
|
33
|
+
USER_AGENT = "unirest-ruby/1.1"
|
|
34
|
+
|
|
35
|
+
@@timeout = 10
|
|
36
|
+
@@default_headers = {}
|
|
34
37
|
|
|
35
38
|
class HttpClient
|
|
36
39
|
|
|
37
|
-
def self.request(method, url, headers, body, &callback)
|
|
38
|
-
http_request = Unirest::HttpRequest.new(method, url, headers, body)
|
|
40
|
+
def self.request(method, url, headers, body, auth, timeout, &callback)
|
|
41
|
+
http_request = Unirest::HttpRequest.new(method, url, headers, body, auth)
|
|
39
42
|
|
|
40
43
|
if callback
|
|
41
44
|
return Thread.new do
|
|
42
|
-
callback.call(self.internal_request(http_request))
|
|
45
|
+
callback.call(self.internal_request(http_request, timeout))
|
|
43
46
|
end
|
|
44
47
|
else
|
|
45
|
-
return self.internal_request(http_request)
|
|
48
|
+
return self.internal_request(http_request, timeout)
|
|
46
49
|
end
|
|
47
50
|
end
|
|
48
51
|
|
|
49
|
-
def self.internal_request(http_request)
|
|
52
|
+
def self.internal_request(http_request, timeout)
|
|
50
53
|
# Set the user agent
|
|
51
54
|
http_request.add_header("user-agent", USER_AGENT)
|
|
55
|
+
http_request.add_header("accept-encoding", "gzip")
|
|
52
56
|
|
|
53
57
|
http_response = nil;
|
|
54
58
|
|
|
55
59
|
begin
|
|
56
60
|
case http_request.method
|
|
57
61
|
when :get
|
|
58
|
-
http_response = RestClient.get http_request.url, http_request.headers
|
|
62
|
+
http_response = RestClient::Request.execute(:method => :get, :url => http_request.url, :headers => http_request.headers, :timeout => timeout)
|
|
59
63
|
when :post
|
|
60
|
-
http_response = RestClient.post http_request.url, http_request.body, http_request.headers
|
|
64
|
+
http_response = RestClient::Request.execute(:method => :post, :url => http_request.url, :payload => http_request.body, :headers => http_request.headers, :timeout => timeout)
|
|
61
65
|
when :put
|
|
62
|
-
http_response = RestClient.put http_request.url, http_request.body, http_request.headers
|
|
66
|
+
http_response = RestClient::Request.execute(:method => :put, :url => http_request.url, :payload => http_request.body, :headers => http_request.headers, :timeout => timeout)
|
|
63
67
|
when :delete
|
|
64
|
-
http_response = RestClient.delete http_request.url, http_request.headers
|
|
68
|
+
http_response = RestClient::Request.execute(:method => :delete, :url => http_request.url, :payload => http_request.body, :headers => http_request.headers, :timeout => timeout)
|
|
65
69
|
when :patch
|
|
66
|
-
http_response = RestClient.patch http_request.url, http_request.body, http_request.headers
|
|
70
|
+
http_response = RestClient::Request.execute(:method => :patch, :url => http_request.url, :payload => http_request.body, :headers => http_request.headers, :timeout => timeout)
|
|
67
71
|
end
|
|
72
|
+
rescue RestClient::RequestTimeout
|
|
73
|
+
raise 'Request Timeout'
|
|
68
74
|
rescue => e
|
|
69
75
|
http_response = e.response
|
|
70
76
|
end
|
|
@@ -74,24 +80,36 @@ module Unirest
|
|
|
74
80
|
|
|
75
81
|
end
|
|
76
82
|
|
|
77
|
-
def self.
|
|
78
|
-
|
|
83
|
+
def self.default_header(name, value)
|
|
84
|
+
@@default_headers[name] = value
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def self.clear_default_headers
|
|
88
|
+
@@default_headers = {}
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def self.timeout(seconds)
|
|
92
|
+
@@timeout = seconds
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def self.get(url, headers: {}, parameters: nil, auth:nil, &callback)
|
|
96
|
+
return HttpClient.request(:get, url, headers.merge(@@default_headers), parameters, auth, @@timeout, &callback)
|
|
79
97
|
end
|
|
80
98
|
|
|
81
|
-
def self.post(url, headers
|
|
82
|
-
return HttpClient.request(:post, url, headers,
|
|
99
|
+
def self.post(url, headers: {}, parameters: nil, auth:nil, &callback)
|
|
100
|
+
return HttpClient.request(:post, url, headers.merge(@@default_headers), parameters, auth, @@timeout, &callback)
|
|
83
101
|
end
|
|
84
102
|
|
|
85
|
-
def self.delete(url, headers
|
|
86
|
-
return HttpClient.request(:delete, url, headers,
|
|
103
|
+
def self.delete(url, headers: {}, parameters: nil, auth:nil, &callback)
|
|
104
|
+
return HttpClient.request(:delete, url, headers.merge(@@default_headers), parameters, auth, @@timeout, &callback)
|
|
87
105
|
end
|
|
88
106
|
|
|
89
|
-
def self.put(url, headers
|
|
90
|
-
return HttpClient.request(:put, url, headers,
|
|
107
|
+
def self.put(url, headers: {}, parameters: nil, auth:nil, &callback)
|
|
108
|
+
return HttpClient.request(:put, url, headers.merge(@@default_headers), parameters, auth, @@timeout, &callback)
|
|
91
109
|
end
|
|
92
110
|
|
|
93
|
-
def self.patch(url, headers
|
|
94
|
-
return HttpClient.request(:patch, url, headers,
|
|
111
|
+
def self.patch(url, headers: {}, parameters: nil, auth:nil, &callback)
|
|
112
|
+
return HttpClient.request(:patch, url, headers.merge(@@default_headers), parameters, auth, @@timeout, &callback)
|
|
95
113
|
end
|
|
96
114
|
|
|
97
115
|
end
|
data/lib/unirest/http_request.rb
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
#
|
|
24
24
|
|
|
25
25
|
require 'addressable/uri'
|
|
26
|
+
require "base64"
|
|
26
27
|
|
|
27
28
|
module Unirest
|
|
28
29
|
|
|
@@ -31,25 +32,58 @@ module Unirest
|
|
|
31
32
|
attr_reader :url
|
|
32
33
|
attr_reader :headers
|
|
33
34
|
attr_reader :body
|
|
35
|
+
attr_reader :auth
|
|
34
36
|
|
|
35
37
|
def add_header(name, value)
|
|
36
38
|
@headers[name] = value
|
|
37
39
|
end
|
|
38
40
|
|
|
39
|
-
def initialize(method, url, headers = {}, body = nil)
|
|
41
|
+
def initialize(method, url, headers = {}, body = nil, auth = nil)
|
|
40
42
|
@method = method
|
|
41
43
|
|
|
42
|
-
|
|
44
|
+
if (method == :get)
|
|
45
|
+
if body.is_a?(Hash) && body.length > 0
|
|
46
|
+
if url.include? "?"
|
|
47
|
+
url += "&"
|
|
48
|
+
else
|
|
49
|
+
url += "?"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
uri = Addressable::URI.new
|
|
53
|
+
uri.query_values = body
|
|
54
|
+
url += uri.query
|
|
55
|
+
end
|
|
56
|
+
else
|
|
57
|
+
@body = body
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
unless url =~ URI.regexp
|
|
43
61
|
raise "Invalid URL: " + url
|
|
44
62
|
end
|
|
45
63
|
|
|
46
|
-
@url =
|
|
64
|
+
@url = url.gsub /\s+/, '%20'
|
|
65
|
+
|
|
47
66
|
@headers = {}
|
|
67
|
+
|
|
68
|
+
if auth != nil && auth.is_a?(Hash)
|
|
69
|
+
user = ""
|
|
70
|
+
password = ""
|
|
71
|
+
if auth[:user] != nil
|
|
72
|
+
user = auth[:user]
|
|
73
|
+
end
|
|
74
|
+
if auth[:password] != nil
|
|
75
|
+
password = auth[:password]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
headers['Authorization'] = "Basic " + Base64.encode64(user + ":" + password)
|
|
79
|
+
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
|
|
48
83
|
# Make the header key lowercase
|
|
49
84
|
headers.each_pair {|key, value| @headers[key.downcase] = value }
|
|
50
|
-
@body = body
|
|
51
85
|
end
|
|
52
86
|
|
|
53
87
|
end
|
|
54
88
|
|
|
55
|
-
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# The MIT License
|
|
2
|
+
#
|
|
3
|
+
# Copyright (c) 2013 Mashape (http://mashape.com)
|
|
4
|
+
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
# a copy of this software and associated documentation files (the
|
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
# the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be
|
|
14
|
+
# included in all copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
#
|
|
24
|
+
|
|
25
|
+
require_relative '../lib/unirest'
|
|
26
|
+
require 'test/unit'
|
|
27
|
+
require 'shoulda'
|
|
28
|
+
|
|
29
|
+
module Unirest
|
|
30
|
+
class RequestsTest < Test::Unit::TestCase
|
|
31
|
+
|
|
32
|
+
should "GET" do
|
|
33
|
+
|
|
34
|
+
response = Unirest.get("http://httpbin.org/get?name=Mark", parameters:{'nick'=> "sinz s"})
|
|
35
|
+
assert response.code == 200
|
|
36
|
+
|
|
37
|
+
args = response.body['args']
|
|
38
|
+
assert args.length == 2
|
|
39
|
+
assert args['name'] == 'Mark'
|
|
40
|
+
assert args['nick'] == 'sinz s'
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
should "POST" do
|
|
45
|
+
|
|
46
|
+
response = Unirest.post("http://httpbin.org/post", parameters:{'name' => 'Mark', 'nick'=> "sinz s"})
|
|
47
|
+
assert response.code == 200
|
|
48
|
+
|
|
49
|
+
args = response.body['form']
|
|
50
|
+
assert args.length == 2
|
|
51
|
+
assert args['name'] == 'Mark'
|
|
52
|
+
assert args['nick'] == 'sinz s'
|
|
53
|
+
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
should "POST custom body" do
|
|
57
|
+
|
|
58
|
+
response = Unirest.post("http://httpbin.org/post", parameters:"hello")
|
|
59
|
+
assert response.code == 200
|
|
60
|
+
|
|
61
|
+
data = response.body['data']
|
|
62
|
+
assert data == "hello"
|
|
63
|
+
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
should "PUT" do
|
|
67
|
+
|
|
68
|
+
response = Unirest.put("http://httpbin.org/put", parameters:{'name' => 'Mark', 'nick'=> "sinz s"})
|
|
69
|
+
assert response.code == 200
|
|
70
|
+
|
|
71
|
+
args = response.body['form']
|
|
72
|
+
assert args.length == 2
|
|
73
|
+
assert args['name'] == 'Mark'
|
|
74
|
+
assert args['nick'] == 'sinz s'
|
|
75
|
+
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
should "PATCH" do
|
|
79
|
+
|
|
80
|
+
response = Unirest.patch("http://httpbin.org/patch", parameters:{'name' => 'Mark', 'nick'=> "sinz s"})
|
|
81
|
+
assert response.code == 200
|
|
82
|
+
|
|
83
|
+
args = response.body['form']
|
|
84
|
+
assert args.length == 2
|
|
85
|
+
assert args['name'] == 'Mark'
|
|
86
|
+
assert args['nick'] == 'sinz s'
|
|
87
|
+
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
should "DELETE" do
|
|
91
|
+
|
|
92
|
+
response = Unirest.delete("http://httpbin.org/delete", parameters:{'name' => 'Mark', 'nick'=> "sinz s"})
|
|
93
|
+
assert response.code == 200
|
|
94
|
+
|
|
95
|
+
data = response.body['data']
|
|
96
|
+
assert data == "name=Mark&nick=sinz%20s"
|
|
97
|
+
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
should "GET ASYNC" do
|
|
101
|
+
|
|
102
|
+
executed = false;
|
|
103
|
+
|
|
104
|
+
thread = Unirest.get("http://httpbin.org/get?name=Mark", parameters:{'nick'=> "sinz s"}) {|response|
|
|
105
|
+
assert response.code == 200
|
|
106
|
+
executed = true
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
assert thread != nil
|
|
110
|
+
assert executed == false
|
|
111
|
+
thread.join()
|
|
112
|
+
assert executed == true
|
|
113
|
+
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
should "gzip" do
|
|
117
|
+
|
|
118
|
+
response = Unirest.get("http://httpbin.org/gzip")
|
|
119
|
+
assert response.code == 200
|
|
120
|
+
|
|
121
|
+
assert response.body['gzipped'] == true
|
|
122
|
+
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
should "Basic Authentication" do
|
|
126
|
+
|
|
127
|
+
response = Unirest.get("http://httpbin.org/get?name=Mark", parameters:{'nick'=> "sinz s"}, auth:{:user=>"marco", :password=>"password"})
|
|
128
|
+
assert response.code == 200
|
|
129
|
+
|
|
130
|
+
headers = response.body['headers']
|
|
131
|
+
assert headers['Authorization'] == "Basic bWFyY286cGFzc3dvcmQ="
|
|
132
|
+
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
should "Timeout" do
|
|
136
|
+
|
|
137
|
+
Unirest.timeout(1);
|
|
138
|
+
begin
|
|
139
|
+
response = Unirest.get("http://httpbin.org/delay/3")
|
|
140
|
+
rescue RuntimeError
|
|
141
|
+
# Ok
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
Unirest.timeout(3);
|
|
145
|
+
response = Unirest.get("http://httpbin.org/delay/1")
|
|
146
|
+
assert response.code == 200
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
should "Default Headers" do
|
|
150
|
+
|
|
151
|
+
Unirest.default_header('Hello','test')
|
|
152
|
+
|
|
153
|
+
response = Unirest.get("http://httpbin.org/get")
|
|
154
|
+
assert response.code == 200
|
|
155
|
+
|
|
156
|
+
headers = response.body['headers']
|
|
157
|
+
assert headers['Hello'] == "test"
|
|
158
|
+
|
|
159
|
+
response = Unirest.get("http://httpbin.org/get")
|
|
160
|
+
assert response.code == 200
|
|
161
|
+
|
|
162
|
+
headers = response.body['headers']
|
|
163
|
+
assert headers['Hello'] == "test"
|
|
164
|
+
|
|
165
|
+
Unirest.clear_default_headers()
|
|
166
|
+
|
|
167
|
+
response = Unirest.get("http://httpbin.org/get")
|
|
168
|
+
assert response.code == 200
|
|
169
|
+
|
|
170
|
+
headers = response.body['headers']
|
|
171
|
+
assert headers['Hello'] == nil
|
|
172
|
+
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
end
|
|
176
|
+
end
|
data/unirest.gemspec
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Gem::Specification.new do |s|
|
|
2
|
+
s.name = 'unirest'
|
|
3
|
+
s.version = '1.1.1'
|
|
4
|
+
s.summary = "Unirest-Ruby"
|
|
5
|
+
s.description = "Unirest is a set of lightweight HTTP libraries available in multiple languages."
|
|
6
|
+
s.authors = ["Mashape", "Marco Palladino"]
|
|
7
|
+
s.email = 'support@mashape.com'
|
|
8
|
+
s.homepage = 'https://github.com/Mashape/unirest-ruby'
|
|
9
|
+
s.license = 'MIT'
|
|
10
|
+
|
|
11
|
+
s.add_dependency('rest-client', '~> 1.6.7')
|
|
12
|
+
s.add_dependency('json', '~> 1.8.1')
|
|
13
|
+
s.add_dependency('addressable', '~> 2.3.5')
|
|
14
|
+
|
|
15
|
+
s.add_development_dependency('shoulda', '~> 3.4.0')
|
|
16
|
+
s.add_development_dependency('test-unit')
|
|
17
|
+
s.add_development_dependency('rake')
|
|
18
|
+
|
|
19
|
+
s.required_ruby_version = '~> 2.0'
|
|
20
|
+
|
|
21
|
+
s.files = `git ls-files`.split("\n")
|
|
22
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
|
23
|
+
s.require_paths = ['lib']
|
|
24
|
+
|
|
25
|
+
end
|
metadata
CHANGED
|
@@ -1,77 +1,141 @@
|
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: unirest
|
|
3
|
-
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.1.1
|
|
5
5
|
platform: ruby
|
|
6
|
-
authors:
|
|
6
|
+
authors:
|
|
7
7
|
- Mashape
|
|
8
|
+
- Marco Palladino
|
|
8
9
|
autorequire:
|
|
9
10
|
bindir: bin
|
|
10
11
|
cert_chain: []
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
- !ruby/object:Gem::Dependency
|
|
12
|
+
date: 2013-11-01 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: rest-client
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
- !ruby/object:Gem::Version
|
|
22
|
-
version: "0"
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
requirements:
|
|
18
|
+
- - ~>
|
|
19
|
+
- !ruby/object:Gem::Version
|
|
20
|
+
version: 1.6.7
|
|
23
21
|
type: :runtime
|
|
24
|
-
version_requirements: *id001
|
|
25
|
-
- !ruby/object:Gem::Dependency
|
|
26
|
-
name: json
|
|
27
22
|
prerelease: false
|
|
28
|
-
|
|
29
|
-
requirements:
|
|
30
|
-
-
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - ~>
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: 1.6.7
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: json
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ~>
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: 1.8.1
|
|
31
35
|
type: :runtime
|
|
32
|
-
version_requirements: *id003
|
|
33
|
-
- !ruby/object:Gem::Dependency
|
|
34
|
-
name: addressable
|
|
35
36
|
prerelease: false
|
|
36
|
-
|
|
37
|
-
requirements:
|
|
38
|
-
-
|
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ~>
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: 1.8.1
|
|
42
|
+
- !ruby/object:Gem::Dependency
|
|
43
|
+
name: addressable
|
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ~>
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: 2.3.5
|
|
39
49
|
type: :runtime
|
|
40
|
-
|
|
41
|
-
|
|
50
|
+
prerelease: false
|
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ~>
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: 2.3.5
|
|
56
|
+
- !ruby/object:Gem::Dependency
|
|
57
|
+
name: shoulda
|
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ~>
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: 3.4.0
|
|
63
|
+
type: :development
|
|
64
|
+
prerelease: false
|
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ~>
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: 3.4.0
|
|
70
|
+
- !ruby/object:Gem::Dependency
|
|
71
|
+
name: test-unit
|
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
|
73
|
+
requirements:
|
|
74
|
+
- - '>='
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
version: '0'
|
|
77
|
+
type: :development
|
|
78
|
+
prerelease: false
|
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
80
|
+
requirements:
|
|
81
|
+
- - '>='
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: '0'
|
|
84
|
+
- !ruby/object:Gem::Dependency
|
|
85
|
+
name: rake
|
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
- - '>='
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: '0'
|
|
91
|
+
type: :development
|
|
92
|
+
prerelease: false
|
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
94
|
+
requirements:
|
|
95
|
+
- - '>='
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: '0'
|
|
98
|
+
description: Unirest is a set of lightweight HTTP libraries available in multiple
|
|
99
|
+
languages.
|
|
42
100
|
email: support@mashape.com
|
|
43
101
|
executables: []
|
|
44
|
-
|
|
45
102
|
extensions: []
|
|
46
|
-
|
|
47
103
|
extra_rdoc_files: []
|
|
48
|
-
|
|
49
|
-
|
|
104
|
+
files:
|
|
105
|
+
- .gitignore
|
|
106
|
+
- .travis.yml
|
|
107
|
+
- Gemfile
|
|
108
|
+
- LICENSE
|
|
109
|
+
- README.md
|
|
110
|
+
- Rakefile
|
|
111
|
+
- lib/unirest.rb
|
|
50
112
|
- lib/unirest/http_request.rb
|
|
51
113
|
- lib/unirest/http_response.rb
|
|
52
|
-
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
114
|
+
- test/unirest_test.rb
|
|
115
|
+
- unirest.gemspec
|
|
116
|
+
homepage: https://github.com/Mashape/unirest-ruby
|
|
117
|
+
licenses:
|
|
118
|
+
- MIT
|
|
56
119
|
metadata: {}
|
|
57
|
-
|
|
58
120
|
post_install_message:
|
|
59
121
|
rdoc_options: []
|
|
60
|
-
|
|
61
|
-
require_paths:
|
|
122
|
+
require_paths:
|
|
62
123
|
- lib
|
|
63
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
|
-
requirements:
|
|
65
|
-
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
|
+
requirements:
|
|
126
|
+
- - ~>
|
|
127
|
+
- !ruby/object:Gem::Version
|
|
128
|
+
version: '2.0'
|
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
|
+
requirements:
|
|
131
|
+
- - '>='
|
|
132
|
+
- !ruby/object:Gem::Version
|
|
133
|
+
version: '0'
|
|
69
134
|
requirements: []
|
|
70
|
-
|
|
71
135
|
rubyforge_project:
|
|
72
|
-
rubygems_version: 2.0.
|
|
136
|
+
rubygems_version: 2.0.3
|
|
73
137
|
signing_key:
|
|
74
138
|
specification_version: 4
|
|
75
|
-
summary: Unirest
|
|
76
|
-
test_files:
|
|
77
|
-
|
|
139
|
+
summary: Unirest-Ruby
|
|
140
|
+
test_files:
|
|
141
|
+
- test/unirest_test.rb
|