moesif_unirest 1.1.4
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 -0
- data/.editorconfig +16 -0
- data/.gitignore +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +174 -0
- data/Rakefile +10 -0
- data/lib/unirest/http_request.rb +89 -0
- data/lib/unirest/http_response.rb +50 -0
- data/lib/unirest.rb +122 -0
- data/test/unirest_test.rb +198 -0
- data/unirest.gemspec +25 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 28968cd5012a5c282924eeecc27a37a1d514137371dbf3cecaca08a5da470457
|
4
|
+
data.tar.gz: 635b928a41248f99dae50b1192ee8e2c02454fdedf65a8872fa123489d408655
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a703291640be82fbf4e279982f5ef9ceb4a449cefe24937ca7feb462eefe624dac425d48c574332801b9e7c7a424717089171950c3eded93ed49a498f8dde777
|
7
|
+
data.tar.gz: 6184175c1ce0186f9878aa6c03e80d53e606b404b846538272128eb5af602091fa6e01231b53a9b36da79475d7a58badb1ee0af937c1719f969e524a2be80607
|
data/.editorconfig
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# http://editorconfig.org
|
2
|
+
root = true
|
3
|
+
|
4
|
+
[*]
|
5
|
+
indent_style = space
|
6
|
+
indent_size = 4
|
7
|
+
end_of_line = lf
|
8
|
+
charset = utf-8
|
9
|
+
trim_trailing_whitespace = true
|
10
|
+
insert_final_newline = true
|
11
|
+
|
12
|
+
[*.yml]
|
13
|
+
indent_size = 2
|
14
|
+
|
15
|
+
[*.{md,yml}]
|
16
|
+
trim_trailing_whitespace = false
|
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-2015 Mashape (https://www.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,174 @@
|
|
1
|
+
# Unirest for Ruby [![Build Status][travis-image]][travis-url] [![version][gem-version]][gem-url]
|
2
|
+
|
3
|
+
[![License][license-image]][license-url]
|
4
|
+
[![Downloads][gem-downloads]][gem-url]
|
5
|
+
[![Code Climate][codeclimate-quality]][codeclimate-url]
|
6
|
+
[![Gitter][gitter-image]][gitter-url]
|
7
|
+
|
8
|
+
![][unirest-logo]
|
9
|
+
|
10
|
+
|
11
|
+
[Unirest](http://unirest.io) is a set of lightweight HTTP libraries available in multiple languages, built and maintained by [Mashape](https://github.com/Mashape), who also maintain the open-source API Gateway [Kong](https://github.com/Mashape/kong).
|
12
|
+
|
13
|
+
## Features
|
14
|
+
|
15
|
+
* Make `GET`, `POST`, `PUT`, `PATCH`, `DELETE` requests
|
16
|
+
* Both syncronous and asynchronous (non-blocking) requests
|
17
|
+
* Supports form parameters, file uploads and custom body entities
|
18
|
+
* Supports gzip
|
19
|
+
* Supports Basic Authentication natively
|
20
|
+
* Customizable timeout
|
21
|
+
* Customizable default headers for every request (DRY)
|
22
|
+
* Automatic JSON parsing into a native object for JSON responses
|
23
|
+
|
24
|
+
## Installing
|
25
|
+
|
26
|
+
Requirements: **Ruby >= 2.0**
|
27
|
+
|
28
|
+
To utilize unirest, install the `unirest` gem:
|
29
|
+
|
30
|
+
```bash
|
31
|
+
gem install unirest
|
32
|
+
```
|
33
|
+
|
34
|
+
After installing the gem package you can now begin to simplifying requests by requiring `unirest`:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
require 'unirest'
|
38
|
+
```
|
39
|
+
|
40
|
+
## Creating Requests
|
41
|
+
|
42
|
+
So you're probably wondering how using Unirest makes creating requests in Ruby easier, let's start with a working example:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
response = Unirest.post "http://httpbin.org/post",
|
46
|
+
headers:{ "Accept" => "application/json" },
|
47
|
+
parameters:{ :age => 23, :foo => "bar" }
|
48
|
+
|
49
|
+
response.code # Status code
|
50
|
+
response.headers # Response headers
|
51
|
+
response.body # Parsed body
|
52
|
+
response.raw_body # Unparsed body
|
53
|
+
```
|
54
|
+
|
55
|
+
## Asynchronous Requests
|
56
|
+
Unirest-Ruby also supports asynchronous requests with a callback function specified inside a block, like:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
response = Unirest.post "http://httpbin.org/post",
|
60
|
+
headers:{ "Accept" => "application/json" },
|
61
|
+
parameters:{ :age => 23, :foo => "bar" } {|response|
|
62
|
+
response.code # Status code
|
63
|
+
response.headers # Response headers
|
64
|
+
response.body # Parsed body
|
65
|
+
response.raw_body # Unparsed body
|
66
|
+
}
|
67
|
+
```
|
68
|
+
|
69
|
+
## File Uploads
|
70
|
+
```ruby
|
71
|
+
response = Unirest.post "http://httpbin.org/post",
|
72
|
+
headers:{ "Accept" => "application/json" },
|
73
|
+
parameters:{ :age => 23, :file => File.new("/path/to/file", 'rb') }
|
74
|
+
```
|
75
|
+
|
76
|
+
## Custom Entity Body
|
77
|
+
```ruby
|
78
|
+
response = Unirest.post "http://httpbin.org/post",
|
79
|
+
headers:{ "Accept" => "application/json" },
|
80
|
+
parameters:{ :age => "value", :foo => "bar" }.to_json # Converting the Hash to a JSON string
|
81
|
+
```
|
82
|
+
|
83
|
+
### Basic Authentication
|
84
|
+
|
85
|
+
Authenticating the request with basic authentication can be done by providing an `auth` Hash with `:user` and `:password` keys like:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
response = Unirest.get "http://httpbin.org/get", auth:{:user=>"username", :password=>"password"}
|
89
|
+
```
|
90
|
+
|
91
|
+
# Request
|
92
|
+
```ruby
|
93
|
+
Unirest.get(url, headers: {}, parameters: nil, auth:nil, &callback)
|
94
|
+
Unirest.post(url, headers: {}, parameters: nil, auth:nil, &callback)
|
95
|
+
Unirest.delete(url, headers: {}, parameters: nil, auth:nil, &callback)
|
96
|
+
Unirest.put(url, headers: {}, parameters: nil, auth:nil, &callback)
|
97
|
+
Unirest.patch(url, headers: {}, parameters: nil, auth:nil, &callback)
|
98
|
+
```
|
99
|
+
|
100
|
+
- `url` (`String`) - Endpoint, address, or uri to be acted upon and requested information from.
|
101
|
+
- `headers` (`Object`) - Request Headers as associative array or object
|
102
|
+
- `parameters` (`Array` | `Object` | `String`) - Request Body associative array or object
|
103
|
+
- `callback` (`Function`) - _Optional_; Asychronous callback method to be invoked upon result.
|
104
|
+
|
105
|
+
# Response
|
106
|
+
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.
|
107
|
+
|
108
|
+
- `code` - HTTP Response Status Code (Example `200`)
|
109
|
+
- `headers` - HTTP Response Headers
|
110
|
+
- `body` - Parsed response body where applicable, for example JSON responses are parsed to Objects / Associative Arrays.
|
111
|
+
- `raw_body` - Un-parsed response body
|
112
|
+
|
113
|
+
# Advanced Configuration
|
114
|
+
|
115
|
+
You can set some advanced configuration to tune Unirest-Ruby:
|
116
|
+
|
117
|
+
### Timeout
|
118
|
+
|
119
|
+
You can set a custom timeout value (in **seconds**):
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
Unirest.timeout(5) # 5s timeout
|
123
|
+
```
|
124
|
+
|
125
|
+
### Default Request Headers
|
126
|
+
|
127
|
+
You can set default headers that will be sent on every request:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
Unirest.default_header('Header1','Value1')
|
131
|
+
Unirest.default_header('Header2','Value2')
|
132
|
+
```
|
133
|
+
|
134
|
+
You can clear the default headers anytime with:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
Unirest.clear_default_headers()
|
138
|
+
```
|
139
|
+
|
140
|
+
### User-Agent
|
141
|
+
|
142
|
+
The default User-Agent string is `unirest-ruby/1.1`. You can customize
|
143
|
+
it like this:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
Unirest.user_agent("custom_user_agent")
|
147
|
+
```
|
148
|
+
|
149
|
+
----
|
150
|
+
|
151
|
+
Made with ♥ from the [Mashape](https://www.mashape.com/) team
|
152
|
+
|
153
|
+
[unirest-logo]: http://cl.ly/image/2P373Y090s2O/Image%202015-10-12%20at%209.48.06%20PM.png
|
154
|
+
|
155
|
+
|
156
|
+
[license-url]: https://github.com/Mashape/unirest-ruby/blob/master/LICENSE
|
157
|
+
[license-image]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat
|
158
|
+
|
159
|
+
[gitter-url]: https://gitter.im/Mashape/unirest-ruby
|
160
|
+
[gitter-image]: https://img.shields.io/badge/Gitter-Join%20Chat-blue.svg?style=flat
|
161
|
+
|
162
|
+
[travis-url]: https://travis-ci.org/Mashape/unirest-ruby
|
163
|
+
[travis-image]: https://img.shields.io/travis/Mashape/unirest-ruby.svg?style=flat
|
164
|
+
|
165
|
+
[gem-url]: https://rubygems.org/gems/unirest
|
166
|
+
[gem-version]: https://img.shields.io/gem/v/unirest.svg?style=flat
|
167
|
+
[gem-downloads]: https://img.shields.io/gem/dt/unirest.svg?style=flat
|
168
|
+
|
169
|
+
[codeclimate-url]: https://codeclimate.com/github/Mashape/unirest-ruby
|
170
|
+
[codeclimate-quality]: https://img.shields.io/codeclimate/github/Mashape/unirest-ruby.svg?style=flat
|
171
|
+
[codeclimate-coverage]: https://img.shields.io/codeclimate/coverage/github/Mashape/unirest-ruby.svg?style=flat
|
172
|
+
|
173
|
+
[versioneye-url]: https://www.versioneye.com/user/projects/x
|
174
|
+
[versioneye-image]: https://img.shields.io/versioneye/d/user/projects/x.svg?style=flat
|
data/Rakefile
ADDED
@@ -0,0 +1,89 @@
|
|
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 'addressable/uri'
|
26
|
+
require "base64"
|
27
|
+
|
28
|
+
module Unirest
|
29
|
+
|
30
|
+
class HttpRequest
|
31
|
+
attr_reader :method
|
32
|
+
attr_reader :url
|
33
|
+
attr_reader :headers
|
34
|
+
attr_reader :body
|
35
|
+
attr_reader :auth
|
36
|
+
|
37
|
+
def add_header(name, value)
|
38
|
+
@headers[name] = value
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize(method, url, headers = {}, body = nil, auth = nil)
|
42
|
+
@method = method
|
43
|
+
|
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
|
61
|
+
raise "Invalid URL: #{url}"
|
62
|
+
end
|
63
|
+
|
64
|
+
@url = url.gsub /\s+/, '%20'
|
65
|
+
|
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 #{["#{user}:#{password}"].pack('m').delete("\r\n")}"
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
# Make the header key lowercase
|
84
|
+
headers.each_pair {|key, value| @headers[key.downcase] = value }
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,50 @@
|
|
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 'json'
|
26
|
+
|
27
|
+
module Unirest
|
28
|
+
|
29
|
+
class HttpResponse
|
30
|
+
attr_reader :code
|
31
|
+
attr_reader :raw_body
|
32
|
+
attr_reader :body
|
33
|
+
attr_reader :headers
|
34
|
+
|
35
|
+
def initialize(http_response)
|
36
|
+
@code = http_response.code
|
37
|
+
@headers = http_response.headers
|
38
|
+
@raw_body = http_response
|
39
|
+
@body = @raw_body
|
40
|
+
|
41
|
+
begin
|
42
|
+
@body = JSON.parse(@raw_body)
|
43
|
+
rescue StandardError
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
data/lib/unirest.rb
ADDED
@@ -0,0 +1,122 @@
|
|
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 'rubygems'
|
26
|
+
require 'rest-client'
|
27
|
+
|
28
|
+
require File.join(File.dirname(__FILE__), "/unirest/http_request.rb")
|
29
|
+
require File.join(File.dirname(__FILE__), "/unirest/http_response.rb")
|
30
|
+
|
31
|
+
module Unirest
|
32
|
+
|
33
|
+
@@timeout = 10
|
34
|
+
@@default_headers = {}
|
35
|
+
@@user_agent = "unirest-ruby/1.1"
|
36
|
+
|
37
|
+
class HttpClient
|
38
|
+
|
39
|
+
def self.request(method, url, headers, body, auth, timeout, &callback)
|
40
|
+
http_request = Unirest::HttpRequest.new(method, url, headers, body, auth)
|
41
|
+
|
42
|
+
if callback
|
43
|
+
Thread.new do
|
44
|
+
callback.call(self.internal_request(http_request, timeout))
|
45
|
+
end
|
46
|
+
else
|
47
|
+
self.internal_request(http_request, timeout)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.internal_request(http_request, timeout)
|
52
|
+
# Set the user agent
|
53
|
+
http_request.add_header("user-agent", Unirest.user_agent)
|
54
|
+
http_request.add_header("accept-encoding", "gzip")
|
55
|
+
|
56
|
+
http_response = nil
|
57
|
+
|
58
|
+
begin
|
59
|
+
case http_request.method
|
60
|
+
when :get
|
61
|
+
http_response = RestClient::Request.execute(:method => :get, :url => http_request.url, :headers => http_request.headers, :timeout => timeout)
|
62
|
+
when :post
|
63
|
+
http_response = RestClient::Request.execute(:method => :post, :url => http_request.url, :payload => http_request.body, :headers => http_request.headers, :timeout => timeout)
|
64
|
+
when :put
|
65
|
+
http_response = RestClient::Request.execute(:method => :put, :url => http_request.url, :payload => http_request.body, :headers => http_request.headers, :timeout => timeout)
|
66
|
+
when :delete
|
67
|
+
http_response = RestClient::Request.execute(:method => :delete, :url => http_request.url, :payload => http_request.body, :headers => http_request.headers, :timeout => timeout)
|
68
|
+
when :patch
|
69
|
+
http_response = RestClient::Request.execute(:method => :patch, :url => http_request.url, :payload => http_request.body, :headers => http_request.headers, :timeout => timeout)
|
70
|
+
end
|
71
|
+
rescue RestClient::RequestTimeout
|
72
|
+
raise 'Request Timeout'
|
73
|
+
rescue RestClient::Exception => e
|
74
|
+
http_response = e.response
|
75
|
+
end
|
76
|
+
|
77
|
+
Unirest::HttpResponse.new(http_response)
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.default_header(name, value)
|
83
|
+
@@default_headers[name] = value
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.clear_default_headers
|
87
|
+
@@default_headers = {}
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.timeout(seconds)
|
91
|
+
@@timeout = seconds
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.user_agent(user_agent=@@user_agent)
|
95
|
+
if user_agent
|
96
|
+
@@user_agent = user_agent
|
97
|
+
else
|
98
|
+
@@user_agent
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.get(url, headers: {}, parameters: nil, auth:nil, &callback)
|
103
|
+
HttpClient.request(:get, url, headers.merge(@@default_headers), parameters, auth, @@timeout, &callback)
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.post(url, headers: {}, parameters: nil, auth:nil, &callback)
|
107
|
+
HttpClient.request(:post, url, headers.merge(@@default_headers), parameters, auth, @@timeout, &callback)
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.delete(url, headers: {}, parameters: nil, auth:nil, &callback)
|
111
|
+
HttpClient.request(:delete, url, headers.merge(@@default_headers), parameters, auth, @@timeout, &callback)
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.put(url, headers: {}, parameters: nil, auth:nil, &callback)
|
115
|
+
HttpClient.request(:put, url, headers.merge(@@default_headers), parameters, auth, @@timeout, &callback)
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.patch(url, headers: {}, parameters: nil, auth:nil, &callback)
|
119
|
+
HttpClient.request(:patch, url, headers.merge(@@default_headers), parameters, auth, @@timeout, &callback)
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
@@ -0,0 +1,198 @@
|
|
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", headers:{ "Content-Type" => "application/json" }, parameters:{'name' => 'Mark'}.to_json)
|
59
|
+
assert response.code == 200
|
60
|
+
|
61
|
+
data = response.body['data']
|
62
|
+
assert data == '{"name":"Mark"}'
|
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['form']
|
96
|
+
assert data['name'] == "Mark"
|
97
|
+
assert data['nick'] == "sinz s"
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
should "GET ASYNC" do
|
102
|
+
|
103
|
+
executed = false;
|
104
|
+
|
105
|
+
thread = Unirest.get("http://httpbin.org/get?name=Mark", parameters:{'nick'=> "sinz s"}) {|response|
|
106
|
+
assert response.code == 200
|
107
|
+
executed = true
|
108
|
+
}
|
109
|
+
|
110
|
+
assert thread != nil
|
111
|
+
assert executed == false
|
112
|
+
thread.join()
|
113
|
+
assert executed == true
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
should "gzip" do
|
118
|
+
|
119
|
+
response = Unirest.get("http://httpbin.org/gzip")
|
120
|
+
assert response.code == 200
|
121
|
+
|
122
|
+
assert response.body['gzipped'] == true
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
should "Basic Authentication" do
|
127
|
+
|
128
|
+
response = Unirest.get("http://httpbin.org/get?name=Mark", parameters:{'nick'=> "sinz s"}, auth:{:user=>"marco", :password=>"password"})
|
129
|
+
assert response.code == 200
|
130
|
+
|
131
|
+
headers = response.body['headers']
|
132
|
+
assert headers['Authorization'] == "Basic bWFyY286cGFzc3dvcmQ="
|
133
|
+
|
134
|
+
end
|
135
|
+
|
136
|
+
should "Timeout" do
|
137
|
+
|
138
|
+
Unirest.timeout(1);
|
139
|
+
begin
|
140
|
+
response = Unirest.get("http://httpbin.org/delay/3")
|
141
|
+
rescue RuntimeError
|
142
|
+
# Ok
|
143
|
+
end
|
144
|
+
|
145
|
+
Unirest.timeout(3);
|
146
|
+
response = Unirest.get("http://httpbin.org/delay/1")
|
147
|
+
assert response.code == 200
|
148
|
+
end
|
149
|
+
|
150
|
+
should "Throw underlying error" do
|
151
|
+
|
152
|
+
begin
|
153
|
+
Unirest.get("http://bad.domain.test")
|
154
|
+
rescue => e
|
155
|
+
assert ! e.is_a?(NoMethodError)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
should "Default Headers" do
|
160
|
+
|
161
|
+
Unirest.default_header('Hello','test')
|
162
|
+
|
163
|
+
response = Unirest.get("http://httpbin.org/get")
|
164
|
+
assert response.code == 200
|
165
|
+
|
166
|
+
headers = response.body['headers']
|
167
|
+
assert headers['Hello'] == "test"
|
168
|
+
|
169
|
+
response = Unirest.get("http://httpbin.org/get")
|
170
|
+
assert response.code == 200
|
171
|
+
|
172
|
+
headers = response.body['headers']
|
173
|
+
assert headers['Hello'] == "test"
|
174
|
+
|
175
|
+
Unirest.clear_default_headers()
|
176
|
+
|
177
|
+
response = Unirest.get("http://httpbin.org/get")
|
178
|
+
assert response.code == 200
|
179
|
+
|
180
|
+
headers = response.body['headers']
|
181
|
+
assert headers['Hello'] == nil
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
should "Custom User Agent" do
|
186
|
+
|
187
|
+
Unirest.user_agent("custom_ua/1.0")
|
188
|
+
|
189
|
+
response = Unirest.get("http://httpbin.org/get")
|
190
|
+
assert response.code == 200
|
191
|
+
|
192
|
+
headers = response.body['headers']
|
193
|
+
assert headers['User-Agent'] == "custom_ua/1.0"
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
end
|
data/unirest.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'moesif_unirest'
|
3
|
+
s.version = '1.1.4'
|
4
|
+
s.summary = "Unirest-Ruby"
|
5
|
+
s.description = "Simplified, lightweight HTTP client library"
|
6
|
+
s.authors = ["Moesif, Inc"]
|
7
|
+
s.email = 'derric@moesif.com'
|
8
|
+
s.homepage = 'https://github.com/Moesif/unirest-ruby'
|
9
|
+
s.license = 'MIT'
|
10
|
+
|
11
|
+
s.add_dependency('rest-client', '>= 1.8.0')
|
12
|
+
s.add_dependency('json', '>= 1.8.1')
|
13
|
+
s.add_dependency('addressable', '>= 2.3.5')
|
14
|
+
|
15
|
+
s.add_development_dependency('shoulda', '~> 3.5.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
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moesif_unirest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Moesif, Inc
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.8.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.8.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.8.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.8.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: addressable
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.3.5
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.3.5
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: shoulda
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.5.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.5.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: test-unit
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Simplified, lightweight HTTP client library
|
98
|
+
email: derric@moesif.com
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files: []
|
102
|
+
files:
|
103
|
+
- ".editorconfig"
|
104
|
+
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/unirest.rb
|
111
|
+
- lib/unirest/http_request.rb
|
112
|
+
- lib/unirest/http_response.rb
|
113
|
+
- test/unirest_test.rb
|
114
|
+
- unirest.gemspec
|
115
|
+
homepage: https://github.com/Moesif/unirest-ruby
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata: {}
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - "~>"
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '2.0'
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.7.7
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Unirest-Ruby
|
139
|
+
test_files:
|
140
|
+
- test/unirest_test.rb
|